2013-08-06 50 views
2

我建立了一个弹性搜索索引,其中包含每个国家/地区的不同_type映射。ElasticSearch _Type with geolocation

所以有一种映射“我们”“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才能使其工作?

+0

您是否尝试过使用通配符:* “位置”?还没有尝试过这一点,并不确定它的性能如何。 –

+0

[多索引文档](http://www.elasticsearch.org/guide/reference/api/multi-index/)也提到添加逗号分隔的索引列表,但没有示例。 –

回答

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

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

+0

在像你这样的基本搜索中,它很好,它可以以任何方式工作,它更多的是关于如何引用geo_distance分类器中的pin.location。 当建立索引映射时,它被设置为“位置”设置为“geo_point”。但这意味着我必须通过其_type来引用它。 us.location。而我需要不是类型特定的。所以它更多的是一个如何设置特定类型的映射时引用pin.location的问题,如果有意义的话。 –