2013-08-06 52 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的問題,如果有意義的話。 –