# How To optimize Elastic Search Query and mapping to fetch the result quickly

**URL:** <https://discuss.elastic.co/t/how-to-optimize-elastic-search-query-and-mapping-to-fetch-the-result-quickly/338942>\
**Category:** Kibana\
**Created:** [July 21, 2023, 10:56am UTC](https://discuss.elastic.co/t/how-to-optimize-elastic-search-query-and-mapping-to-fetch-the-result-quickly/338942 "2023-07-21T10:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepak\_prem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepak_prem/32/107756_2.png) [@deepak\_prem](https://discuss.elastic.co/u/deepak_prem)\
**Post date:** [July 21, 2023, 10:56am UTC](https://discuss.elastic.co/t/how-to-optimize-elastic-search-query-and-mapping-to-fetch-the-result-quickly/338942/1 "2023-07-21T10:56:00Z")

</div>

I have the following index mapping and Elastic search query,

**Sample Index Mapping:-**

```auto
{
  "college_metric": {
    "mappings": {
      "properties": {
        "Batch__kt": {
          "type": "nested",
          "include_in_root": true,
          "properties": {
            "course": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            },
            "stream": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            }
          }
        },
        "batch_type": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            }
          }
        },
        "batch_title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            }
          }
        },
        "effective_date": {
          "type": "keyword",
          "fields": {
            "raw": {
              "type": "date",
              "format": "date_optional_time"
            }
          }
        },
        "batch_number": {
          "type": "keyword",
          "fields": {
            "raw": {
              "type": "integer"
            }
          }
        },
        "Management__kt": {
          "type": "nested",
          "include_in_root": true,
          "properties": {
            "Pricing": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            },
            "Operation": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "integer"
                }
              }
            },
            "Maintaince": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            },
            "Symetric": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            },
            "Owner": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword"
                }
              }
            }
          }
          ...
        }
      }
    }
  }
}

```

**Elastic Search Query:-**

```auto
GET college_metric/_search
{
  "from": 0,
  "size": 10,
  "_source": [
    "batch_title",
    "batch_number",
    "batch_type",
    "effective_date"
  ],
  "track_total_hits": true,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "type": "colleges"
                  }
                }
              ],
            }
          },
          "should": [
            {
              "query_string": {
                "query": "account",
                "type": "best_fields",
                "fields": [
                  "batch_title",
                  "batch_number",
                  "batch_type",
                  "*__kt.*"
                ],
                "lenient": true,
                "default_operator": "AND",
                "boost": 3
              }
            },
            {
              "query_string": {
                "query": "account",
                "type": "phrase_prefix",
                "fields": [
                  "batch_title",
                  "batch_number",
                  "batch_type",
                  "effective_date"
                  "*__kt.*"
                ],
                "lenient": true,
                "boost": 2
              }
            },
            {
              "query_string": {
                "query": "account",
                "fields": [
                  "batch_title",
                  "batch_number",
                  "batch_type",
                  "effective_date"
                  "*__kt.*"
                ],
                "lenient": true,
                "type": "best_fields"
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
  },
  "min_score": 0.000001,
  "highlight": {
    "fields": {
      "*": {
        "type": "plain"
      }
    }
  }
} 

```

Is there any way to optimize this mapping or maybe the Query so that we can fetch the result quickly, This query is taking more time to bring the outcome for some keywords like "account". and when I am searching for more results for the same keyword after increasing the size value from 10 to 40-50 then Elastic Search Kibana itself throws a 504 Gateway Time-out Error.

**Note:-** 7.10 elastic-search version I am using.

---

<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:** [August 18, 2023, 10:56am UTC](https://discuss.elastic.co/t/how-to-optimize-elastic-search-query-and-mapping-to-fetch-the-result-quickly/338942/2 "2023-08-18T10:56:55Z")

</div>

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