# Distinct records with geo\_distance sort on aggregation ES

**URL:** <https://discuss.elastic.co/t/distinct-records-with-geo-distance-sort-on-aggregation-es/311606>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 7, 2022, 4:31pm UTC](https://discuss.elastic.co/t/distinct-records-with-geo-distance-sort-on-aggregation-es/311606 "2022-08-07T16:31:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZulQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zulq/32/101104_2.png) [@ZulQ](https://discuss.elastic.co/u/ZulQ)\
**Post date:** [August 7, 2022, 4:31pm UTC](https://discuss.elastic.co/t/distinct-records-with-geo-distance-sort-on-aggregation-es/311606/1 "2022-08-07T16:31:49Z")

</div>

I'm working on nearby API using elasticsearch.

I'm trying to run 4 actions in ES query

- match condition (here running a script to get records within radius)
- get distinct records based on company's Key (want to get one record from a company)
- sort records based on geo\_distance
- add the field as **Distance** to get the distance between user and location

```auto
const query = {
  query: {
    bool: {
      must: [
        {
          match: {
            shopStatus: "active",
          },
        },
        {
          script: {
            script: {
              params: {
                lat: parseFloat(req.lat),
                lon: parseFloat(req.lon),
              },
              source:
                "doc['location'].arcDistance(params.lat, params.lon) / 1000 <= doc['searchRadius'].value",
              lang: "painless",
            },
          },
        },
      ],
    },
  },
  aggs: {
    duplicateCount: {
      terms: {
        field: "companyKey",
        size: 10000,
      },
      aggs: {
        duplicateDocuments: {
          top_hits: {
            sort: [
              {
                _geo_distance: {
                  location: {
                    lat: parseFloat(req.lat),
                    lon: parseFloat(req.lon),
                  },
                  order: "asc",
                  unit: "km",
                  mode: "min",
                  distance_type: "arc",
                  ignore_unmapped: true,
                },
              },
            ],
            script_fields: {
              distance: {
                script: {
                  params: {
                    lat: parseFloat(req.lat),
                    lon: parseFloat(req.lon),
                  },
                  inline: `doc['location'].arcDistance(params.lat, params.lon)/1000`,
                },
              },
            },

            stored_fields: ["_source"],
            size: 1,
          },
        },
      },
    },
  },
};

```

Here's the out put:

```auto
data: [
  {
    companyKey: "1234",
    companyName: "Floward",
    branchKey: "3425234",
    branch: "Mursilat",
    distance: 1.810064121687324,
  },
  {
    companyKey: "0978",
    companyName: "Dkhoon",
    branchKey: "352345",
    branch: "Wahah blue branch ",
    distance: 0.08931851500047634,
  },
  {
    companyKey: "567675",
    companyName: "Abdulaziz test",
    branchKey: "53425",
    branch: "Jj",
    distance: 0.011447273197846672,
  },
  {
    companyKey: "56756",
    companyName: "Mouj",
    branchKey: "345345",
    branch: "King fahad",
    distance: 5.822936713752124,
  },
];

```

I have two issues

- How to sort records based on geo\_distance
- will query actions(match, script) apply to aggregation data...?

Can you please help me out to solve these issues

---

<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:** [September 4, 2022, 4:31pm UTC](https://discuss.elastic.co/t/distinct-records-with-geo-distance-sort-on-aggregation-es/311606/2 "2022-09-04T16:31:56Z")

</div>

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