首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lucene索引:查询时获取空结果

Lucene索引:查询时获取空结果
EN

Stack Overflow用户
提问于 2019-06-19 03:02:22
回答 1查看 465关注 0票数 0

我试图用Lucene索引进行查询,但在日志中得到了空的结果和下方的错误。

代码语言:javascript
复制
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]

我正在使用关系型数据库DocumentStore,我已经检查过索引和节点是在节点表中创建的。

代码语言:javascript
复制
   @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索引的示例代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-21 17:43:20

你可能要做的事情有几个问题。第一件事就是你观察到的错误。因为您使用的是通过type="counter"提供索引的InitialContent。为此,您需要在构建存储库时使用.with(new NodeCounterEditorProvider())。这应该可以避免您看到的错误。

但是,您的代码可能仍然无法工作,因为lucene索引是异步的(您已经正确配置了)。由于这种异步行为,您不能在添加节点后立即进行查询。我试过你的代码,但在进行查询之前不得不添加一些类似Thread.sleep(10*1000)的东西。

作为另一个附注,我建议您尝试使用IndexDefinitionBuilder来构建lucene索引结构。因此,您可以替换为Tree index = root.getTree("/");

代码语言:javascript
复制
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();

使用

代码语言:javascript
复制
IndexDefinitionBuilder idxBuilder = new IndexDefinitionBuilder();
idxBuilder.indexRule("nt:base").property("name").propertyIndex();
idxBuilder.build(root.getTree("/").addChild("oak:index").addChild("lucene"));
root.commit();

后一种方法,即imo,更不容易出错,并且更具可重用性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56655489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档