# Best way to return which field matched

**URL:** <https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140>\
**Category:** Elasticsearch\
**Created:** [March 22, 2011, 1:18am UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140 "2011-03-22T01:18:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew1](https://discuss.elastic.co/u/Andrew1)\
**Post date:** [March 22, 2011, 1:18am UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/1 "2011-03-22T01:18:29Z")

</div>

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](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](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](http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search/spans/SpanQuery.html)

---

<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:** [March 22, 2011, 8:18am UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/2 "2011-03-22T08:18:13Z")

</div>

There is no way in Lucene to tell which field the query matched on. Well, the query itself is constructed against a single field, but, doing a search on \_all will not tell you which field it ended up matching on (and supporting that will make \_all useless perf wise).

One could try and hack and track all the query AST and see if it matched something, and then check that it actually ended up as one of the document hits, but again, that would make the whole execution stack much slower.

The SpanQuery javadoc is a bit misleading. It returns the field the query was executed on, which in case of span, you ned up executing the span on a single field.

-shay.banon  
On Tuesday, March 22, 2011 at 3:18 AM, Andrew wrote:

> 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](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] [Carbon60: Managed Cloud Services](http://www.gossamer-threads.com/lists/lucene/java-user/66959?do=post_view_threaded#66959)  
> [2] [SpanQuery (Lucene 3.0.3 API)](http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search/spans/SpanQuery.html)

---

<div class="post-metadata">

**Author:** ![timscott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timscott/32/3220_2.png) [@timscott](https://discuss.elastic.co/u/timscott)\
**Post date:** [March 22, 2011, 3:53pm UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/3 "2011-03-22T15:53:14Z")

</div>

I wonder if you can use "explain" somehow. Explain returns details of  
how the score was computed for each hit, including which fields  
matched. The details are cryptic and can be interpreted only by a  
very knowledgable human. I think it would be possible (albeit not  
very easy) to parse the data and boil it down to something more human  
readable or something that your code could make decisions upon. That  
said, I'm not certain exactly how explain deals with match\_all.

On Mar 22, 3:18 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> There is no way in Lucene to tell which field the query matched on. Well, the query itself is constructed against a single field, but, doing a search on \_all will not tell you which field it ended up matching on (and supporting that will make \_all useless perf wise).
> 
> One could try and hack and track all the query AST and see if it matched something, and then check that it actually ended up as one of the document hits, but again, that would make the whole execution stack much slower.
> 
> The SpanQuery javadoc is a bit misleading. It returns the field the query was executed on, which in case of span, you ned up executing the span on a single field.
> 
> -shay.banon
> 
> On Tuesday, March 22, 2011 at 3:18 AM, Andrew wrote:
> 
> > 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](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][Carbon60: Managed Cloud Services](http://www.gossamer-threads.com/lists/lucene/java-user/66959?do=post_)...  
> > [2][Index of /\_\_root/docs.lucene.apache.org/core/3\_0\_3/api/core/org/apache/lucene/search](http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search)...

---

<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:** [March 22, 2011, 5:29pm UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/4 "2011-03-22T17:29:37Z")

</div>

Right, explain does not have a specific element for field, its just text. Though, using explain will cause for slower searches (how slow?, really depends on the type of query).  
On Tuesday, March 22, 2011 at 5:53 PM, Tim Scott wrote:

> I wonder if you can use "explain" somehow. Explain returns details of  
> how the score was computed for each hit, including which fields  
> matched. The details are cryptic and can be interpreted only by a  
> very knowledgable human. I think it would be possible (albeit not  
> very easy) to parse the data and boil it down to something more human  
> readable or something that your code could make decisions upon. That  
> said, I'm not certain exactly how explain deals with match\_all.
> 
> On Mar 22, 3:18 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > There is no way in Lucene to tell which field the query matched on. Well, the query itself is constructed against a single field, but, doing a search on \_all will not tell you which field it ended up matching on (and supporting that will make \_all useless perf wise).
> > 
> > One could try and hack and track all the query AST and see if it matched something, and then check that it actually ended up as one of the document hits, but again, that would make the whole execution stack much slower.
> > 
> > The SpanQuery javadoc is a bit misleading. It returns the field the query was executed on, which in case of span, you ned up executing the span on a single field.
> > 
> > -shay.banon
> > 
> > On Tuesday, March 22, 2011 at 3:18 AM, Andrew wrote:
> > 
> > > 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](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][Carbon60: Managed Cloud Services](http://www.gossamer-threads.com/lists/lucene/java-user/66959?do=post_)...  
> > > [2][Index of /\_\_root/docs.lucene.apache.org/core/3\_0\_3/api/core/org/apache/lucene/search](http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/search)...

---

<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:09am UTC](https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/5 "2017-07-06T04:09:26Z")

</div>


