Lucene索引:查询时得到空结果

时间:2019-06-18 19:02:21

标签: 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索引的示例代码?

1 个答案:

答案 0 :(得分:0)

您可能正在做的事情有两个问题。第一件事就是您正在观察的错误。由于您使用的是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();

后一种方法imo不太容易出错,而更容易重复使用。