【问题标题】:ElasticSearch _Type with geolocationElasticSearch _Type 与地理位置
【发布时间】:2013-08-19 04:58:07
【问题描述】:

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

【问题讨论】:

  • 您是否尝试过使用通配符:*."location"?还没有尝试过,不确定性能如何。
  • multi-index docs 也谈到了添加以逗号分隔的索引列表,但没有示例。

标签: elasticsearch


【解决方案1】:

对不起,如果我遗漏了一些明显的东西,但你为什么不能只按“位置”排序。它似乎工作得很好:

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 ]
    } ]
  }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 在像你这样的基本搜索中,它很好,它可以工作,更多的是关于我如何在 geo_distance 排序器中引用 pin.location。设置索引映射时,将“位置”设置为“geo_point”。但这意味着我必须通过它的_type 来引用它。我们的位置。然而,我不需要特定类型。因此,如果为特定类型设置映射时,应该如何引用 pin.location 的问题更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多