2015-06-15 107 views
0

我試圖搜索以前添加到索引的文檔,該索引已被配置爲允許地理空間查詢(或者我認爲)。
我的elasticsearch實例託管在qbox.io上。Elasticsearch地理空間搜索,索引設置問題

這是我寫的命令行

curl -XPOST username:[email protected]/events -d '{ 
    "settings" : { 
     "number_of_shards" : 1 
    }, 
    "mappings" : { 
     "mygeopoints": { 
     "properties": { 
      "geopoint": { 
      "type": "geo_point", 
      "lat_lon" : true 
      }, 
      "radius": { 
      "type": "long" 
      } 
     } 
     } 
    } 
    }' 

創建索引正如我的理解是,我應該我events指數,我想執行的搜索的類型之間建立映射代碼它。

這是我寫的創建測試文檔中的代碼:

var elasticsearch = require('elasticsearch'); 
var client = new elasticsearch.Client({ 
    host: 'username:[email protected]' 
}); 

client.create({ 
    index: 'events', 
    type: 'geo_point', 
    body: { 
    location: { 
     lat: 51.507351, 
     lon: -0.127758 
    } 
    } 
}, console.log); 

這是我寫的搜索給出半徑的文檔的代碼

var elasticsearch = require('elasticsearch'); 
var client = new elasticsearch.Client({ 
    host: 'username:[email protected]' 
}); 

client.search({ 
    filtered: { 
    query: { 
     match_all: {} 
    }, 
    filter: { 
     geo_distance: { 
     distance: '1km', 
     location: { 
      lat: 48.507351, 
      lon: -0.127758 
     } 
     } 
    } 
    } 
}, console.log); 

我的問題是,所有的證件event索引總是顯示出來,所以我沒有用地理空間查詢成功過濾;你發現任何錯誤,或者你有任何指導我可以遵循這樣做嗎?我搜索過,只找到了一些信息。

回答

1

有代碼中的幾個問題:

問題1:當你在第二個片段創建文檔,你不使用正確映射類型和你的身體不包括正確的如在映射聲明字段名稱:

client.create({ 
    index: 'events', 
    type: 'geo_point',  <-------- wrong type 
    body: { 
    location: {   <-------- wrong field name 
     lat: 51.507351, 
     lon: -0.127758 
    } 
    } 
}, console.log); 

由於在映射類型,你聲明類型被稱爲mygeopointsgeo_point場被稱爲geopoint,您的通話create必須正確地使用它們像這樣:

client.create({ 
    index: 'events', 
    type: 'mygeopoints', 
    body: { 
    geopoint: { 
     lat: 51.507351, 
     lon: -0.127758 
    } 
    } 
}, console.log); 

問題2:作爲查詢DSL需要分配給body參數(類似於您create調用),它也是很好的做法,添加index參數哈希在你search call是不正確的參數集中你的搜索(見下文)

問題3:最後,在你查詢你不使用你的geo_distance過濾器正確的領域,你有location代替geopoint。您的查詢應該是這樣的:

client.search({ 
    index: 'events',    <---- add index name 
    body: {       <---- add query in body parameter 
    query:{ 
    filtered: { 
     filter: { 
     geo_distance: { 
      distance: '1km', 
      geopoint: {   <---- proper geo point field name 
      lat: 48.507351, 
      lon: -0.127758 
      } 
     } 
     } 
    } 
    } 
    } 
}, console.log); 
+0

非常感謝您的回答,我會盡快回復您! –

+0

我試過這個,我得到了以下錯誤:https://gist.github.com/lazywithclass/099ef5ec61f870a07e56 如果我從查詢的「body」中刪除'filtered'屬性,雖然我得到了預期的結果,請問你能指出一些情況,或者請指點一下正確的方向嗎? 感謝您的幫助,非常感謝! –

+0

哎呀,我的壞,我複製/粘貼你的查詢,並忘記把它放在一個'查詢'。上面更新了我的答案。 – Val