# Possible to get matched term in TermsQuery

**URL:** <https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817>\
**Category:** Elasticsearch\
**Created:** [January 21, 2011, 11:30pm UTC](https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817 "2011-01-21T23:30:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chunlei\_Wu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chunlei_wu/32/824_2.png) [@Chunlei\_Wu](https://discuss.elastic.co/u/Chunlei_Wu)\
**Post date:** [January 21, 2011, 11:30pm UTC](https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817/1 "2011-01-21T23:30:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 23, 2011, 4:52pm UTC](https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817/2 "2011-01-23T16:52:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chunlei\_Wu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chunlei_wu/32/824_2.png) [@Chunlei\_Wu](https://discuss.elastic.co/u/Chunlei_Wu)\
**Post date:** [January 24, 2011, 11:30pm UTC](https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817/3 "2011-01-24T23:30:27Z")

</div>

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](mailto: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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:13am UTC](https://discuss.elastic.co/t/possible-to-get-matched-term-in-termsquery/3817/4 "2017-07-06T04:13:17Z")

</div>


