# How to optimize this agg search elasticsearch?

**URL:** https://discuss.elastic.co/t/how-to-optimize-this-agg-search-elasticsearch/311521
**Category:** Elasticsearch
**Tags:** runtime-fields
**Created:** [August 5, 2022, 11:59am UTC](https://discuss.elastic.co/t/how-to-optimize-this-agg-search-elasticsearch/311521 "2022-08-05T11:59:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)
#### Post date: [August 5, 2022, 11:59am UTC](https://discuss.elastic.co/t/how-to-optimize-this-agg-search-elasticsearch/311521/1 "2022-08-05T11:59:37Z")

</div>

i have query dsl Elasticsearch with field data 'arr\_color' is an string "red\_blu\_black" and an agg script like this

```auto
 {
        "runtime_mappings": {},
        "size": 0,
        "query": {
            "bool": {
                "must": {
                    "query_string": {
                        "query": "(arr_color:(*bl*))"
                    }
                
                }
            }
        },
        "aggs": {
            "field_occurences": {
                "terms": {
                    "size": 10000,
                    "script": {
                      "source": """def array_list = Arrays.asList(doc['arr_color.keyword'].value.splitOnToken('_'));
                      List result = []; 
                      for ( i in array_list){if(i.contains('bl')){result.add(i);}}
                      return result;"""
                    }
                },
                "aggs": {
                    "paging": {
                        "bucket_sort": {
                            "from": 0,
                            "size": 10
                        }
                    },
                    "is_all_deleted": {
                        "min": {
                            "field": "is_deleted"
                        }
                    }
                }
            
         
      }
        }
 } 

```

the result will return is all element after split field `arr_color` like [blu, black, blue,...]

but it's seem have to high caculate if the field have many color and have too much doc.  
How can i optimize this code, and can i mapping this agg to ES so next time if i want to use this agg, i just need to call

```auto
"agg":{
  "token_string":{
    "field": "arr_color"
 }
}

```

and will mapping agg can help me run query faster than post script everytime i query? Thanks

---

<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: [September 2, 2022, 12:00pm UTC](https://discuss.elastic.co/t/how-to-optimize-this-agg-search-elasticsearch/311521/2 "2022-09-02T12:00:20Z")

</div>

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