Elasticsearch node.js geo_distance过滤器

时间:2015-05-11 16:06:48

标签: node.js filter elasticsearch geolocation

我在nodejs app中使用elasticsearch模块,以便按geo_points查找文档。

根据http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-search,我试图这样做:

client.search({
  index: 'myindex',
  type: 'mytype',
  body: {
    filtered : {
        query : {
            match_all : {}
        },
        filter : {
            geo_distance : {
                distance : "200km",
                coordiates : {
                    lat : 40,
                    lon : -70
                }
            }
        }
    }
  }
}).then(function (resp) {
    var hits = resp.hits.hits;
    console.log(hits, "items");
}).catch(function (error) {
    console.log("Error on geo_distance");
});

但我总是遇到搜索解析错误。

有人有想法吗?

THX

1 个答案:

答案 0 :(得分:2)

最后,我找到了正确的语法/参数:

client.search({
  index: 'myindex',
  type: 'mytype',
  body: {
      from: from,
      size: size,
      query: {
        filtered: {
                filter: {
                    geo_distance: {
                        distance: '100000km',
                        coordiates: {
                            lat: 0.0,
                            lon: 0.0
                        }
                    }
                }
            }
        }
  }
}).then(function (resp) {
    var hits = resp.hits.hits;
    console.log(hits.length, "items around");
}).catch(function (error) {
    console.log("Error on geo_distance (coordiates)");
});

Thx Jhilden为你提供帮助。