# Code optimization

**URL:** <https://discuss.elastic.co/t/code-optimization/229713>\
**Category:** Elasticsearch\
**Created:** [April 24, 2020, 7:45pm UTC](https://discuss.elastic.co/t/code-optimization/229713 "2020-04-24T19:45:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikolaj\_88](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikolaj_88/32/67080_2.png) [@nikolaj\_88](https://discuss.elastic.co/u/nikolaj_88)\
**Post date:** [April 24, 2020, 7:45pm UTC](https://discuss.elastic.co/t/code-optimization/229713/1 "2020-04-24T19:45:22Z")

</div>

Hello,

I'm implementing the new search engine on my website and I have doubts about my code.  
The following code (working version) it's quite bulky and can be bigger depending on search term and I think that it could be optimized. My example is based on search terms: "nabla chair". As you can see for each term must clause contain a block of should clause code with one of the terms. If I search using terms: "nabla long chair" one more should block of code will be added and so on.

```
GET proitems/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "path": "name",
                                    "query": {
                                        "bool": {
                                            "minimum_should_match": 1,
                                            "filter": {
                                                "terms": {
                                                    "name.lng": [
                                                        "en",
                                                        "de"
                                                    ]
                                                }
                                            },
                                            "should": [
                                                {
                                                    "term": {
                                                        "name.text": "nabla"
                                                    }
                                                },
                                                {
                                                    "term": {
                                                        "name.text.folded": "nabla"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            },
                            {
                                "bool": {
                                    "minimum_should_match": 1,
                                    "should": [
                                        {
                                            "term": {
                                                "man_name": "nabla"
                                            }
                                        },
                                        {
                                            "term": {
                                                "man_name.folded": "nabla"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "path": "name",
                                    "query": {
                                        "bool": {
                                            "minimum_should_match": 1,
                                            "filter": {
                                                "terms": {
                                                    "name.lng": [
                                                        "en",
                                                        "de"
                                                    ]
                                                }
                                            },
                                            "should": [
                                                {
                                                    "term": {
                                                        "name.text": "chair"
                                                    }
                                                },
                                                {
                                                    "term": {
                                                        "name.text.folded": "chair"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            },
                            {
                                "bool": {
                                    "minimum_should_match": 1,
                                    "should": [
                                        {
                                            "term": {
                                                "man_name": "chair"
                                            }
                                        },
                                        {
                                            "term": {
                                                "man_name.folded": "chair"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

Any suggestion would be welcome in order to optimize the code.  
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:** [May 22, 2020, 7:46pm UTC](https://discuss.elastic.co/t/code-optimization/229713/2 "2020-05-22T19:46:25Z")

</div>

Closed on May 22, 2020, 7:46pm UTC

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