Sort the documents by sum of nested field value for matching documents


This Content is from Stack Overflow. Question asked by GP007

A beginner in ElasticSearch DSL here.

I have a list of users and their locations with location attributes as nested data.

          "id" : "ojEggfh9GRs"
          "name" : "chan k",
          "locations" : [
              "locationId" : "1010",
              "orderCount" : 100
              "locationId" : "1020",
              "orderCount" : 50
              "locationId" : "1030",
              "orderCount" : 10

What I’d like to query is: The list of users who have certain locations given in query, sorted by the sum of orderCount across those locations.

I tried a aggregation query for this, but i need user name and user id and few other attributes in results, it does not seem like correct way to do it.

Let me know if anyone can help me understand how can I get this result with other options like function-score or something else.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?