java - Lucene索引: getting empty result while query

标签 java indexing lucene jackrabbit-oak

我尝试使用 Lucene 索引进行查询,但在日志中得到空结果和以下错误,

Traversal query (query without index): select [jcr:path] from [nt:base] where isdescendantnode('/test') and name='World'; consider creating an index


[async] The index update failed
org.apache.jackrabbit.oak.api.CommitFailedException: OakAsync0002: Missing index provider detected for type [counter] on index [/oak:index/counter]


我正在使用 RDB DocumentStore,并且我已经检查了在节点表中创建的索引和节点。我尝试了下面的代码,

   @Autowired 
   NodeStore rdbNodeStore;

   //create reposiotory
   LuceneIndexProvider provider = new LuceneIndexProvider();
   ContentRepository repository = new Oak(rdbNodeStore)
                .with(new OpenSecurityProvider())
                .with(new InitialContent())
                .with((QueryIndexProvider) provider)
                .with((Observer) provider)
                .with(new LuceneIndexEditorProvider())
                .withAsyncIndexing("async", 
   5).createContentRepository();

    //login reposiotory and retrive session
    ContentSession contentSession = repository.login(null, null);
    Root root = contentSession.getLatestRoot();

    //create lucene index
      Tree index = root.getTree("/");

      Tree t = index.addChild("oak:index");

      t = t.addChild("lucene");
      t.setProperty("jcr:primaryType", "oak:QueryIndexDefinition", Type.NAME);
      t.setProperty("compatVersion", Long.valueOf(2L), Type.LONG);
      t.setProperty("type", "lucene", Type.STRING);
      t.setProperty("async", "async", Type.STRING);

      t = t.addChild("indexRules");
      t = t.addChild("nt:base");
      Tree propnode = t.addChild("properties");
      Tree t1 = propnode.addChild("name");
      t1.setProperty("name", "name");
      t1.setProperty("propertyIndex", Boolean.valueOf(true), Type.BOOLEAN);
      root.commit();

      //Create TestNode
      String h = "Hello" + System.currentTimeMillis();
      String w = "World" + System.currentTimeMillis();

      Tree test = root.getTree("/").addChild("test");
      test.addChild("a").setProperty("name", Arrays.asList(new String[] { h, w }), Type.STRINGS);
      test.addChild("b").setProperty("name", h);
      root.commit();

      //Search
      String query = "select [jcr:path] from [nt:base] where isdescendantnode('/test') and name='World' option(traversal ok)";

      List<String> paths = executeQuery(root, query, "JCR-SQL2", true, false);
      for (String path : paths) {
        System.out.println("Path=" + path);
      }

谁能分享一些关于如何创建 Lucene 索引的示例代码吗?

最佳答案

您可能正在做的事情存在一些问题。首先是您观察到的错误。由于您使用的是 InitialContent,它使用 type="counter" 提供索引。为此,您需要在构建存储库时使用 .with(new NodeCounterEditorProvider()) 。这应该可以避免您看到的错误。

但是,您的代码可能仍然无法工作,因为 lucene 索引是异步的(您已正确配置)。由于这种异步行为,您无法在添加节点后立即查询。 我尝试了您的代码,但在查询之前必须添加类似 Thread.sleep(10*1000) 的内容。

作为另一个旁注,我建议您尝试使用 IndexDefinitionBuilder 来制作 lucene 索引结构。所以,你可以替换 树索引 = root.getTree("/");

Tree t = index.addChild("oak:index");

t = t.addChild("lucene");
t.setProperty("jcr:primaryType", "oak:QueryIndexDefinition", Type.NAME);
t.setProperty("compatVersion", Long.valueOf(2L), Type.LONG);
t.setProperty("type", "lucene", Type.STRING);
t.setProperty("async", "async", Type.STRING);

t = t.addChild("indexRules");
t = t.addChild("nt:base");
Tree propnode = t.addChild("properties");
Tree t1 = propnode.addChild("name");
t1.setProperty("name", "name");
t1.setProperty("propertyIndex", Boolean.valueOf(true), Type.BOOLEAN);
root.commit();

IndexDefinitionBuilder idxBuilder = new IndexDefinitionBuilder();
idxBuilder.indexRule("nt:base").property("name").propertyIndex();
idxBuilder.build(root.getTree("/").addChild("oak:index").addChild("lucene"));
root.commit();

在我看来,后一种方法更不容易出错并且更容易重做。

关于java - Lucene索引: getting empty result while query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655489/

相关文章:

java - Thymeleaf 或 JSP : which is better with Spring Boot?

solr - 在 Solr 中获得相似度分数

lucene - Elasticsearch/Lucene亮点

python - 如何在 Django 模板中使用变量作为索引?

java - Elastic Search - 排序 - 在 0 条件下在整数字段之间切换

lucene - 有和没有_all的elasticsearch索引大小

java - 部署到 Jboss 7.1.0 EAP 时未指定持久性单元名称错误

java - 组织.hibernate.HibernateException : No TransactionManagerLookup specified on hibernate upgrade

java - Spring Boot Filter 没有过滤我所有的日志

在 Apache Solr 中对文件夹及其子文件夹中包含的所有文件进行索引