如何编写查询弹性搜索?

时间:2018-07-04 14:08:04

标签: elasticsearch

我有这样的索引评论

{
    "_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 "
    }
},

我如何查询以获取附近的列表评论,但限制每个字段由现场商人获得5条评论? 我被困在这里太久了! 谢谢!

1 个答案:

答案 0 :(得分:2)

您只需要返回给定位置附近(例如100m)的评论,然后需要按marchant terms进行汇总并添加top_hits子汇总。它是这样的:

{
  "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
          }
        }
      }
    }
  }
}

只需将位置替换为要搜索的位置,如果需要更大或更小的距离,则可能替换为距离。