# Query by distance performance is low

**URL:** <https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128>\
**Category:** Elasticsearch\
**Created:** [March 18, 2020, 2:11pm UTC](https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128 "2020-03-18T14:11:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danbl](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@Danbl](https://discuss.elastic.co/u/Danbl)\
**Post date:** [March 18, 2020, 2:11pm UTC](https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128/1 "2020-03-18T14:11:12Z")

</div>

```auto
My search condition is using location to search objects around user.
Search range is calculated dynamically. CPU is very high.
What can i do to get better performance

{
    "query": {
        "bool": {
            "filter": [
                {
                    "match": {
                        "title": "cyber"
                    }
                },
                {
                    "script": {
                        "script": {
                            "source": "if(doc['effectiveArea'].size() != 0 || doc['location'].size() == 0) {for (int i = 0; i < doc['effectiveArea'].size(); i++) { if (params.areaList.contains(doc['effectiveArea'][i])) return true; } return false; } return (doc['location'].arcDistance(params.lat,params.lon) < doc['weight'].value *100)",
                            "lang": "painless",
                            "params": {
                                "areaList": [],
                                "lon": 120.5555,
                                "lat": 35.55555
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 18, 2020, 2:31pm UTC](https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128/2 "2020-03-18T14:31:45Z")

</div>

If you need to search objects around the user, why not use a geo point and a [geo\_distance query](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-geo-distance-query.html)?

I see that the script is using some weight, but maybe you can first filter by geo distance and then do the costly script query, which needs to be computed for every document that matches `cyber`. This would reduce the number of documents for the script query a lot.

---

<div class="post-metadata">

**Author:** ![Danbl](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@Danbl](https://discuss.elastic.co/u/Danbl)\
**Post date:** [March 19, 2020, 5:51am UTC](https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128/3 "2020-03-19T05:51:36Z")

</div>

Thank you. I will try.

---

<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:** [April 16, 2020, 5:51am UTC](https://discuss.elastic.co/t/query-by-distance-performance-is-low/224128/4 "2020-04-16T05:51:37Z")

</div>

Closed on April 16, 2020, 5:51am UTC

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