# .keyword in query dsl

**URL:** https://discuss.elastic.co/t/keyword-in-query-dsl/174350
**Category:** Elasticsearch
**Created:** [March 28, 2019, 1:51pm UTC](https://discuss.elastic.co/t/keyword-in-query-dsl/174350 "2019-03-28T13:51:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RubenStudent](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@RubenStudent](https://discuss.elastic.co/u/RubenStudent)
#### Post date: [March 28, 2019, 1:51pm UTC](https://discuss.elastic.co/t/keyword-in-query-dsl/174350/1 "2019-03-28T13:51:12Z")

</div>

Dear ES staff

I am developing a plugin to connect to a local Elasticsearch cluster. I have to programmatically construct ES queries with to Query DSL and have problems with the keyword an text data type. I have done some tests with a small dataset. This is the mapping of the dataset:  
{  
"test3" : {  
"mappings" : {  
"\_doc" : {  
"properties" : {  
"key1" : {  
"type" : "text"  
},  
"key2" : {  
"type" : "keyword"  
},  
"key3" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
}  
}  
}  
}  
}  
}

The question is: for which fields do I have to use the '.keyword' after the field?

In my tests, I noticed that for key3 (default datatype text and keyword) I have to use it for aggregations but cannot use it for filters. All good. So I implemented it this way.

When I try it on a different dataset also having a field with type: text and keyword. I don't get any results back. When I try it with the '.keyword' behind the field, I get the correct results. Could you explain me what the reason is for this different behaviour?

---

<div class="post-metadata">

### Author: ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)
#### Post date: [April 8, 2019, 2:02am UTC](https://discuss.elastic.co/t/keyword-in-query-dsl/174350/2 "2019-04-08T02:02:30Z")

</div>

> [@RubenStudent](#):
>
> The question is: for which fields do I have to use the '.keyword' after the field?

It's really up to you. Adding a `fields` mapping will configure the [multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html) feature which allows indexing the same value from a field with different mappings. Please, review the referred documentation for more information.

> [@RubenStudent](#):
>
> In my tests, I noticed that for key3 (default datatype text and keyword) I have to use it for aggregations but cannot use it for filters. All good. So I implemented it this way.

I am not sure I follow this completely. You should be able to use filters both in `text` and `keyword` datatypes.

> [@RubenStudent](#):
>
> When I try it on a different dataset also having a field with type: text and keyword. I don't get any results back. When I try it with the '.keyword' behind the field, I get the correct results. Could you explain me what the reason is for this different behaviour?

I don't know the query that you are using, so it's difficult to provide more information. But it's important to understand that querying a field of type `text` is completely different from querying a field of type `keyword`. The first will be full-text search while the latter will be exact match. Review documentations for [text](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/text.html) and [keyword](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/keyword.html) for more information on the differences.

---

<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: [May 6, 2019, 2:02am UTC](https://discuss.elastic.co/t/keyword-in-query-dsl/174350/3 "2019-05-06T02:02:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
