# Problem: Facets tokenize tags with spaces. Is there a solution?

**URL:** https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351
**Category:** Elasticsearch
**Created:** [January 11, 2012, 6:01pm UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351 "2012-01-11T18:01:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Royce](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royce/32/3016_2.png) [@Royce](https://discuss.elastic.co/u/Royce)
#### Post date: [January 11, 2012, 6:01pm UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/1 "2012-01-11T18:01:33Z")

</div>

Hi,

I'm using facets to do filters on search results. One tag, for  
example, is a city name "Kansas City." The facet interprets "Kansas  
City" as two separate counts, "Kansas" and "City".

How can I configure facets to recognize "Kansas City" as one tag?

Take care,

Royce

---

<div class="post-metadata">

### Author: ![Royce](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royce/32/3016_2.png) [@Royce](https://discuss.elastic.co/u/Royce)
#### Post date: [January 11, 2012, 6:06pm UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/2 "2012-01-11T18:06:32Z")

</div>

Similar topics:

[http://groups.google.com/group/elasticsearch/browse\_thread/thread/ec24d56db34275b1/ccdaa025a3bb2481?lnk=gst&q=facet+tokenize#ccdaa025a3bb2481](http://groups.google.com/group/elasticsearch/browse_thread/thread/ec24d56db34275b1/ccdaa025a3bb2481?lnk=gst&q=facet+tokenize#ccdaa025a3bb2481)

On Jan 11, 12:01 pm, Royce [royce.hay...@gmail.com](mailto:royce.hay...@gmail.com) wrote:

> Hi,
> 
> I'm using facets to do filters on search results. One tag, for  
> example, is a city name "Kansas City." The facet interprets "Kansas  
> City" as two separate counts, "Kansas" and "City".
> 
> How can I configure facets to recognize "Kansas City" as one tag?
> 
> Take care,
> 
> Royce

---

<div class="post-metadata">

### Author: ![Royce](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royce/32/3016_2.png) [@Royce](https://discuss.elastic.co/u/Royce)
#### Post date: [January 11, 2012, 6:10pm UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/3 "2012-01-11T18:10:12Z")

</div>

Let me be clear, that my tag isn't "Kansas City". Instead, it's  
"location" and one of the cities that shows up is "Kansas City."

On Jan 11, 12:01 pm, Royce [royce.hay...@gmail.com](mailto:royce.hay...@gmail.com) wrote:

> Hi,
> 
> I'm using facets to do filters on search results. One tag, for  
> example, is a city name "Kansas City." The facet interprets "Kansas  
> City" as two separate counts, "Kansas" and "City".
> 
> How can I configure facets to recognize "Kansas City" as one tag?
> 
> Take care,
> 
> Royce

---

<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: [January 12, 2012, 1:54am UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/4 "2012-01-12T01:54:24Z")

</div>

The topic you referenced has the answer. If the field you are faceting  
on is a string, then it needs to either be not analyzed or analyzed  
with something like the KeywordAnalyzer which terms the term as a  
single token. Can you gist the mapping you are using? In your example,  
it appears that location is being analyzed and is indexed as two  
tokens "Kansas" and "City", which is the default behavior. The facet  
will treat the two tokens as unique terms.

> **[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.

--  
Ivan

> **[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.

On Wed, Jan 11, 2012 at 10:01 AM, Royce [royce.haynes@gmail.com](mailto:royce.haynes@gmail.com) wrote:

> Hi,
> 
> I'm using facets to do filters on search results. One tag, for  
> example, is a city name "Kansas City." The facet interprets "Kansas  
> City" as two separate counts, "Kansas" and "City".
> 
> How can I configure facets to recognize "Kansas City" as one tag?
> 
> Take care,
> 
> Royce

---

<div class="post-metadata">

### Author: ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://discuss.elastic.co/u/Suraj)
#### Post date: [August 20, 2012, 10:46am UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/5 "2012-08-20T10:46:13Z")

</div>

Hi,

I have the same problem as with Royce. I have the following mappings:

curl -XPOST "[http://localhost:9200/pictures](http://localhost:9200/pictures)" -d '  
{  
"mappings" : {  
"pictures" : {  
"properties" : {  
"id": { "type": "string" },  
"description": {"type": "string", "index": "not\_analyzed"},  
"featured": { "type": "boolean" },  
"categories": { "type": "string", "index": "not\_analyzed" },  
"tags": { "type": "string", "index": "not\_analyzed",  
"analyzer": "keyword" },  
"created\_at": { "type": "double" }  
}  
}  
}  
}'

And My Data is:

curl -X POST "[http://localhost:9200/pictures/picture](http://localhost:9200/pictures/picture)" -d '{  
"picture": {  
"id": "4defe0ecf02a8724b8000047",  
"title": "Victoria Secret PhotoShoot",  
"description": "From France and Italy",  
"featured": true,  
"categories": [  
"Fashion",  
"Girls",  
],  
"tags": [  
"girl",  
"photoshoot",  
"supermodel",  
"Victoria Secret"  
],  
"created\_at": 1405784416.04672  
}  
}'

And My Query is:  
curl -X POST "[http://localhost:9200/pictures/\_search?pretty=true](http://localhost:9200/pictures/_search?pretty=true)" -d '  
{  
"query": {  
"text": {  
"tags": {  
"query": "Victoria Secret"  
}  
}  
},  
"facets": {  
"tags": {  
"terms": {  
"field": "tags"  
}  
}  
}  
}'

The Output result is:  
{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" :   
},  
"facets" : {  
"tags" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 0,  
"other" : 0,  
"terms" :   
}  
}  
}

So I got total 0 in facets and total: 0 in hits

Any Idea Why its not working? I know that when remove the keyword analyzer  
from tags and make it "not\_analyzed" then I get result but there is still a  
problem of case insensitive.

Cheers!  
Suraj

On Thursday, January 12, 2012 7:39:24 AM UTC+5:45, Ivan Brusic wrote:

> The topic you referenced has the answer. If the field you are faceting  
> on is a string, then it needs to either be not analyzed or analyzed  
> with something like the KeywordAnalyzer which terms the term as a  
> single token. Can you gist the mapping you are using? In your example,  
> it appears that location is being analyzed and is indexed as two  
> tokens "Kansas" and "City", which is the default behavior. The facet  
> will treat the two tokens as unique terms.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/index-modules/analysis/keyword-analyzer.html)
> 
> --  
> Ivan
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/)
> 
> On Wed, Jan 11, 2012 at 10:01 AM, Royce \<[royce....@gmail.com](mailto:royce....@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Hi,
> > 
> > I'm using facets to do filters on search results. One tag, for  
> > example, is a city name "Kansas City." The facet interprets "Kansas  
> > City" as two separate counts, "Kansas" and "City".
> > 
> > How can I configure facets to recognize "Kansas City" as one tag?
> > 
> > Take care,
> > 
> > Royce

--

---

<div class="post-metadata">

### Author: ![mohammad](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@mohammad](https://discuss.elastic.co/u/mohammad)
#### Post date: [February 4, 2014, 5:56pm UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/6 "2014-02-04T17:56:24Z")

</div>

Hello everyone,  
well i am new to elastic search and i am facing some similar difficulties as mentioned above. i tried implementing some of the suggested solution but to no avail.  
I am posting part of codes and will be very grateful if somebody could help me out. Thanks in advance.

the codes are written in java:  
// i have the following in the mapping part  
CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(index)  
.setSettings(ImmutableSettings.settingsBuilder().loadFromSource(configIndex));

```
	builder.addMapping("StatTest", "{\n" + 
	"	\"StatTest\" : {\n" + 
	" \"_all\" : { \n" + 
	" \"analyzer\":\"francais\" \n" + 
	" },\n" + 
	" \"properties\" : {\n" + 
	" \"idUser\" : {\"type\" : \"string\", \"analyzer\":\"francais\"},\n" +
	" \"loginOfUser\" : {\"type\" : \"string\", \"analyzer\":\"francais\"},\n" + 
	" \"nameOfUser\" : {\"type\" : \"string\", \"analyzer\":\"francais\"},\n" + 
	" }\n" + 
	"	}\n" + 
	"}");	

```

//the sample data stored are the following  
{idUser: "0121", loginOfUser: "login0121", nameOfUser :"mona lisa"},  
{idUser: "0122", loginOfUser: "login0122", nameOfUser :"James Dean"},

//i am trying to get facets based upon name of user  
//TermsFacetBuilder fb = FacetBuilders.termsFacet("idOfUser").field("loginOfUser");  
TermsFacetBuilder fb = FacetBuilders.termsFacet("idOfUser").field("nameOfUser");   
SearchRequestBuilder srb1 = client.prepareSearch().setIndices(index).addFacet(fb);   
AndFilterBuilder myFilters = FilterBuilders.andFilter();   
myFilters.add(FilterBuilders.termFilter("year", "2014"));  
FilterBuilder fbBuilder = FilterBuilders.andFilter(myFilters);  
FilteredQueryBuilder q = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),fbBuilder);  
SearchResponse sr = srb1.setQuery(q).execute().actionGet();   
TermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("idOfUser");   
for (TermsFacet.Entry entry : f) {  
String type = entry.getTerm().toString();  
[//System.out.println](https://System.out.println)("....enter type : "+type);  
[//System.out.println](https://System.out.println)("....enter entry.getCount() : "+entry.getCount());

```
		}

```

//problems faced whenever i am trying to do a facet based on login of user,  
everything works well  
the variable type returns :  
login0121  
login0122

however when i try to do a facet based on nameOfUser , the following is returned:  
mona  
lisa  
James  
Dean

/////  
i want to retriev the usernames as one token only,  
am i missing some codes somewhere  
i will be very thankful if any one can help me on this  
thanks in advance

---

<div class="post-metadata">

### Author: ![jsbonline2006](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@jsbonline2006](https://discuss.elastic.co/u/jsbonline2006)
#### Post date: [February 5, 2014, 3:34am UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/7 "2014-02-05T03:34:17Z")

</div>

Hi All,

Here is the solution for all of you:

1. You have to define your facet as multi\_field value as follows

"mappings": {  
"data": {  
"properties": {  
"name": {  
"type": "multi\_field",  
"fields": {  
"name": {  
"type": "string",  
"index": "analyzed"  
},  
"untouched": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
},

Here my "name" field is multi\_field value. I can use "name" for searching  
purpose and "name.untouched" for faceting purpose.

I was facing same issue earlier as you guys mentioned in above thread. and  
then above mapping and usage helped me in resolving this issue

Regards,  
Jayesh Bhoyar  
[http://www.linkedin.com/in/jayeshbhoyar](http://www.linkedin.com/in/jayeshbhoyar)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/20772e3f-2244-42e8-bf19-ac37c0efbaab%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/20772e3f-2244-42e8-bf19-ac37c0efbaab%40googlegroups.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, 1:52am UTC](https://discuss.elastic.co/t/problem-facets-tokenize-tags-with-spaces-is-there-a-solution/6351/8 "2017-07-06T01:52:34Z")

</div>


