如何在ExpressJS上使用ElasticSearch geo_distance

点击

{ "_index" : "users", "_type" : "user", "_id" : "001", "_score" : 1, "_source" : { "utype" : "user", "username" : "test", "location" : { "lat" : 0.1, "lon" : 0.0 }, "uid" : "001" } } 

Node.js – app.js

 client.search({ index: 'users', type: 'user', body: { query: { filtered: { query: { match_all: {} }, filter: { geo_distance: { distance: '2km', location: { lat: 0.0, lon: 0.1 } } } } } } }).then(function (response) { var hits = response.hits.hits; console.log(hits); }, function (error, response) { // ... }); 

错误

“error”:“SearchPhaseExecutionException [无法执行阶段[query_fetch],所有分片失败; shardFailures

…我不确定是否正确使用了geo_distance ,但是我确实使用了与elasticsearch文档相同的格式。 我使用ExpressJS安装elasticsearch npm,并使用上面的client.search代码进行geo_distance过滤。 而且命中不仅返回一个对象。 任何人都可以帮助我如何使这项工作? 谢谢!

您必须删除第一个查询,并用“user.location”replace“location”,因为ElasticSearch将其解释为不像属性的types。

  client.search({ index: 'users', body: { filter: { geo_distance: { distance: '2km', location: { lat: 0.0, lon: 0.1 } } } } }).then(function (response) { var hits = response.hits.hits; console.log(hits); }, function (error, response) { // ... }); 

我希望它可以帮助你。