Lucene在多值字段中找到索引?

时间:2013-03-10 13:47:06

标签: solr lucene solarium

我有一个多值的名称字段,我必须在列表中找到匹配值的索引。

DOC example:

profile_id: 1
names: [ "My name", "something", "My second name", "My nickname"]

查询:

profile_id:1 AND names:"My secon name"~

预期结果:

my doc, and the index of the matched, 2

有可能吗?

1 个答案:

答案 0 :(得分:0)

SpanTermQuery与TermQuery一样匹配文档,但它也跟踪出现在同一文档中的相同术语的位置。

Spans positionInfoForAllMatchingDocs = spanQuery.getSpans(..arguments..);
int position = -1;
while(positionInfoForAllMatchingDocs.next()){
      position = positionInfoForAllMatchingDocs.start() // Returns the start position of the current match.
      System.out.println("Found match in the document with id: " + positionInfoForAllMatchingDocs.doc() + " at position: " + position); // You obviously want to replace this sysout with something elegant.
}
  • 确保您计划检索位置信息的字段已使用Field.TermVector.WITH_POSITIONS或Field.TermVector.WITH_POSITIONS_AND_OFFSETS编制索引。