# Should I use field datatypes when performing hard matches?

**URL:** <https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303>\
**Category:** Elasticsearch\
**Created:** [June 3, 2018, 4:49am UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303 "2018-06-03T04:49:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeikel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yeikel/32/125434_2.png) [@yeikel](https://discuss.elastic.co/u/yeikel)\
**Post date:** [June 3, 2018, 4:49am UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/1 "2018-06-03T04:49:57Z")

</div>

I am trying to understand if [field datatypes](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) add any value when the goal is to do hard matches.

For example, let's say I'd like to index

```
{
  "name": "Sophie",
  "age": "20"
}

```

What is the difference between using the type `keyword` or `short` if my goal is to do a term query?

Is there any benefit? Perpahs, space or speed?

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 3, 2018, 6:54am UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/2 "2018-06-03T06:54:14Z")

</div>

You'll be able to do a term query on both, depends on the results you want, for example the range query behaves differently whether the field type is string or numeric.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#query-dsl-range-query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#query-dsl-range-query)

---

<div class="post-metadata">

**Author:** ![yeikel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yeikel/32/125434_2.png) [@yeikel](https://discuss.elastic.co/u/yeikel)\
**Post date:** [June 3, 2018, 7:53am UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/3 "2018-06-03T07:53:37Z")

</div>

I am only interested in hard matches at the moment (no range queries for example). My main question is if there is any benefit in indexing the data as the "right" datatype when performing hard term searches.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 3, 2018, 9:48am UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/4 "2018-06-03T09:48:08Z")

</div>

If you are doing "hard matches" (do you mean filtering?), then it shouldn't matter if it's a keyword or a short.

BUT, would you create a database table with everything `varchar(255)`?

---

<div class="post-metadata">

**Author:** ![yeikel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yeikel/32/125434_2.png) [@yeikel](https://discuss.elastic.co/u/yeikel)\
**Post date:** [June 3, 2018, 3:39pm UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/5 "2018-06-03T15:39:31Z")

</div>

> [@warkolm](#):
>
> If you are doing "hard matches" (do you mean filtering?), then it shouldn't matter if it's a keyword or a short.

I am referring to exact matches. If I understood correctly, that's the goal of term queries?

> BUT, would you create a database table with everything varchar(255)?

I understand what you mean. Could you please clarify if there is any gain when using the right data type?

EDIT : I found [this information](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/tune-for-search-speed.html#_map_identifiers_as_literal_keyword_literal) in the documentation that explain this question

---

<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 1, 2018, 3:46pm UTC](https://discuss.elastic.co/t/should-i-use-field-datatypes-when-performing-hard-matches/134303/6 "2018-07-01T15:46:36Z")

</div>

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