Jackrabbit Oak Lucine index и SQL2 запрос для полнотекстового поиска в txt и pdf - PullRequest
1 голос
/ 02 октября 2019

Я пытаюсь реализовать полнотекстовый поиск по содержимому файла, используя версию 1.16.0 Oak.

Попытка создать индекс, как сказано в документации Oak, для индексации всех свойств.

/oak:index/assetType
  - jcr:primaryType = "oak:QueryIndexDefinition"
  - type = "lucene"
  - compatVersion = 2
  - async = "async"
  + indexRules
    - jcr:primaryType = "nt:unstructured"
    + nt:base
      + properties
        - jcr:primaryType = "nt:unstructured"
        + allProps
          - name = ".*"
          - isRegexp = true
          - nodeScopeIndex = true
  1. Создать индекс. Пробовал разные комбинации типов узлов. Ничего не работает.
 public static void createIndex(Repository repository) {
        Session session = null;
        try {
            session = repository.login();

            Node root = session.getRootNode();
            Node index = root.getNode("oak:index");
            Node lucineIndex = index.addNode("assetType","oak:QueryIndexDefinition");
            lucineIndex.setProperty("compatVersion", "2");
            lucineIndex.setProperty("type", "lucene");
            lucineIndex.setProperty("async", "async");
            Node rules = lucineIndex.addNode("indexRules", "nt:unstructured");
                Node base = rules.addNode("nt:base");
                    Node properties = base.addNode("properties", "nt:unstructured");
                        Node allProps = properties.addNode("allProps");
                        allProps.setProperty("jcr:content", ".*");
                        allProps.setProperty("isRegexp", true);
                        allProps.setProperty("nodeScopeIndex", true);
            session.save();
        } catch (LoginException e) {
            e.printStackTrace();
        } catch (RepositoryException e) {
            e.printStackTrace();
        } finally {
            session.logout();
        }
    }
Добавить файл
    public static void saveFileIfNotExist(byte[] rawFile, String fileName, String folderName, String mimeType, Repository repository) {
        Session session = null;
        try {
            session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
            Node root = session.getRootNode();
            Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(rawFile));
            if(!root.hasNode(folderName)) {
                System.out.println("NO FOLDER");
                Node folder = root.addNode(folderName, "nt:folder");
                Node file = folder.addNode(fileName, "nt:file");
                Node content = file.addNode("jcr:content", "nt:resource");
                content.setProperty("jcr:mimeType", mimeType);
                content.setProperty("jcr:data", binary);
            } else {
                System.out.println("FOLDER EXIST");
            }
            session.save();
        }
        catch (RepositoryException e) {
            e.printStackTrace();
        }  finally {
            session.logout();
        }
    }

Содержимое файла:

An implementation of the Value interface must override the inherited method
Object.equals(Object) so that, given Value instances V1 and V2,
V1.equals(V2) will return true if.
Попробуйте найти содержимое файла
DocumentNodeStore rdb = new DocumentNodeStore(new RDBDocumentNodeStoreBuilder().setRDBConnection(dataSource));
        Repository repo = new Jcr(new Oak(rdb)).with(new OpenSecurityProvider()).createRepository();


createIndex(repo);

        byte[] rawFile = readBytes("D:\\file.txt");
        saveFileIfNotExist(rawFile, "txt_folder", "text_file", "text/plain", repo);


        Session session = null;
        try {
            session = repo.login();
            Node root = session.getRootNode();
            Node index = root.getNode("oak:index");
            QueryManager queryManager = session.getWorkspace().getQueryManager();session.getWorkspace().getQueryManager();

            Query query = queryManager.createQuery("SELECT * FROM [nt:resource] AS s WHERE CONTAINS(s.*, '*so*') option(traversal warn)", Query.JCR_SQL2);

            QueryResult result = query.execute();
            RowIterator ri = result.getRows();
            while (ri.hasNext()) {
                Row row = ri.nextRow();
                System.out.println("Row: " + row.toString());
            }

        } catch (RepositoryException e) {
            e.printStackTrace();
        }
        finally {
            session.logout();
            ((RepositoryImpl) repo).shutdown();
            rdb.dispose();
        }

Но ничего не возвращается и выдает в журнале предупреждение:

2019-10-02 18:27:35,821 [main] WARN  QueryImpl - Traversal query (query without index): SELECT * FROM [nt:resource] AS s WHERE CONTAINS(s.*, '*so*') option(traversal warn); consider creating an index
  1. Итак, как правильно составить индекс и сделатьправильный запрос для поиска по содержимому файла?
  2. Как искать в pdf документах?
...