# Finding matches based on distance radius within documents without script

**URL:** <https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737>\
**Category:** Elasticsearch\
**Created:** [November 21, 2016, 2:46pm UTC](https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737 "2016-11-21T14:46:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neros](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@neros](https://discuss.elastic.co/u/neros)\
**Post date:** [November 21, 2016, 2:46pm UTC](https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737/1 "2016-11-21T14:46:39Z")

</div>

Hi, I have an elasticsearch instance running on a shared environment, which has scripting disabled.

The data format is not set in stone, however for time being, I have documents such as this:

```
{
    "id" : "903c3bd0-369d-4349-a6ae-a6c100d7a0c2",
    "firstName" : "Test",
    "lastName" : "Carer",
    "radius" : 10.0000,
    "location" : {
	"lat" : 55.9748408074944,
	"lon" : -3.16677079753718
    }
}

```

The search query is about finding all documents by Geo distance, that are located no further than the radius specified in the document from the location specified in the document. I could do this using a script:

```
"filter": {
    "and": {
        "filters": [
	    {
		    "geo_distance": {
			    "location": [
				    55.9748408074944,
				    -3.16677079753718
			    ],
			    "distance": "3000mi"
			}
		},
		{
			"script": {
				"script": "doc['location'].arcDistanceInMiles(-3.16677079753718, 55.9748408074944) < doc['radius'].value"
			}
		}
	]
}

```

}

Now my question is... how can I achieve the same result WITHOUT using script?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 2:57pm UTC](https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737/2 "2016-11-21T14:57:37Z")

</div>

Hi,

one idea would be to change your document to index the location+radius together as a [geo shape](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html) field. Then you could e.g. use a corresponding [geo shape query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html) to find all documents that intersect with a given point.

The slightly compromise in [accuracy](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#_accuracy) should not matter most of the time, and you can tune your configuration to give you the storage space / accuracy tradeoff that you need.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 3:22pm UTC](https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737/3 "2016-11-21T15:22:45Z")

</div>

I just wanted to try this out on a toy example myself, so for future reference:

```auto
PUT /index/
{
  "mappings": {
    "type": {
      "properties": {
        "location": {
          "type": "geo_shape",
          "tree": "quadtree",
          "precision": "1m"
        }
      }
    }
  }
}

PUT /index/type/1
{
  "foo" : "bar",
  "location" : {
        "type" : "circle",
        "coordinates" : [-3.16677079753718, 55.9748408074944],
        "radius" : "10km"
    }
}

GET /index/type/_search
{
    "query":{
        "bool": {
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "point",
                            "coordinates" : [-3.16677079753718, 55.8848408074944]
                        },
                        "relation": "intersects"
                    }
                }
            }
        }
    }
}

```

I didn't do any research and tuning on the geoshape mapping settings, `1m` precision might be more than needed here and also the choice of `quadtree` is arbitrary. I played around with the reference point in the query and it seems to be doing what is expected. I cannot say much in terms of memory / storage / performance requirements compared to the script solution, but if that's no option then this is maybe a good starting point.

---

<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:** [December 19, 2016, 3:23pm UTC](https://discuss.elastic.co/t/finding-matches-based-on-distance-radius-within-documents-without-script/66737/4 "2016-12-19T15:23:03Z")

</div>

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