Hi,
I have two current ways that I've been returning which field matched
from a query:
- Going through the results and doing an in memory search through each
result and doing a string contains, - Creating a named filter for each field.
This seems like a FAQ, so I apologise if this isn't just sitting on a
page one click away from the homepage.
The first solution just seems inefficient as it's plausible (but it
may not be true) that Lucene knows what the field is when it does
match.
The second has the drawback, in that the queries get large very
quickly. It starts off okay if you have an index with two fields and
you want to know which field has matched you construct two named
filters. But the queries get quite large as you go across lots of
documents that may have have different fields to search.
Some ideas I've come across:
- Explain (as hinted at here [1])?
- SpanQuery [2] give you back a field - could you use that?
- Highlighting - but it doesn't seem the right answer (am I wrong - if
so is there a simple example)?
I've been looking through the mailing list for an answer without a lot
of joy. I'm hopeful that there's a better way to return the field
that matched. Below is example of the second solution.
curl -XGET 'http://localhost:9200/index/type/_search?pretty=true' -d '
{
"query" : {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"or" : [
{
"fquery" : {
"query" : {
"query_string" : {
"query" : "field1:100"
}
},
"_name" : "field1"
}
},
{
"fquery" : {
"query" : {
"query_string" : {
"query" : "field2:100"
}
},
"_name" : "field2"
}
}
]
}
}
}
}'
[1] http://www.gossamer-threads.com/lists/lucene/java-user/66959?do=post_view_threaded#66959
[2] http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search/spans/SpanQuery.html