1

I have a index review like this

{
    "_index": "zody_review",
    "_type": "review",
    "_id": "5b3c6c9e68cf860e1af5f7fd",
    "_score": null,
    "_source": {
      "user": "571899623dc63af34d67a662",
      "merchant": "56f8f80119a4c1ae791cf7bf",
      "point": 3,
      "score": 2,
      "createdAt": "2018-07-04T13:43:42.331+07:00",
      "location": {
        "lat": 16.07054040054832,
        "lon": 108.22062939405441
      },
      "feedback": "Phuc vu khong tot lam "
    }
},

How can I query to get list review nearby, but limit get 5 reviews for each group by field merchant? I've been stuck here too long! Thank you!

1
  • what do you mean by nearby in your query and also can you write about the query that you've used for aggregations? Commented Jul 4, 2018 at 14:49

1 Answer 1

2

You need to only return reviews that are near (say 100m) a given location and then you need to aggregate by marchant terms and add a top_hits sub-aggregation. It goes like this:

{
  "size": 0,
  "query": {
    "geo_distance": {
      "distance": "100m",
      "location": {
        "lat": 16.07055,
        "lon": 108.2207
      }
    }
  },
  "aggs": {
    "by_merchant": {
      "terms": {
        "field": "merchant"
      },
      "aggs": {
        "top_5": {
          "top_hits": {
            "_source": [
              "feedback"
            ],
            "size": 5
          }
        }
      }
    }
  }
}

Simply replace the location by the one you want to search around and probably the distance if you need a larger or smaller distance.

Sign up to request clarification or add additional context in comments.

3 Comments

"Nearby" that mean sort by distance from your location
Do you know how to sort before aggs ?
You mean after aggs since the hits are sorted per each aggregation bucket?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.