【问题标题】:How to fetch unique geo codes from Elasticsearch?如何从 Elasticsearch 中获取唯一的地理代码?
【发布时间】:2017-07-26 15:40:27
【问题描述】:

我是 Elasticsearch 的新手。我已经通过 CURL 命令创建了索引并插入了一些文档。

curl -XPUT 'localhost:9200/museums?pretty' -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "doc": {
            "properties": {
                "location": {
                    "type": "geo_point"
                }
            }
        }
    }
}
'
curl -XPOST 'localhost:9200/museums/doc/_bulk?refresh&pretty' -H 'Content-Type: application/json' -d'
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d\u0027Orsay"}
{"index":{"_id":7}}
{"location": "52.374081,4.912350", "name": "NEMO7 Science Museum"}
{"index":{"_id":8}}
{"location": "52.369219,4.901618", "name": "Museum8 Het Rembrandthuis"}
{"index":{"_id":9}}
{"location": "52.371667,4.914722", "name": "Nederlands9 Scheepvaartmuseum"}
{"index":{"_id":10}}
{"location": "51.222900,4.405200", "name": "Letterenhuis10"}
{"index":{"_id":11}}
{"location": "48.861111,2.336389", "name": "Musée11 du Louvre"}
{"index":{"_id":12}}
{"location": "48.860000,2.327000", "name": "Musée12 d\u0027Orsay"}
'

如果您看到 curl 命令,我已经制作了一些重复的文档并插入了这些命令。现在,我想获取所有具有唯一地理代码的文档并对其应用 SORT(ASC)。

我得到了一个示例 CURL 命令,如下所示。

curl -XPOST 'localhost:9200/museums/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ]
            }
        }
    }
}
'

但是,它使用 RANGE。我只想获取唯一的地理代码并按升序排序。我也用谷歌搜索了,但是,无论我要获取 UNIQUE 文档,都只适用于 TEXT/NUMERIC 类型的文档。不在 GEO CODES 类型的文档中。

需要帮助。

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl


    【解决方案1】:

    试试:

    curl -XPOST 'localhost:9200/museums/_search?size=0&pretty' -H 'Content-Type: application/json' -d' {

        "size" : 0,
        "aggs": {
              "distinct_geo_distance" : {
                  "cardinality" : {
                    "field" : "location"
                  }
              }
         }
    }
    

    【讨论】:

    • 感谢您的回复。它返回不同的文档计数。在我的情况下是 6。但是,我也想要文档的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多