# First query is slow, rest of them are fast. How to boost this?

**URL:** <https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094>\
**Category:** Elasticsearch\
**Created:** [September 21, 2016, 9:32am UTC](https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094 "2016-09-21T09:32:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)\
**Post date:** [September 21, 2016, 9:32am UTC](https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094/1 "2016-09-21T09:32:14Z")

</div>

I'm using this kind of mapping (well, it's a shortener version in order to make the question easier) on a children-parent relationship where `item` is the `parent` and `user_items` is the `children`.

```
curl -XPUT 'localhost:9200/myindex?pretty=true' -d '{
  "mappings": {
    "items": {
       "dynamic": "strict",
       "properties" : {
            "title" : { "type": "string" },
            "body" : { "type": "string" },
}},
    "user_items": {
      "dynamic": "strict",
      "_parent": {"type": "items" },
      "properties" : {
            "user_id" : { "type": "integer" },
            "source_id" : { "type": "integer" },
}}}}'

```

And the type of query I usually make:

```
curl -XGET 'localhost:9200/myindex/items/_search?pretty=true' -d '{
    "query": {
      "bool": {
         "must": [
            {
               "query_string": {
                  "fields": ["title", "body"],
                  "query": "mercado"
               }
            },
            {
               "has_child": {
                  "type": "user_items",
                  "query": {
                     "term": {
                        "user_id": 655
    }}}}]}}}'

```

On this query it has to search on the fields `title` and `body` the string `mercado` on a given `user_id`, in this case `655`

I read that the reason of being so slow the first query is that it  
gets cacheed and then the rest queries are fast because it works with  
the cached content.

I read I can make the first query faster using **[eager](https://www.elastic.co/guide/en/elasticsearch/guide/current/preload-fielddata.html)** to preload my data (using `"loading" : "eager"`, right?) but I dont know what do I've to preload. Do I've to use the earger on `title` and `body` as follows?

```
{
  "mappings": {
    "items": {
       "dynamic": "strict",
       "properties" : {
            "title" : { "type": "string" ,
                        "fielddata": {
                            "loading" : "eager"}},
            "body" : { "type": "string",
                        "fielddata": {
                            "loading" : "eager"}},
}},
    "user_items": {
      "dynamic": "strict",
      "_parent": {"type": "items" },
      "properties" : {
            "user_id" : { "type": "integer" },
            "source_id" : { "type": "integer" },
}}}}'

```

Any other recommendation fot boosting/cacheeing the first query is welcome. Thanks in advance

PS: I'm using ES 2.3.2 under a Linux machine and I've a total of 25.396.369 documents.

---

<div class="post-metadata">

**Author:** ![mikemccand](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mikemccand](https://discuss.elastic.co/u/mikemccand)\
**Post date:** [September 21, 2016, 7:03pm UTC](https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094/2 "2016-09-21T19:03:55Z")

</div>

I think that's the right place to set `eager`: [https://www.elastic.co/guide/en/elasticsearch/guide/current/preload-fielddata.html#eager-fielddata](https://www.elastic.co/guide/en/elasticsearch/guide/current/preload-fielddata.html#eager-fielddata)

However, it's much better to switch to doc values instead, which are disk-based / off-heap. This lets the OS manage the "hot" pages itself, without paying a huge penalty for whoever first needs the values for this field.

Mike McCandless

---

<div class="post-metadata">

**Author:** ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)\
**Post date:** [September 22, 2016, 7:30am UTC](https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094/3 "2016-09-22T07:30:20Z")

</div>

Thanks for the reply, @mikemccand

I've a little doubt wi the doc values.

Can I set the `doc_values` to `false` when all the fields are using the following analyzer?

```
curl -XPUT 'localhost:9200/myindex/_settings?pretty=true' -d '{
    "analysis": {
      "analyzer": {
        "default": {
          "tokenizer" : "standard",
          "filter": ["lowercase", "asciifolding"],
          "char_filter" : "html_strip"
}}}}'

```

(as the `body` or the `title` contains some html, I want to remove all the tags from it and that's why I'm using `html_strip`)

I dont know if I misunderstood the docs, but it seems that you can't use `doc_values : false` when you are using analyzed fields.

---

<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 5, 2017, 10:18pm UTC](https://discuss.elastic.co/t/first-query-is-slow-rest-of-them-are-fast-how-to-boost-this/61094/4 "2017-07-05T22:18:17Z")

</div>


