Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch first query is slow, rest of them are fast

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 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.

like image 595
Avión Avatar asked Mar 11 '23 02:03

Avión


1 Answers

Just fixed the same issue. Field Data preloading was the key. Index warming was deprecated and doc_values is on by default. My application searched a couple fields on a large index (100G+) and was slow I had to rebuild the index with loading=eager for all of the fields that I searched on. This preloads it and causes a pretty long startup but after that search went from initial of 10s (<400ms afterwards) to <900ms initial search (<400ms afterwards). Make the mapping and reimport the data

PUT localhost:9200/newindex/
{
  "mappings": {
    "items": {
      "properties": {
        "title": {
          "type": "string",
          "fielddata": {
            "loading" : "eager"
          }
        },
        "body": {
          "type": "string",
          "fielddata": {
            "loading" : "eager"
          }
        }
      } 
    }
  }
}   
like image 75
JJJ Avatar answered Mar 20 '23 14:03

JJJ