0

I have a multivalued field of names and I have to find the index of the matching value in the list.

DOC example:

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

query:

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

Expected result:

my doc, and the index of the matched, 2

Is it possible?

M4rk
  • 2,172
  • 5
  • 36
  • 70
  • Did you try this query? Because multivalued field handled the same way. May be you need clarify question what exactly goes wrong? – Dewfy Mar 10 '13 at 16:50
  • expetcted result means what I need, instead the response is simply the doc, without knowing which of names's index is matched – M4rk Mar 10 '13 at 17:19
  • MultiFieldQueryParser - allows you specify as many names as you need. – Dewfy Mar 10 '13 at 17:26
  • Also this link(I never tried) http://lucene.472066.n3.nabble.com/search-through-all-fields-td548608.html talkd about field *all* – Dewfy Mar 10 '13 at 17:32
  • That 'index' thing is very ambiguous since the term "Index" is primarily used in Lucene for inverted-index. I believe you are looking for 'position' of matched term, right? – phanin Mar 10 '13 at 22:28

1 Answers1

0

SpanTermQuery matches documents just like TermQuery, but it also keeps track of position of the same terms that appear within the same document.

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.
}
  • Make sure that the field, for which you are planning to retrieve the positional information, was indexed with Field.TermVector.WITH_POSITIONS or Field.TermVector.WITH_POSITIONS_AND_OFFSETS.
phanin
  • 5,327
  • 5
  • 32
  • 50