Possible to get matched term in TermsQuery


(Chunlei Wu) #1

Hi,

 If I do a Terms Query for the hits matching any of item in a

list, is it possible to know which hit matches which term? Possible
with script field?

For example, if the query is like this:
{
"terms" : {
"tags" : [ "blue", "pill" ]
"minimum_match" : 1
}
}

I want to know which hit matches "blue" and which matches "pill". Of
course, in real query, that list could be much longer.

Adding a highlight field and then parsing from the highlighted text
could be a workaround. But it does not sound like a right way, and I
am not sure if there is any other direct way.

Thanks.


(Shay Banon) #2

Try and use term filters, you can name each filter, and then get matched_filters per hit stating which filter it matched on.
On Saturday, January 22, 2011 at 1:30 AM, Chunlei Wu wrote:

Hi,

If I do a Terms Query for the hits matching any of item in a
list, is it possible to know which hit matches which term? Possible
with script field?

For example, if the query is like this:
{
"terms" : {
"tags" : [ "blue", "pill" ]
"minimum_match" : 1
}
}

I want to know which hit matches "blue" and which matches "pill". Of
course, in real query, that list could be much longer.

Adding a highlight field and then parsing from the highlighted text
could be a workaround. But it does not sound like a right way, and I
am not sure if there is any other direct way.

Thanks.


(Chunlei Wu) #3

Thanks, that works for my purpose. For reference, I converted my
previous terms query to this filtered query:

{"query" : {
"filtered" : {
"query" : {
"match_all" : { },
},
"filter" : {
"or" : [
{
"fquery" : {
"query" : {
"term" : { "tags" : "blue" }
},
"_name" : "blue"
}
},
{
"fquery" : {
"query" : {
"term" : { "tags" : "pill" }
},
"_name" : "pill"
}
}
]
}
}
}
}

On Jan 23, 8:52 am, Shay Banon shay.ba...@elasticsearch.com wrote:

Try and use term filters, you can name each filter, and then get matched_filters per hit stating which filter it matched on.

On Saturday, January 22, 2011 at 1:30 AM, Chunlei Wu wrote:

Hi,

If I do a Terms Query for the hits matching any of item in a
list, is it possible to know which hit matches which term? Possible
with script field?

For example, if the query is like this:
{
"terms" : {
"tags" : [ "blue", "pill" ]
"minimum_match" : 1
}
}

I want to know which hit matches "blue" and which matches "pill". Of
course, in real query, that list could be much longer.

Adding a highlight field and then parsing from the highlighted text
could be a workaround. But it does not sound like a right way, and I
am not sure if there is any other direct way.

Thanks.


(system) #4