# Sort Results by Matching Pair of GeoPoints within a single document

**URL:** <https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199>\
**Category:** Elasticsearch\
**Created:** [January 16, 2024, 7:22pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199 "2024-01-16T19:22:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ty.mivance](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@ty.mivance](https://discuss.elastic.co/u/ty.mivance)\
**Post date:** [January 16, 2024, 7:22pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199/1 "2024-01-16T19:22:04Z")

</div>

Hi Everyone,

We have an elastic index that contains the following field mapping:

```auto
"mappings": {
            "properties": {
                "destination": {
                    "type": "geo_point"
                },
                "name": {
                    "type": "text"
                },
                "source": {
                    "type": "geo_point"
                }
            }
        },

```

Question:

We want results sorted by radius of 100 KM of both the Source and Destination of the same document. So the idea is matching results based on closest to Source and Destination. May I get some help how to write this Search Query to give me this type of result? Do you have sample queries?

Thank you very much in advance

---

<div class="post-metadata">

**Author:** ![ty.mivance](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@ty.mivance](https://discuss.elastic.co/u/ty.mivance)\
**Post date:** [January 18, 2024, 8:00pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199/3 "2024-01-18T20:00:28Z")

</div>

Just wanted to follow up if anyone has any insights how we can acheive this? The document mapping can be any structure. The key point is that the document contains a source and destination and being able to use two gepoints in the query to order results? Is this possible

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 18, 2024, 11:47pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199/4 "2024-01-18T23:47:31Z")

</div>

Hi @ty.mivance

I don't have in-depth knowledge of geo queries but what I would try in your scenario would be to use a sort script. I would write a script that was capable of calculating the distance of the points from the radius.  
I believe it would work, but on the other hand you could lose performance because we are using script in the query.

```auto
GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": """
         //script distance points
         """,
        "params": {
          "radius": 100
        }
      },
      "order": "asc"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ty.mivance](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@ty.mivance](https://discuss.elastic.co/u/ty.mivance)\
**Post date:** [January 19, 2024, 3:16pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199/5 "2024-01-19T15:16:43Z")

</div>

I appreciate the answer. I am hoping someone can help me write this using standard elastic query to acheive this goal?

---

<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:** [February 16, 2024, 3:17pm UTC](https://discuss.elastic.co/t/sort-results-by-matching-pair-of-geopoints-within-a-single-document/351199/6 "2024-02-16T15:17:04Z")

</div>

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