# Order documents by multiple geolocations

**URL:** https://discuss.elastic.co/t/order-documents-by-multiple-geolocations/321449
**Category:** Elasticsearch
**Created:** [December 17, 2022, 9:44am UTC](https://discuss.elastic.co/t/order-documents-by-multiple-geolocations/321449 "2022-12-17T09:44:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![merianos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merianos/32/114844_2.png) [@merianos](https://discuss.elastic.co/u/merianos)
#### Post date: [December 17, 2022, 9:44am UTC](https://discuss.elastic.co/t/order-documents-by-multiple-geolocations/321449/1 "2022-12-17T09:44:19Z")

</div>

I am new to Elasticsearch and I try to create an index for companies that come with multiple branches in the city.

Each of the branches it has it's own geolocation point.

My companies document looks like this:

```auto
{
    "company_name": "Company X",
    "branch": [
        {
            "address": {
                // ... other fields
                "location": "0.0000,1.1111"
            }
        }
    ]
}

```

The index have the following mapping:

```auto
{
  "companies": {
    "mappings": {
      "dynamic_templates": [
        {
          "ids": {
            "match": "id",
            "match_mapping_type": "long",
            "mapping": {
              "type": "long"
            }
          }
        },
        {
          "company_locations": {
            "match": "location",
            "match_mapping_type": "string",
            "mapping": {
              "type": "geo_point"
            }
          }
        }
      ],
      "properties": {
        "branch": {
          "properties": {
            "address": {
              "properties": {
                // ...
                "location": {
                  "type": "geo_point"
                },
                // ...
              }
            },
          }
        }
      }
    }
  }
}

```

Now, in the Elasticsearch I've indexed the following documents:

```auto
{
    "company_name": "Company #1",
    "branch": [
        {
            "address": {
                "location": "39.615,19.8948"
            }
        }
    ]
}

```

and

```auto
{
    "company_name": "Company #2",
    "branch": [
        {
            "address": {
                "location": "39.586,19.9028"
            }
        },
        {
            "address": {
                "location": "39.612,19.9134"
            }
        },
        {
            "address": {
                "location": "39.607,19.8946"
            }
        }
    ]
}

```

So what is my problem. If I try to run the following search query, unfortunately the company displayed first is the `Company #2` although the geodistance query has the location data of the `Company #1` :

```auto
GET companies/_search
{
  
  "fields": [
    "company_name", 
    "branch.address.location"
  ],
  "_source": false,
  "sort": [
    {
      "_geo_distance": {
        "branch.address.location": {
          "lon": 39.615,
          "lat": 19.8948
        },
        "order": "asc",
        "unit": "km"
      }
    }
  ]
}

```

Am I doing something wrong? Is there a way to sort the search results using this method?

Please keep in mind that if for example search with a geolocation that is more close to some of the geolocations of the "Comapny #2", in this case I need the `Company #2` to be first.

Finally, if the setup I have isn't correct for what I need, if there's any other way to achieve that same result with different document structure please let me know. I am still in the begin of the project and It's simple to adapt to what is more appropriate.

Thank you in advance!

---

<div class="post-metadata">

### Author: ![merianos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merianos/32/114844_2.png) [@merianos](https://discuss.elastic.co/u/merianos)
#### Post date: [December 24, 2022, 9:14am UTC](https://discuss.elastic.co/t/order-documents-by-multiple-geolocations/321449/2 "2022-12-24T09:14:28Z")

</div>

OK, It was me that I didn't pay close attention to what I did.

In the sorting I use the longitude value in the latitude field and the latitude value to the longitude field. Once I switched the values the query works as it is supposed to work 🙂

---

<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: [January 21, 2023, 9:14am UTC](https://discuss.elastic.co/t/order-documents-by-multiple-geolocations/321449/3 "2023-01-21T09:14:55Z")

</div>

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