带有地理位置的ElasticSearch _Type

时间:2013-08-06 03:07:05

标签: elasticsearch

我已经设置了一个弹性搜索索引,其中包含每个国家/地区的不同_type映射。

所以有“us”“au”“uk”等的映射。

每个映射都包含“geo_point”类型的位置映射

在添加不同的_types之前

我的查询排序如下:

"sort" : [
    {

            "_geo_distance" : {
            "postcode.location" : [' . $mylocation_long . ',' . $mylocation_lat . '], 
            "order" : "asc",
            "unit" : "km"
        }
    }
],

将_types添加到数据并对其进行映射不再有效,而是将其指定为:

 "sort" : [
    {

            "_geo_distance" : {
            "$country.location" : [' . $mylocation_long . ',' . $mylocation_lat . '], 
            "order" : "asc",
            "unit" : "km"
        }
    }
],

这很好。

但是,有时需要在一个国家/地区之外进行查询。因此将其设置为“us.location”是不正确的,并且无法正常工作。

在这种情况下,当我不知道国家/地区并且需要按映射位置对其进行排序时,如何进行此排序工作。

或者是否有这种情况无法完成,并且所有文档都必须具有相同的_type才能使其生效?

2 个答案:

答案 0 :(得分:2)

很抱歉,如果我遗漏了一些明显的东西,但为什么你不能只选择“位置”。它似乎工作正常:

curl -XDELETE localhost:9200/test-idx/ && echo 
curl -XPUT localhost:9200/test-idx/ -d '
{
    "settings":{
        "number_of_shards":1,
        "number_of_replicas":0
    },
    "mappings": {
        "us": {
            "properties": {
                "location": {
                  "type": "geo_point"
                }        
            }
        },
        "uk": {
            "properties": {
                "location": {
                  "type": "geo_point"
                }        
            }
        },
        "au": {
            "properties": {
                "location": {
                  "type": "geo_point"
                }        
            }
        }
    }
}' && echo 
curl -XPUT localhost:9200/test-idx/us/1 -d '
{
    "location": "42.3606402,-71.0674569"
}
' && echo 
curl -XPUT localhost:9200/test-idx/uk/2 -d '
{
    "location": "51.5286416,-0.1017943"
}
' && echo 
curl -XPUT localhost:9200/test-idx/au/3 -d '
{
    "location": "-33.8471226,151.0594183"
}
' && echo 

curl -XPOST localhost:9200/test-idx/_refresh && echo 
curl "localhost:9200/test-idx/_search?pretty" -d '{
    "query": {
        "match_all": {}
    },
    "sort" : [
        {
                "_geo_distance" : {
                "location" : "52.3712989,4.8937347", 
                "order" : "asc",
                "unit" : "km"
            }
        }
    ]
}' && echo

输出:

{"ok":true,"acknowledged":true}
{"ok":true,"acknowledged":true}
{"ok":true,"_index":"test-idx","_type":"us","_id":"1","_version":1}
{"ok":true,"_index":"test-idx","_type":"uk","_id":"2","_version":1}
{"ok":true,"_index":"test-idx","_type":"au","_id":"3","_version":1}
{"ok":true,"_shards":{"total":1,"successful":1,"failed":0}}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [ {
      "_index" : "test-idx",
      "_type" : "uk",
      "_id" : "2",
      "_score" : null, "_source" : {"location": "51.5286416,-0.1017943"},
      "sort" : [ 355.2735714686373 ]
    }, {
      "_index" : "test-idx",
      "_type" : "us",
      "_id" : "1",
      "_score" : null, "_source" : {"location": "42.3606402,-71.0674569"},
      "sort" : [ 5563.606078215864 ]
    }, {
      "_index" : "test-idx",
      "_type" : "au",
      "_id" : "3",
      "_score" : null, "_source" : {"location": "-33.8471226,151.0594183"},
      "sort" : [ 16650.926847312003 ]
    } ]
  }
}

答案 1 :(得分:0)

当您将工作查询指向/ index / _search而不是/ index / type / _search时会发生什么?