【问题标题】:Elastic Search nearest distinct geo locations弹性搜索最近的不同地理位置
【发布时间】:2020-04-16 19:10:38
【问题描述】:

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

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

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

【问题讨论】:

  • 使用地理距离查询
  • 地理距离查询不给出不同的,它只给出最近的位置。假设我需要前 100 个最近的位置。在前 100 个中,每个位置重复保存 10 次。那么前 100 个位置将只包含 10 个。这是确切的问题。
  • 也许将其包装在大小为 1 的热门点击聚合中以返回不同的位置?

标签: elasticsearch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多