# Terms facet not working with not\_analyzed fields and dynamic template

**URL:** <https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433>\
**Category:** Elasticsearch\
**Created:** [March 18, 2014, 5:36pm UTC](https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433 "2014-03-18T17:36:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahesh\_Kommareddi](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Mahesh\_Kommareddi](https://discuss.elastic.co/u/Mahesh_Kommareddi)\
**Post date:** [March 18, 2014, 5:36pm UTC](https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433/1 "2014-03-18T17:36:25Z")

</div>

Hi all.

I've been trying to get a terms facet working along side not\_analyzed  
fields an I'm at a bit of a loss about how to get some of this worked out.

First, I'm using a dynamic template, that allows for multi\_field and where  
the fields have an analyzed section and an not\_analyzed section.

You can see and try the mapping here:

> <https://gist.github.com/mkommar/9624564>

I put in some test data:

> <https://gist.github.com/mkommar/9624626>

Now I try searching against the analyzed field (field1) and it works as I  
would think:  
curl -XGET localhost:9200/mtest/dynamics/\_search?q=field1:Words

{"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":1,"max\_score":0.19178301,"hits":[{"\_index":"mtest","\_type":  
"dynamics","\_id":"1","\_score":0.19178301, "\_source" : {"field1" : "Words  
are nice", "field2": "We are here"}}]}}

I try searching field1 again with a different term that appears twice and  
it works as I would expect  
curl -XGET localhost:9200/mtest/dynamics/\_search?q=field1:Word

{"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":2,"max\_score":0.30685282,"hits":[{"\_index":"mtest","\_type":  
"dynamics","\_id":"3","\_score":0.30685282, "\_source" : {"field1" : "Word",  
"field2": "not a match"}},"\_index":"mtest","\_type":"dynamics","\_id":"4",  
"\_score":0.30685282, "\_source" : {"field1" : "Word", "field2": "match"}}]}}

If I search for the single word with the not\_analyzed field, I get no  
matches. Something I don't expect. No hits:  
curl -XGET localhost:9200/mtest/dynamics/\_search?q=field1.org:Word

{"took":2,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":0,"max\_score":null,"hits":[]}}

However, if I search on whole phrases, I get a hit:  
curl -XGET localhost:9200/mtest/dynamics/\_search?q=field1.org:Words%20are%  
20nice

{"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":1,"max\_score":0.02250402,"hits":[{"\_index":"mtest","\_type":  
"dynamics","\_id":"1","\_score":0.02250402, "\_source" : {"field1" : "Words  
are nice", "field2": "We are here"}}]}}

So Question 1 is: How do I get hits on single words for not\_analyzed fields?

Question 2 is about the terms facet relating to this mapping / not\_analyed  
setting.

If I do a match\_all against all the items and run a terms facet, I get the  
tokenized results I expect:

curl -XGET localhost:9200/mtest/dynamics/\_search -d '{  
"query" : {  
"match\_all": {}  
},  
"facets" : {  
"topFieldElements" : {  
"terms" : {  
"field" : "field1",  
"size" : 10  
}  
}  
}  
}'

... "facets":{"topFieldElements":{"\_type":"terms","missing":0,"total":5,  
"other":0,"terms":[{"term":"word","count":2},{"term":"words","count":1},{  
"term":"sentence","count":1},{"term":"nice","count":1}]}}}

However, running the terms facet on not\_analyzed fields results in no  
information (missing: 4):  
curl -XGET localhost:9200/mtest/dynamics/\_search -d '{  
"query" : {  
"match\_all": {}  
},  
"facets" : {  
"topFieldElements" : {  
"terms" : {  
"field" : "field1.org",  
"size" : 10  
}  
}  
}  
}'

{"took":2,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":4,"max\_score":1.0,"hits":[{"\_index":"mtest","\_type":  
"dynamics","\_id":"3","\_score":1.0, "\_source" : {"field1" : "Word", "field2": "not  
a match"}},{"\_index":"mtest","\_type":"dynamics","\_id":"2","\_score":1.0,  
"\_source" : {"field1" : "this is a sentence", "field2": "We are here"}},{  
"\_index":"mtest","\_type":"dynamics","\_id":"4","\_score":1.0, "\_source" : {  
"field1" : "Word", "field2": "match"}},{"\_index":"mtest","\_type":"dynamics",  
"\_id":"1","\_score":1.0, "\_source" : {"field1" : "Words are nice", "field2": "We  
are here"}}]},"facets":{"topFieldElements":{"\_type":"terms","missing":4,  
"total":0,"other":0,"terms":[]}}}

This appears to be the case in any amount of data (?). How can I get terms  
data in not\_analyzed fields?

Thanks!

--  
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/bf270a25-60d8-4f03-b2ed-d2dc568748b2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bf270a25-60d8-4f03-b2ed-d2dc568748b2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 18, 2014, 11:57pm UTC](https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433/2 "2014-03-18T23:57:47Z")

</div>

Just FYI, the dynamic mapping applies only to fields that you have no  
explicit mappings defined. In your case, you predefined field1 and field2  
as type string so the dynamic mapping will ignore those 2 fields and will  
not be applied to them. That is probably the cause of your query behaviors.

If you remove field1 and field2 from your initial mapping, I think you  
should get what you are expecting.

--  
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/afdbdac5-7315-42ef-a2b9-c30a488408ec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/afdbdac5-7315-42ef-a2b9-c30a488408ec%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mahesh\_Kommareddi](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Mahesh\_Kommareddi](https://discuss.elastic.co/u/Mahesh_Kommareddi)\
**Post date:** [March 19, 2014, 1:29pm UTC](https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433/3 "2014-03-19T13:29:45Z")

</div>

This seemed to be the case! Using the mapping you suggested (below) which  
made the other search and facet queries work the I wanted them to.

curl -XPUT localhost:9200/mtest  
curl -X POST localhost:9200/mtest/dynamics/\_mapping -d '  
{  
"dynamic":{  
"dynamic\_templates":[  
{  
"template\_1":{  
"match":"\*",  
"mapping":{  
"type":"multi\_field",  
"fields":{  
"{name}":{  
"type":"string",  
"index":"analyzed"  
},  
"org":{  
"type":"string",  
"index":"not\_analyzed"  
}  
}  
}  
}  
}  
]  
}  
}'

Thanks, again, Binh.

Mahesh

On Tuesday, March 18, 2014 7:57:47 PM UTC-4, Binh Ly wrote:

> Just FYI, the dynamic mapping applies only to fields that you have no  
> explicit mappings defined. In your case, you predefined field1 and field2  
> as type string so the dynamic mapping will ignore those 2 fields and will  
> not be applied to them. That is probably the cause of your query behaviors.
> 
> If you remove field1 and field2 from your initial mapping, I think you  
> should get what you are expecting.

--  
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/e489a762-21b3-4aca-8d29-ed0199a095b8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e489a762-21b3-4aca-8d29-ed0199a095b8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:42am UTC](https://discuss.elastic.co/t/terms-facet-not-working-with-not-analyzed-fields-and-dynamic-template/16433/4 "2017-07-06T01:42:11Z")

</div>


