弹性搜索最近的不同地理位置



我创建了以下映射来索引我的 mysql 集合:

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

如何检索最近的不同文档?

假设您的索引称为 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
          }
        }
      }
    }
  }
}

最新更新