Lucene索引:查询时得到空结果 [英] Lucene index: getting empty result while query

查看:202
本文介绍了Lucene索引:查询时得到空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Lucene索引进行查询,但结果为空,并且日志中出现以下错误,

I am trying to query with Lucene index but getting the empty result and below errors in the log,

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,并检查了索引和节点是否在节点表中创建.我尝试了以下代码,

I am using RDB DocumentStore and I have checked index and node are created in nodes table.i tried below code,

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

can anyone share some sample code on how to create Lucene index?

推荐答案

您可能正在做的事情有两个问题.第一件事就是您正在观察的错误.由于您使用的是InitialContent,它会为type="counter"设置索引.为此,您需要在构建存储库时使用.with(new NodeCounterEditorProvider()).那应该避免您看到的错误.

There are a couple of issues with what you're likely doing. First thing would the error that you're observing. Since you're using InitialContent which provisions an index with type="counter". For that you'd need to have .with(new NodeCounterEditorProvider()) while building the repository. That should avoid the error you are seeing.

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

But, your code would likely still not work because lucene indexes are async (which you've correctly configured). Due to that asynchronous behavior, you can't query immediately after adding the node. I tried your code but had to add something like Thread.sleep(10*1000) before going for querying.

作为另一个说明,我建议您尝试IndexDefinitionBuilder来制作lucene索引结构.因此,您可以更换 树索引= root.getTree("/");

As another side-note, I'd recommend that you try out IndexDefinitionBuilder to make lucene index structure. So, you could replace 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();

使用

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

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

The latter approach, imo, is less error prone and more redabale.

这篇关于Lucene索引:查询时得到空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆