# Returning count on field matches (on a query across all fields)

**URL:** <https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179>\
**Category:** Elasticsearch\
**Created:** [October 30, 2013, 10:54pm UTC](https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179 "2013-10-30T22:54:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saurabh\_Daftary](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@Saurabh\_Daftary](https://discuss.elastic.co/u/Saurabh_Daftary)\
**Post date:** [October 30, 2013, 10:54pm UTC](https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179/1 "2013-10-30T22:54:16Z")

</div>

Given an example document mapping:

{  
"users": {  
"properties": {  
"firstName": {  
"type": "string"  
},  
"lastName": {  
"type": "string"  
},  
"address": {  
"type": "string"  
}  
}  
}  
}

Now if we index several documents in this mapping, and then do a query  
across all fields (like a free text query):

{  
"query": {  
"query\_string": {  
"query": "A\*"  
}  
}

Now each hit in this query would match against some field in a given  
document (say it matches first\_name on 50 occasions, last\_name on 30  
occasions, address on 60 occasions).

Is there a way in ES where I could get the above kind of result set ? Can  
you achieve something like this using facets?

I was not able to figure out a way to return the count on field matches.  
Any help would be appreciated.

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 31, 2013, 7:03pm UTC](https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179/2 "2013-10-31T19:03:14Z")

</div>

AFAIK, the only way to get this information is to parse the score  
explanation yourself to see which fields were used in the scoring. Each hit  
contains its own explanation. Of course, since you have to parse the data  
yourself, then you can only aggregate stats on the top k documents.

--  
Ivan

On Wed, Oct 30, 2013 at 3:54 PM, Saurabh Daftary [sdaftary19@gmail.com](mailto:sdaftary19@gmail.com)wrote:

> Given an example document mapping:
> 
> {  
> "users": {  
> "properties": {  
> "firstName": {  
> "type": "string"  
> },  
> "lastName": {  
> "type": "string"  
> },  
> "address": {  
> "type": "string"  
> }  
> }  
> }  
> }
> 
> Now if we index several documents in this mapping, and then do a query  
> across all fields (like a free text query):
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "A\*"  
> }  
> }
> 
> Now each hit in this query would match against some field in a given  
> document (say it matches first\_name on 50 occasions, last\_name on 30  
> occasions, address on 60 occasions).
> 
> Is there a way in ES where I could get the above kind of result set ? Can  
> you achieve something like this using facets?
> 
> I was not able to figure out a way to return the count on field matches.  
> Any help would be appreciated.
> 
> --  
> 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:** ![Saurabh\_Daftary\_2](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Saurabh\_Daftary\_2](https://discuss.elastic.co/u/Saurabh_Daftary_2)\
**Post date:** [November 1, 2013, 8:35pm UTC](https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179/3 "2013-11-01T20:35:48Z")

</div>

Thanks for your inputs Ivan.I did figure out a way.Kind of a hack  
though.You could request a facet query/filter for each field in the  
request.Something like:

facets": {  
"last\_name": {  
"query": {  
"query\_string": {  
"query": "lastName: San\*"  
}  
}  
},  
"first\_name": {  
"query": {  
"query\_string": {  
"query": "firstName: San\*"  
}  
}  
}  
}

Assuming firstName and lastName are your two fields........

On Wednesday, 30 October 2013 18:54:16 UTC-4, Saurabh Daftary wrote:

> Given an example document mapping:
> 
> {  
> "users": {  
> "properties": {  
> "firstName": {  
> "type": "string"  
> },  
> "lastName": {  
> "type": "string"  
> },  
> "address": {  
> "type": "string"  
> }  
> }  
> }  
> }
> 
> Now if we index several documents in this mapping, and then do a query  
> across all fields (like a free text query):
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "A\*"  
> }  
> }
> 
> Now each hit in this query would match against some field in a given  
> document (say it matches first\_name on 50 occasions, last\_name on 30  
> occasions, address on 60 occasions).
> 
> Is there a way in ES where I could get the above kind of result set ? Can  
> you achieve something like this using facets?
> 
> I was not able to figure out a way to return the count on field matches.  
> Any help would be appreciated.

--  
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:09am UTC](https://discuss.elastic.co/t/returning-count-on-field-matches-on-a-query-across-all-fields/14179/4 "2017-07-06T02:09:15Z")

</div>


