# Add tags or booleans to speed up queries?

**URL:** <https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947>\
**Category:** Elasticsearch\
**Created:** [April 22, 2021, 10:14am UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947 "2021-04-22T10:14:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GabrielM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabrielm/32/47827_2.png) [@GabrielM](https://discuss.elastic.co/u/GabrielM)\
**Post date:** [April 22, 2021, 10:14am UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/1 "2021-04-22T10:14:44Z")

</div>

Hello,  
I want to query documents if the length of a field is more than 1k or 10k, etc.

I'm trying to follow an Elasticsearch best practice to avoid doing queries like "if field\_length\> 1000" ([Tune for search speed | Elasticsearch Guide [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/tune-for-search-speed.html#_pre_index_data))

I see 2 solutions to do this:

- Add an array of string tags, example:

```auto
{
  "my_long_field": "xxxxxxx"
  "tags": ["is_more_than_1k", "is_more_than_10k"]
}

```

And query:

```auto
{
  "query": {
    "match": {
      "tags": "is_more_than_10k"
    }
  }
}

```

- Add booleans:

```auto
{
  "my_long_field": "xxxxxxx"
  "is_more_than_1k": true
  "is_more_than_10k": true
  "is_more_than_100k": false
}

```

And query:

```auto
{
  "query": {
    "match": {
      "is_more_than_10k": true
    }
  }
}

```

Do you know which solution is the fastest please ?

Thank you in advance, have a nice day !

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 22, 2021, 10:29am UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/2 "2021-04-22T10:29:25Z")

</div>

Another solution would be to actually store the exact size of the field in the document and then use a range query.

---

<div class="post-metadata">

**Author:** ![GabrielM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabrielm/32/47827_2.png) [@GabrielM](https://discuss.elastic.co/u/GabrielM)\
**Post date:** [April 22, 2021, 10:59am UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/3 "2021-04-22T10:59:59Z")

</div>

Hello and thank you for your response. The official documentation advises to add tags instead of doing a range query: [Tune for search speed | Elasticsearch Guide [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/tune-for-search-speed.html#_pre_index_data)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 22, 2021, 12:30pm UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/4 "2021-04-22T12:30:30Z")

</div>

Agreed. I'm "just" saying that using ranges is more flexible.  
Also depending on the datasize you might not notice a big difference between one way or the other. Specifically when using the same values for the range queries. I guess they will be cached at some point.

---

<div class="post-metadata">

**Author:** ![GabrielM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabrielm/32/47827_2.png) [@GabrielM](https://discuss.elastic.co/u/GabrielM)\
**Post date:** [April 22, 2021, 12:37pm UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/5 "2021-04-22T12:37:45Z")

</div>

I agree, it's more flexible but we are talking about +50TB dataset here and this small optimization can save my life 😃

---

<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 20, 2021, 12:38pm UTC](https://discuss.elastic.co/t/add-tags-or-booleans-to-speed-up-queries/270947/6 "2021-05-20T12:38:13Z")

</div>

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