2015-06-15 104 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