# Need count of terms using facets, taking space into account

**URL:** https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540
**Category:** Elasticsearch
**Created:** [May 2, 2012, 1:40pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540 "2012-05-02T13:40:01Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![anjesh](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@anjesh](https://discuss.elastic.co/u/anjesh)
#### Post date: [May 2, 2012, 1:40pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/1 "2012-05-02T13:40:01Z")

</div>

Hi,

I have a following data in ES.  
{  
"title": "title1 of project",  
"organization": "XYZ company"  
},  
{  
"title": "title2 of project",  
"organization": "XYZ company"  
},  
{  
"title": "title3 of project",  
"organization": "ABC company"  
},

I need the count of organizations as follows:  
"ABC company":1  
"XYZ company": 2

I tried using facets but facets give the count of words

curl -X POST [http://localhost:9200/testcompany/activity/\_search?pretty=true-d](http://localhost:9200/testcompany/activity/_search?pretty=true-d) '  
{ "query" : {"match\_all": {} },  
"facets" : {"organization" : {"terms" : {"field": "organization"}}}}'

gives

"facets" : {  
"organization" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 6,  
"other" : 0,  
"terms" : [ {  
"term" : "company",  
"count" : 3  
}, {  
"term" : "xyz",  
"count" : 2  
}, {  
"term" : "abc",  
"count" : 1  
} ]  
}

I have no idea if there are any options which checks for the whole phrase  
than words in the facet terms.  
Tried searching here and there but couldn't find anything.

Thanks  
Anjesh.

---

<div class="post-metadata">

### Author: ![jagdeep](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jagdeep](https://discuss.elastic.co/u/jagdeep)
#### Post date: [May 2, 2012, 4:06pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/2 "2012-05-02T16:06:54Z")

</div>

It basically depends on mapping. You have used default standard  
analyzer, insted of that you need to use keyword analyzer.

Regards  
Jagdeep

On May 2, 6:40 pm, anjesh [anjeshtulad...@gmail.com](mailto:anjeshtulad...@gmail.com) wrote:

> Hi,
> 
> I have a following data in ES.  
> {  
> "title": "title1 of project",  
> "organization": "XYZ company"},
> 
> {  
> "title": "title2 of project",  
> "organization": "XYZ company"},
> 
> {  
> "title": "title3 of project",  
> "organization": "ABC company"
> 
> },
> 
> I need the count of organizations as follows:  
> "ABC company":1  
> "XYZ company": 2
> 
> I tried using facets but facets give the count of words
> 
> curl -X POSThttp://localhost:9200/testcompany/activity/\_search?pretty=true-d'  
> { "query" : {"match\_all": {} },  
> "facets" : {"organization" : {"terms" : {"field": "organization"}}}}'
> 
> gives
> 
> "facets" : {  
> "organization" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 6,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "company",  
> "count" : 3  
> }, {  
> "term" : "xyz",  
> "count" : 2  
> }, {  
> "term" : "abc",  
> "count" : 1  
> } ]  
> }
> 
> I have no idea if there are any options which checks for the whole phrase  
> than words in the facet terms.  
> Tried searching here and there but couldn't find anything.
> 
> Thanks  
> Anjesh.

---

<div class="post-metadata">

### Author: ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)
#### Post date: [May 3, 2012, 9:31am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/3 "2012-05-03T09:31:53Z")

</div>

hi jagdeep

i am also facing same problem can u give me one mapping example for this implementation it would be very helpful to me...

thanx  
Sumit Gupta

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 3, 2012, 9:51am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/4 "2012-05-03T09:51:37Z")

</div>

Hi,

I think that setting index for 'organization' to 'not\_analyzed' should work  
like you want.

Best regards.

2012/5/3 Sumit Guptaa [sumit.gupta.ngi@gmail.com](mailto:sumit.gupta.ngi@gmail.com)

> hi jagdeep
> 
> i am also facing same problem can u give me one mapping example for this  
> implementation it would be very helpful to me...
> 
> thanx  
> Sumit Gupta
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3958655.html](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3958655.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)
#### Post date: [May 3, 2012, 10:15am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/5 "2012-05-03T10:15:58Z")

</div>

Hi Marcin

for getting the phrase count "not\_analyzed" is not working..so if u hv any idea for searching the phrase using facet query. please help me..

Thanx  
Sumit Gupta

---

<div class="post-metadata">

### Author: ![sujoysett](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sujoysett](https://discuss.elastic.co/u/sujoysett)
#### Post date: [May 3, 2012, 11:49am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/6 "2012-05-03T11:49:34Z")

</div>

First register custom analyzers, using your own configuration in the format  
like following, along with the index creation API

{  
"index": {  
"number\_of\_shards": 5,  
"number\_of\_replicas": 0,  
"analysis": {  
"analyzer": {  
"standard1": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"standard",  
"lowercase"  
]  
},  
"keyword1": {  
"type": "custom",  
"tokenizer": "keyword"  
},  
"keyword2": {  
"type": "pattern",  
"pattern": ","  
}  
}  
}  
}  
}

Next, use these analyzers to map to individual fields of the data you are  
going to post in your index. Use something like following in the update  
mapping API

{  
"mediasource": {  
"properties": {  
"mediaSourceTypeId": {  
"index": "analyzed",  
"type": "integer"  
},  
"isuName": {  
"analyzer": "keyword1",  
"type": "string"  
},  
"newsCategories": {  
"properties": {  
"category": {  
"analyzer": "keyword1",  
"type": "string"  
},  
"category\_words": {  
"analyzer": "keyword1",  
"type": "string"  
},  
"score": {  
"index": "analyzed",  
"type": "double"  
}  
}  
}  
}  
}  
}

For your data example, you have to analyze "organization" field with  
keyword analyzer. Just like I did for "isuName" field in my example.

On Thursday, May 3, 2012 3:45:58 PM UTC+5:30, Sumit Gupta wrote:

> Hi Marcin
> 
> for getting the phrase count "not\_analyzed" is not working..so if u hv any  
> idea for searching the phrase using facet query. please help me..
> 
> Thanx  
> Sumit Gupta
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3958739.html](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3958739.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)
#### Post date: [May 3, 2012, 1:12pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/7 "2012-05-03T13:12:20Z")

</div>

Hi sujoysett  
thanks for ur quick response.

after giving the mapping that u define when we search for phrase there is no hit and searching for term the result like this...

curl -XPUT '[http://localhost:9200/my\_twitter1/my\_tweet/1](http://localhost:9200/my_twitter1/my_tweet/1)' -d '{  
"user" : "hi hello how",  
"post\_date" : "2011-09-20T16:20:00",  
"message" : "abc xyz def abc xyz def"  
}'

and when we want to apply this facet query like

curl -X POST 'localhost:9200/my\_twitter1/my\_tweet/\_search?pretty=true' -d '{  
"query": {  
"term": {  
"message": "abc"  
}  
},  
"facets": {  
"message": {  
"terms": {  
"field": "message"  
}  
}  
}  
}'

and i m getting the result for all the count of abc like "abc":1,"xyz":1,"def":1 and when we search for "abc xyz" ther is no hit..

so please help me how i can search for "abc xyz" and also find the count "abc xyz" using facet query..

thanx  
Sumit Gupta

---

<div class="post-metadata">

### Author: ![jagdeep](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jagdeep](https://discuss.elastic.co/u/jagdeep)
#### Post date: [May 3, 2012, 3:12pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/8 "2012-05-03T15:12:05Z")

</div>

Sumit change the of this field to keyword as explained by Sujoy. By  
default its using standard analyzer.  
"message": {  
"analyzer": "keyword",  
"type": "string"  
},

Regards  
Jagdeep

On May 3, 6:12 pm, Sumit Guptaa [sumit.gupta....@gmail.com](mailto:sumit.gupta....@gmail.com) wrote:

> Hi sujoysett  
> thanks for ur quick response.
> 
> after giving the mapping that u define when we search for phrase there is  
> no hit and searching for term the result like this...
> 
> curl -XPUT '[http://localhost:9200/my\_twitter1/my\_tweet/1'-d](http://localhost:9200/my_twitter1/my_tweet/1'-d) '{  
> "user" : "hi hello how",  
> "post\_date" : "2011-09-20T16:20:00",  
> "message" : "abc xyz def abc xyz def"
> 
> }'
> 
> and when we want to apply this facet query like
> 
> curl -X POST 'localhost:9200/my\_twitter1/my\_tweet/\_search?pretty=true' -d '{  
> "query": {  
> "term": {  
> "message": "abc"  
> }  
> },  
> "facets": {  
> "message": {  
> "terms": {  
> "field": "message"  
> }  
> }  
> }
> 
> }'
> 
> and i m getting the result for all the count of abc like  
> "abc":1,"xyz":1,"def":1 and when we search for "abc xyz" ther is no  
> hit..
> 
> so please help me how i can search for "abc xyz" and also find the count  
> "abc xyz" using facet query..
> 
> thanx  
> Sumit Gupta
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-u](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-u)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)
#### Post date: [May 3, 2012, 4:31pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/9 "2012-05-03T16:31:41Z")

</div>

Hi jagdeep

please can u give me one example for searching like "abc xyz" using facet query that give the count "abc xyz"for also...

because i am unable to get search "abc xyz" using facet query ...

Thanx,  
Sumit Gupta

---

<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: [May 4, 2012, 3:12pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/10 "2012-05-04T15:12:59Z")

</div>

Sumit,

I do not think you will be able to achieve what you want without  
implementing a custom tokenizer. Analyzed tokenizers will tokenize on  
whitespace, and keyword analyzers take the whole term without  
stemming/splitting. You need a tokenizer that tokenizes a string into  
different permutations of the terms. Something like this tokenizer  
must already exist, but I do not think it is part of the default  
Lucene/Elasticsearch packages.

Cheers,

Ivan

On Thu, May 3, 2012 at 9:31 AM, Sumit Guptaa [sumit.gupta.ngi@gmail.com](mailto:sumit.gupta.ngi@gmail.com) wrote:

> Hi jagdeep
> 
> please can u give me one example for searching like "abc xyz" using facet  
> query that give the count "abc xyz"for also...
> 
> because i am unable to get search "abc xyz" using facet query ...
> 
> Thanx,  
> Sumit Gupta
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3959783.html](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3959783.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)
#### Post date: [May 5, 2012, 6:06am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/11 "2012-05-05T06:06:46Z")

</div>

hi Ivan

can u give me the full implementation for this so that i am able to perform the facet query on phrase....please help me...

Thanx,  
Sumit Gupta

---

<div class="post-metadata">

### Author: ![anjesh](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@anjesh](https://discuss.elastic.co/u/anjesh)
#### Post date: [May 5, 2012, 5:05pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/12 "2012-05-05T17:05:24Z")

</div>

hi

I managed to get what i am looking for using the followings. Thanks  
Jagdeep. I posted in entirety - that should work.

curl -XDELETE [http://localhost:9200/testcompany/](http://localhost:9200/testcompany/)  
curl -XPUT [http://localhost:9200/testcompany/](http://localhost:9200/testcompany/)  
curl -XPUT '[http://localhost:9200/testcompany/activity1/\_mapping](http://localhost:9200/testcompany/activity1/_mapping)' -d '{  
"activity" : {  
"properties" : {  
"organization" : {"analyzer": "keyword", "type": "string"}  
}  
}  
}'  
curl -XPUT [http://localhost:9200/testcompany/activity1/1](http://localhost:9200/testcompany/activity1/1) -d '{  
"title": "title1 of project",  
"organization": "ABC company"  
}'  
curl -XPUT [http://localhost:9200/testcompany/activity1/2](http://localhost:9200/testcompany/activity1/2) -d '{  
"title": "title2 of project",  
"organization": "XYZ company"  
}'  
curl -XPUT [http://localhost:9200/testcompany/activity1/3](http://localhost:9200/testcompany/activity1/3) -d '{  
"title": "title3 of project",  
"organization": "XYZ company"  
}'  
curl -X POST [http://localhost:9200/testcompany/activity1/\_search?pretty=true-d](http://localhost:9200/testcompany/activity1/_search?pretty=true-d)  
'{  
"query" : {"match\_all":{}},  
"facets" : {"organization" : {"terms" : {"field": "organization"}}}  
}'

gives

"facets" : {  
"organization" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 3,  
"other" : 0,  
"terms" : [ {  
"term" : "XYZ company",  
"count" : 2  
}, {  
"term" : "ABC company",  
"count" : 1  
} ]  
}

However now i can't search for ABC in organization field, as Sumit seems to  
be asking.

curl -X POST [http://localhost:9200/testcompany/activity1/\_search?pretty=true-d](http://localhost:9200/testcompany/activity1/_search?pretty=true-d)  
'{  
"query" : {"term":{"organization": "ABC"}},  
"facets" : {"organization" : {"terms" : {"field": "organization"}}}  
}'

gives 0 hits.

But

curl -X POST [http://localhost:9200/testcompany/activity1/\_search?pretty=true-d](http://localhost:9200/testcompany/activity1/_search?pretty=true-d)  
'{  
"query" : {"term":{"organization": "ABC company"}},  
"facets" : {"organization" : {"terms" : {"field": "organization"}}}  
}'

gives

"facets" : {  
"organization" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 1,  
"other" : 0,  
"terms" : [ {  
"term" : "ABC company",  
"count" : 1  
} ]  
}

I think something is still missing there and i can't seem to figure it out.  
The search is case sensitive in this case - "abc company" doesn't give  
results. I don't fully understand the internals - notably tokens,  
analyzers. I would appreciate if somebody could point to the appropriate  
posts.

Best  
Anjesh

On 5 May 2012 11:51, Sumit Guptaa [sumit.gupta.ngi@gmail.com](mailto:sumit.gupta.ngi@gmail.com) wrote:

> hi Ivan
> 
> can u give me the full implementation for this so that i am able to perform  
> the facet query on phrase....please help me...
> 
> Thanx,  
> Sumit Gupta
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3964087.html](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-using-facets-taking-space-into-account-tp3956699p3964087.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![jagdeep](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jagdeep](https://discuss.elastic.co/u/jagdeep)
#### Post date: [May 5, 2012, 6:07pm UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/13 "2012-05-05T18:07:06Z")

</div>

You either have to use pattern analyzer with case\_insensitive flag as  
explained here

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Or you need to use regex with case\_insensitive flag in your query  
string

Regards  
Jagdeep

On May 5, 10:05 pm, anjesh [anjeshtulad...@gmail.com](mailto:anjeshtulad...@gmail.com) wrote:

> hi
> 
> I managed to get what i am looking for using the followings. Thanks  
> Jagdeep. I posted in entirety - that should work.
> 
> curl -XDELETEhttp://localhost:9200/testcompany/  
> curl -XPUThttp://localhost:9200/testcompany/  
> curl -XPUT '[http://localhost:9200/testcompany/activity1/\_mapping'-d](http://localhost:9200/testcompany/activity1/_mapping'-d) '{  
> "activity" : {  
> "properties" : {  
> "organization" : {"analyzer": "keyword", "type": "string"}  
> }  
> }}'
> 
> curl -XPUThttp://localhost:9200/testcompany/activity1/1-d '{  
> "title": "title1 of project",  
> "organization": "ABC company"}'
> 
> curl -XPUThttp://localhost:9200/testcompany/activity1/2-d '{  
> "title": "title2 of project",  
> "organization": "XYZ company"}'
> 
> curl -XPUThttp://localhost:9200/testcompany/activity1/3-d '{  
> "title": "title3 of project",  
> "organization": "XYZ company"}'
> 
> curl -X POSThttp://localhost:9200/testcompany/activity1/\_search?pretty=true-d  
> '{  
> "query" : {"match\_all":{}},  
> "facets" : {"organization" : {"terms" : {"field": "organization"}}}
> 
> }'
> 
> gives
> 
> "facets" : {  
> "organization" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 3,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "XYZ company",  
> "count" : 2  
> }, {  
> "term" : "ABC company",  
> "count" : 1  
> } ]  
> }
> 
> However now i can't search for ABC in organization field, as Sumit seems to  
> be asking.
> 
> curl -X POSThttp://localhost:9200/testcompany/activity1/\_search?pretty=true-d  
> '{  
> "query" : {"term":{"organization": "ABC"}},  
> "facets" : {"organization" : {"terms" : {"field": "organization"}}}
> 
> }'
> 
> gives 0 hits.
> 
> But
> 
> curl -X POSThttp://localhost:9200/testcompany/activity1/\_search?pretty=true-d  
> '{  
> "query" : {"term":{"organization": "ABC company"}},  
> "facets" : {"organization" : {"terms" : {"field": "organization"}}}
> 
> }'
> 
> gives
> 
> "facets" : {  
> "organization" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 1,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "ABC company",  
> "count" : 1  
> } ]  
> }
> 
> I think something is still missing there and i can't seem to figure it out.  
> The search is case sensitive in this case - "abc company" doesn't give  
> results. I don't fully understand the internals - notably tokens,  
> analyzers. I would appreciate if somebody could point to the appropriate  
> posts.
> 
> Best  
> Anjesh
> 
> On 5 May 2012 11:51, Sumit Guptaa [sumit.gupta....@gmail.com](mailto:sumit.gupta....@gmail.com) wrote:
> 
> > hi Ivan
> 
> > can u give me the full implementation for this so that i am able to perform  
> > the facet query on phrase....please help me...
> 
> > Thanx,  
> > Sumit Gupta
> 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-u](http://elasticsearch-users.115913.n3.nabble.com/need-count-of-terms-u)...  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 3:29am UTC](https://discuss.elastic.co/t/need-count-of-terms-using-facets-taking-space-into-account/7540/14 "2017-07-06T03:29:59Z")

</div>


