# Keyword, doc\_value and analysis

**URL:** <https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634>\
**Category:** Elasticsearch\
**Created:** [August 24, 2019, 7:00pm UTC](https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634 "2019-08-24T19:00:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@newbie1](https://discuss.elastic.co/u/newbie1)\
**Post date:** [August 24, 2019, 7:00pm UTC](https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634/1 "2019-08-24T19:00:46Z")

</div>

Hi,

I am confused about keyowrd, doc\_value and analysis.  
I found two topics (/keyword-datatype-and-analysis/66359 and help-understanding-keyword-vs-not-analyzed/10374 ) on this subjet but i am not able to understant if the answers of these topics answer to my point.

I understood that keyword are stored as doc\_values and that doc\_values are not analysed. This is why they can be used as filter or in all operations for which Fielddata are needed.

But it is possible to do a search query of type match on keyword and such queries are analyzed. So how it is possible if keywords are not analyzed ?

This is running quite wheel and i do not understand how.  
`
PUT test
{
"mappings" : {
"doc" : {
"properties" : {
"category" : {
"type" : "keyword"
}
}
}
}
}`

PUT test/doc/3  
{  
"category":"management"  
}

GET test/\_search  
{  
"query":  
{  
"match": {  
"category": "bugs"  
}  
}  
}

Thanks for any explanations on this subject.

By the way, how to format code on this forum ?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 25, 2019, 10:13am UTC](https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634/2 "2019-08-25T10:13:20Z")

</div>

Elasticsearch creates multiple datastructures out of the documents that you index. One of those datastructures is the inverted index. Both `text` and `keyword` type string fields will get their values indexed in such an inverted index. The inverted index is what allows you to query those fields. Elasticsearch will use this datastructure for queries.

Another datastructure that gets created is those doc values. Doc values are used for operations like aggregations and sorting. Doc values are available for `keyword` fields, but not for `text` fields. This is why by default you can aggregate and sort on `keyword` fields but not on `text` fields. If you want to aggregate and sort on `text` fields you would have to enable fielddata. Fielddata is very similar to doc values, with the difference being that fielddata is created in-memory when needed, while doc values are stored on disk when you index your documents.

Now, how does text analysis play in to all this? Text analysis is the processing that Elasticsearch applies to `text` fields. By default, any value that you index as a `text` field will be lowercased and broken up into its individual words. For example a string like `"New York"` becomes `new` and `york`, and these two tokens will be put in the inverted index.

Whenever you query such a text field with for example a `match` query, Elasticsearch will apply the same analysis to your query string. It is this processing that allows you to case-insensitively search for the individual words `new` or `york` and find the document that contains the string `"New York`".

Text analysis is not applied to `keyword` fields. Whenever you index a string like `"New York"` in a keyword field, what Elasticsearch will put in the inverted index is the exact original string `"New York"`. When you query that field, the query string will also not get analyzed and as a result, you will only be able to find this document if you query for the exact string `"New York"`, capital N, capital Y and one space between those words.

If I were to summarize all this I would say: `text` fields are for full text searches (case-insensitive search on individual words, powered by text analysis), while `keyword` field are for sorting and aggregating.

(To format code on this forum you can use the `</>` button)

---

<div class="post-metadata">

**Author:** ![newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@newbie1](https://discuss.elastic.co/u/newbie1)\
**Post date:** [August 25, 2019, 3:04pm UTC](https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634/3 "2019-08-25T15:04:39Z")

</div>

Hi Abdon,

A great thanks for this whole and detailed explanation, I understood that keywords are stored in the inverted index without analysis, this is why it is possible to search on it but usual analysis is not performed neither on the keyword neither on the search query.

Thanks again.

---

<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:** [September 22, 2019, 3:04pm UTC](https://discuss.elastic.co/t/keyword-doc-value-and-analysis/196634/4 "2019-09-22T15:04:44Z")

</div>

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