# How to optimize my ElasticSearch Query

**URL:** <https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531>\
**Category:** Elasticsearch\
**Created:** [March 28, 2025, 2:02pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531 "2025-03-28T14:02:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [March 28, 2025, 2:02pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/1 "2025-03-28T14:02:10Z")

</div>

Hello, I use a really strong request for my project. I tried to follow Elasticsearch tunes for search speed but it looks like its slower than before. Here you can find my request before I did the optimizations :

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must": [
                          {
                            "bool": {
                              "must": [
                                {
                                  "term": {
                                    "countryId": {
                                      "value": "fr"
                                    }
                                  }
                                },
                                {
                                  "term": {
                                    "siteId": {
                                      "value": "vz"
                                    }
                                  }
                                },
                                {
                                  "term": {
                                    "periodicityId": {
                                      "value": 3
                                    }
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "budgetMin": {
                                            "lte": 479000
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "budgetMin"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "budgetMax": {
                                            "gte": 479000
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "budgetMax"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "roomMin": {
                                            "lte": 4
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "roomMin"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "roomMax": {
                                            "gte": 4
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "roomMax"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "bedRoomMin": {
                                            "lte": 3
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "bedRoomMin"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "bedRoomMax": {
                                            "gte": 3
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "bedRoomMax"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "surfaceMin": {
                                            "lte": 88
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "surfaceMin"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "range": {
                                          "surfaceMax": {
                                            "gte": 88
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "must_not": [
                                            {
                                              "exists": {
                                                "field": "surfaceMax"
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "term": {
                                    "propertyTypes": {
                                      "value": 6
                                    }
                                  }
                                },
                                {
                                  "term": {
                                    "indiv": {
                                      "value": false
                                    }
                                  }
                                },
                                {
                                  "term": {
                                    "withVisual3DLink": {
                                      "value": false
                                    }
                                  }
                                }
                              ],
                              "must_not": [
                                {
                                  "term": {
                                    "advertType": {
                                      "value": "rentals"
                                    }
                                  }
                                },
                                {
                                  "exists": {
                                    "field": "landSurfaceMin"
                                  }
                                },
                                {
                                  "exists": {
                                    "field": "landSurfaceMax"
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "bool": {
                              "should": [
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "bool": {
                                          "should": [
                                            {
                                              "term": {
                                                "cityIds": {
                                                  "value": 19270
                                                }
                                              }
                                            },
                                            {
                                              "term": {
                                                "departmentIds": {
                                                  "value": 4325
                                                }
                                              }
                                            },
                                            {
                                              "term": {
                                                "regionIds": {
                                                  "value": 979
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      },
                                      {
                                        "match": {
                                          "searchMode": {
                                            "query": "default"
                                          }
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "geo_shape": {
                                          "polygonalShapes": {
                                            "shape": {
                                              "type": "point",
                                              "coordinates": [
                                                -1.7193224,
                                                48.0779145
                                              ]
                                            }
                                          }
                                        }
                                      },
                                      {
                                        "bool": {
                                          "should": [
                                            {
                                              "match": {
                                                "searchMode": {
                                                  "query": "polygonal"
                                                }
                                              }
                                            },
                                            {
                                              "match": {
                                                "searchMode": {
                                                  "query": "isochrone"
                                                }
                                              }
                                            }
                                          ]
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "geo_shape": {
                                          "radius": {
                                            "shape": {
                                              "type": "point",
                                              "coordinates": [
                                                -1.7193224,
                                                48.0779145
                                              ]
                                            }
                                          }
                                        }
                                      },
                                      {
                                        "match": {
                                          "searchMode": {
                                            "query": "radius"
                                          }
                                        }
                                      }
                                    ]
                                  }
                                },
                                {
                                  "bool": {
                                    "must": [
                                      {
                                        "geo_shape": {
                                          "polygonalShapes": {
                                            "shape": {
                                              "type": "point",
                                              "coordinates": [
                                                -1.7193224,
                                                48.0779145
                                              ]
                                            },
                                            "relation": "contains"
                                          }
                                        }
                                      },
                                      {
                                        "match": {
                                          "searchMode": {
                                            "query": "aspirational_search"
                                          }
                                        }
                                      }
                                    ],
                                    "must_not": [
                                      {
                                        "geo_shape": {
                                          "polygonalHoles": {
                                            "shape": {
                                              "type": "point",
                                              "coordinates": [
                                                -1.7193224,
                                                48.0779145
                                              ]
                                            },
                                            "relation": "contains"
                                          }
                                        }
                                      }
                                    ]
                                  }
                                }
                              ]
                            }
                          },
                          {
                            "term": {
                              "reducePrice": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "withVideoLink": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "isNotOverlooked": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "isNorthFacing": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "isWestFacing": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "isEastFacing": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "isLastFloor": {
                                "value": false
                              }
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "term": {
                              "floorPreference": {
                                "value": "no_ground_floor"
                              }
                            }
                          },
                          {
                            "term": {
                              "floorPreference": {
                                "value": "none"
                              }
                            }
                          },
                          {
                            "bool": {
                              "must_not": [
                                {
                                  "exists": {
                                    "field": "floorPreference"
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "isFurnished": {
                                "value": false
                              }
                            }
                          }
                        ],
                        "must_not": [
                          {
                            "term": {
                              "unfurnished": {
                                "value": true
                              }
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "unfurnished": {
                                "value": true
                              }
                            }
                          }
                        ],
                        "must_not": [
                          {
                            "term": {
                              "isFurnished": {
                                "value": false
                              }
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "isFurnished": {
                                "value": false
                              }
                            }
                          },
                          {
                            "term": {
                              "unfurnished": {
                                "value": true
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "term": {
                  "hasPool": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasCellar": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasGarage": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasHandicapAccess": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasTerrace": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasFireplace": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasClosets": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasParquet": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasBuildingManager": {
                    "value": false
                  }
                }
              },
              {
                "term": {
                  "hasAlarm": {
                    "value": false
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10000
}

```

Sorry for the long request.

Do you have any idea what can I do to have faster request and to limit CPU/Disk usage ?

Thanks for the time you'll take for me

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 28, 2025, 2:17pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/2 "2025-03-28T14:17:33Z")

</div>

Which version of Elasticsearch are you using?

What is the size and specification of your cluster?

What is the size of the index/indices you are querying? How many primary and replica shards?

What latency are you experiencing?

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [March 28, 2025, 2:29pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/3 "2025-03-28T14:29:32Z")

</div>

Thanks for your quick answer.

I'm using the the version 7.17.8.

I got a cluster of 6 nodes, with 32gb of heap size for each.

I got 1.3M of docs and 5.7Gb. I got 1 primary shards and 2 replicas by node.

The problem is when I'm doing multiple request to the index in parallel I got "data too large" errors. I'm trying to optimize my mapper and my requests.  
But with the tuning my request are as twice slower than before.

Another thing, I'm doing researches with geoshape. For a GeoPoint I'm looking which docs contains it. Maybe it requires a lot of CPU.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 28, 2025, 2:33pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/4 "2025-03-28T14:33:28Z")

</div>

> [@simon\_Guille](#):
>
> I got 1.3M of docs and 5.7Gb. I got 1 primary shards and 2 replicas by node.

Does this mean you have 6 primary shards?

If that is the case I would recommend [shrinking this index down to a single primary shard](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html) and set the number of replicas to 5 so all nodes hold a copy of this index. As your index is quite small it should fit in the page cache.

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [March 28, 2025, 2:37pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/5 "2025-03-28T14:37:37Z")

</div>

I'm not an expert in Elasticsearch.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/d/8d6f0fd9acb7a023563ee1bf54448053eafbd251.png)

I'm right when i'm saying that i got a primary shards by nodes ?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 28, 2025, 2:43pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/6 "2025-03-28T14:43:35Z")

</div>

Looks like you have 5 primary shards and 2 replicas set up. Try with 1 primary shard and 5 replicas instead.

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [March 28, 2025, 2:51pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/7 "2025-03-28T14:51:15Z")

</div>

Thank for your recommandation. Do you know where i can find the documentation to find out more on shards and replicas configuration ?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 28, 2025, 3:15pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/8 "2025-03-28T15:15:46Z")

</div>

Look in the [official documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html).

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [March 28, 2025, 3:23pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/9 "2025-03-28T15:23:47Z")

</div>

Thanks for your time. Have a great day !

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [March 28, 2025, 6:23pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/10 "2025-03-28T18:23:37Z")

</div>

Just to check, your index name had today’s date in it?

Is there a new index every day/wee/whatever ?

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [April 1, 2025, 7:40am UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/11 "2025-04-01T07:40:42Z")

</div>

Hello, We create a new index every months. Also, this index is updated continuously. I tried to reduce the shards number but I still get errors like "data too large".

FYI, the "data too large" exception rise when we perform parallel requests during a batch.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [April 1, 2025, 10:50am UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/12 "2025-04-01T10:50:16Z")

</div>

Did you successfully change to 1 primary shard and 5 replicas? Can you share equivalent of screenshot above ?

I struggled to parse your query (as a human just reading it). Is that query generated by a person, or some application ?

You can profile the query too, see where the time is being spent.

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [April 1, 2025, 12:08pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/13 "2025-04-01T12:08:43Z")

</div>

I changed to 1 primary and 2 replicas to test.

I use Nest in C# to generate this query.

here is a screenshot of all my indexes :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/8/c822c4298e14bb8b13c3a91c7568fe970ee6e726.png)

at the left you can see the modified index we're talking about

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [April 1, 2025, 2:21pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/14 "2025-04-01T14:21:17Z")

</div>

What does the profiler tell you?

> **[Profile API | Elasticsearch Guide \[8.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html)**

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [April 1, 2025, 2:30pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/15 "2025-04-01T14:30:59Z")

</div>

I already tried to profile my query but the query is so big my Multi Elasticsearch Heads pluggin crashes when i'm trying to look at it. I can't copy and paste it.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [April 1, 2025, 2:56pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/16 "2025-04-01T14:56:13Z")

</div>

Ok, the query generated by NEST/C# is not nice.

Did you try devtools in Kibana ?

Maybe have a think what the query is trying to find, and rebuild that manually.

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [April 1, 2025, 3:19pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/17 "2025-04-01T15:19:52Z")

</div>

Currently we don't have Kibana. I'll install it and look forward with it

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [April 3, 2025, 8:14pm UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/18 "2025-04-03T20:14:41Z")

</div>

Curious if you made any progress ?

---

<div class="post-metadata">

**Author:** ![simon\_Guille](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_guille/32/142316_2.png) [@simon\_Guille](https://discuss.elastic.co/u/simon_Guille)\
**Post date:** [April 4, 2025, 8:26am UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/19 "2025-04-04T08:26:24Z")

</div>

I did the 1 primary and 5 replicas config on my cluster. I had less errors but still a lot (1,3k). During my batch I send 10 requests in parallel, maybe that's too many. How can I quantify the maximum number of parallel requests my cluster can handle ?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 4, 2025, 8:34am UTC](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531/20 "2025-04-04T08:34:51Z")

</div>

> [@simon\_Guille](#):
>
> I did the 1 primary and 5 replicas config on my cluster.

That recommendation assumed you were querying a single index. If you are querying multiple indices you may wan to reduce the number of replicas to e.g. 2.

> [@simon\_Guille](#):
>
> How can I quantify the maximum number of parallel requests my cluster can handle ?

Start testing with a single concurrent query and then gradually increase the number of concurrent queries until you start seeing errors or get excessive latencies.

[Next page](https://discuss.elastic.co/t/how-to-optimize-my-elasticsearch-query/376531.md?page=2)
