2

我已经建立了一个弹性搜索索引,其中包括每个国家/地区的不同 _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 才能使其正常工作?

4

2 回答 2

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 ]
    } ]
  }
}
于 2013-08-18T20:58:07.190 回答
0

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

于 2013-08-06T06:42:24.213 回答