# Getting the field which matched the document

**URL:** <https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566>\
**Category:** Elasticsearch\
**Created:** [April 12, 2013, 1:31pm UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566 "2013-04-12T13:31:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Hazrati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_hazrati/32/2391_2.png) [@Vikas\_Hazrati](https://discuss.elastic.co/u/Vikas_Hazrati)\
**Post date:** [April 12, 2013, 1:31pm UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/1 "2013-04-12T13:31:53Z")

</div>

Hi,

I would like to explain a quick scenario here. Say we have a document which  
has fields name and address

Document 1  
name: Vikas  
address: West Patel Nagar

Document 2  
name: Joe  
address: Vikas Puri

Now if i do a simple search like this  
curl'[http://localhost:9200/blog/post/\_search?q=Vikas&pretty=true](http://localhost:9200/blog/post/_search?q=Vikas&pretty=true)'

It pulls back both the documents. But, is there a way to know which  
document was pulled back due to what field? I have read something about  
highlighting but I am not sure if that would help here.

On the basis of the field on which the document is matched we would like to  
show it differently.

Regards | Vikas  
[www.knoldus.com](http://www.knoldus.com)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [April 14, 2013, 10:18am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/2 "2013-04-14T10:18:27Z")

</div>

There's no easy way to know this. You could use highlighting or explain,  
but those are essentially hacks.

The correct way to do this would be to issue two searches, one against the  
name field and one against the address field.

clint

On Fri, Apr 12, 2013 at 3:31 PM, Vikas Hazrati [vikas@knoldus.com](mailto:vikas@knoldus.com) wrote:

> Hi,
> 
> I would like to explain a quick scenario here. Say we have a document  
> which has fields name and address
> 
> Document 1  
> name: Vikas  
> address: West Patel Nagar
> 
> Document 2  
> name: Joe  
> address: Vikas Puri
> 
> Now if i do a simple search like this curl'  
> [http://localhost:9200/blog/post/\_search?q=Vikas&pretty=true](http://localhost:9200/blog/post/_search?q=Vikas&pretty=true)'
> 
> It pulls back both the documents. But, is there a way to know which  
> document was pulled back due to what field? I have read something about  
> highlighting but I am not sure if that would help here.
> 
> On the basis of the field on which the document is matched we would like  
> to show it differently.
> 
> Regards | Vikas  
> [www.knoldus.com](http://www.knoldus.com)
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Vikas\_Hazrati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_hazrati/32/2391_2.png) [@Vikas\_Hazrati](https://discuss.elastic.co/u/Vikas_Hazrati)\
**Post date:** [April 15, 2013, 4:46am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/3 "2013-04-15T04:46:22Z")

</div>

Hi Clint,

Thanks for your response. I would have loved to fire 2 queries had the  
scenario got just 2 fields to work against. We have tens if not scores  
against which a record could match. Hence, I was wondering if there is a  
better solution. I would check out explain as well.

Is this a problem that other people had to deal with as well?

Regards | Vikas

On Sunday, April 14, 2013 3:48:27 PM UTC+5:30, Clinton Gormley wrote:

> There's no easy way to know this. You could use highlighting or explain,  
> but those are essentially hacks.
> 
> The correct way to do this would be to issue two searches, one against the  
> name field and one against the address field.
> 
> clint
> 
> On Fri, Apr 12, 2013 at 3:31 PM, Vikas Hazrati \<[vi...@knoldus.com](mailto:vi...@knoldus.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,
> > 
> > I would like to explain a quick scenario here. Say we have a document  
> > which has fields name and address
> > 
> > Document 1  
> > name: Vikas  
> > address: West Patel Nagar
> > 
> > Document 2  
> > name: Joe  
> > address: Vikas Puri
> > 
> > Now if i do a simple search like this curl'  
> > [http://localhost:9200/blog/post/\_search?q=Vikas&pretty=true](http://localhost:9200/blog/post/_search?q=Vikas&pretty=true)'
> > 
> > It pulls back both the documents. But, is there a way to know which  
> > document was pulled back due to what field? I have read something about  
> > highlighting but I am not sure if that would help here.
> > 
> > On the basis of the field on which the document is matched we would like  
> > to show it differently.
> > 
> > Regards | Vikas  
> > [www.knoldus.com](http://www.knoldus.com)
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Iftekharul\_Haque](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Iftekharul\_Haque](https://discuss.elastic.co/u/Iftekharul_Haque)\
**Post date:** [April 15, 2013, 9:01am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/4 "2013-04-15T09:01:46Z")

</div>

> > > On the basis of the field on which the document is matched we would like  
> > > to show it differently.

This seems vague. What exactly is it you want to achieve? Might be you  
can have the query return the entire document, then resolve which  
field was matched in code.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Vikas\_Hazrati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_hazrati/32/2391_2.png) [@Vikas\_Hazrati](https://discuss.elastic.co/u/Vikas_Hazrati)\
**Post date:** [April 15, 2013, 11:13am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/5 "2013-04-15T11:13:03Z")

</div>

Right, probably it has some ambiguity. What you suggest can be done but I  
was wondering if the API would hep me in some way. So there would be 87  
fields in the document. As a wide search. the user potentially searches for  
""

Based on whether the field which matches was an "address" or if it was a  
"magician name" we would like to alter the workflow.

So the crux of the matter is to get which field matched. I hope that  
clarifies it a bit

On Monday, April 15, 2013 2:31:46 PM UTC+5:30, iftekhar wrote:

> > > > On the basis of the field on which the document is matched we would  
> > > > like  
> > > > to show it differently.
> 
> This seems vague. What exactly is it you want to achieve? Might be you  
> can have the query return the entire document, then resolve which  
> field was matched in code.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Iftekharul\_Haque](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Iftekharul\_Haque](https://discuss.elastic.co/u/Iftekharul_Haque)\
**Post date:** [April 15, 2013, 11:30am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/6 "2013-04-15T11:30:48Z")

</div>

On Mon, Apr 15, 2013 at 1:13 PM, Vikas Hazrati [vikas@knoldus.com](mailto:vikas@knoldus.com) wrote:

> Right, probably it has some ambiguity. What you suggest can be done but I  
> was wondering if the API would hep me in some way. So there would be 87  
> fields in the document. As a wide search. the user potentially searches for  
> ""
> 
> Based on whether the field which matches was an "address" or if it was a  
> "magician name" we would like to alter the workflow.
> 
> So the crux of the matter is to get which field matched. I hope that  
> clarifies it a bit

Indeed.

You can consider building multiple indices, one with the authoritative  
data you're querying on, and another being an index that specializes  
in helping you categorize tokens. So it would contain a mapping of  
token to "search string type," so "123 Baker Street" =\> "Address," and  
"Mandrake" =\> "Magician." Then do a mutli-query to your cluster, one  
for string types, and one for the actual results.

Alternatively, each document can contain all the tokens present in  
that document, mapping to the searchstring type. Just get that back on  
each query.

This gets confusing, though, if "Mandrake" matches magician, and  
matches on, say, "Mandrake Street." That's a slightly different  
problem, though, because if a document contains the same token in two  
types, ( e.g. {magician =\> "Mandrake", address =\> "123 Mandrake  
Street"}), you'll need more business logic to handle those cases.

You might be trying to offload potentially non-trivial business logic  
onto ES. You may need to look at solving the problem in another layer!

Hope it helps!

- Ifty.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:41am UTC](https://discuss.elastic.co/t/getting-the-field-which-matched-the-document/11566/7 "2017-07-06T02:41:04Z")

</div>


