弹性搜索区别最近的地理位置

时间:2018-04-14 18:18:13

标签: elasticsearch

我使用弹性搜索创建了一个映射来索引我的mysql集合。这是映射属性

"properties" : {
   "post_place_location": {
        "type": "geo_point"
   }
}

我想从弹性搜索中检索不同的最近地理位置

1 个答案:

答案 0 :(得分:0)

假设您的索引称为locs,而您的geo_point字段称为location,则可以使用以下内容:

GET locs/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "geo_distance": {
            "distance": "20km",
            "location": {
              "lat": 48.19735694026487,
              "lon": 16.38336181640625
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "unique_count": {
      "cardinality": {
        "script": {
          "lang": "painless",
          "source": "doc['location'].getValue();"
        }
      }
    },
    "terms_agg": {
      "terms": {
        "script": {
          "lang": "painless",
          "source": "doc['location'].getValue();"
        }
      },
      "aggs": {
        "top_hits_agg": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}