gpt4 book ai didi

node.js - 如何在 ExpressJS 上使用 ElasticSearch geo_distance

转载 作者:太空宇宙 更新时间:2023-11-03 22:15:14 25 4
gpt4 key购买 nike

点击

{
"_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[Failed to execute phase [query_fetch], all shards failed; shardFailures

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

最佳答案

您必须删除第一个查询并将“location”替换为“user.location”,因为 ElasticSearch 将其解释为类型而不是属性。

 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) {
// ...
});

希望对你有帮助。

关于node.js - 如何在 ExpressJS 上使用 ElasticSearch geo_distance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com