如何从Elasticsearch获取唯一的地理代码?

时间:2017-07-26 07:14:16

标签: elasticsearch elasticsearch-dsl

我是Elasticsearch的新手。我创建了INDEX&按照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命令我已经制作了一些重复的文档&插入那些也。现在,我想获取所有具有UNIQUE GEO CODES&对此应用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。我只想要获取唯一的GEO CODES&按升序排序。我也用Google搜索,但是,无论我要获取UNIQUE文档,只能使用TEXT / NUMERIC类型的文档。不在GEO CODES类型文档上。

需要一些帮助。

1 个答案:

答案 0 :(得分:0)

尝试:

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

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