【发布时间】:2021-10-18 12:08:37
【问题描述】:
我正在尝试过滤嵌套对象并按结果排序,但是,我尝试了一些没有成功的事情,我将放弃我最初的尝试并且它部分工作,它只是根据我在搜索中的内容进行过滤变量,但所有结果都来自这个嵌套对象,因为它位于另一个嵌套对象的“根”内
弹性版本:带有 NodeJS 的 7.13.0 使用来自 npm 的 @elastic/elasticsearch 官方包
let params: RequestParams.Search = {
index: index,
body: {
size: 30,
query: {
bool: {
must: [
{
nested: {
path: "profile",
query: {
bool: {
must: [
{
match: {
"profile.id": profileId,
},
},
],
},
},
},
},
],
filter: [
{
nested: {
path: "profile.following",
ignore_unmapped: true,
query: {
query_string: {
fields: [
"profile.following.name",
"profile.following.username",
],
query: searchWord + "*",
},
},
},
},
],
},
},
},
};
我需要它是在函数中通过参数传递的特定“profile.id”,因此结果只有 1 个具有 N 个人的个人资料
文档映射如下,我只留下了与问题相关的字段:
{
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "integer"
},
"phone": {
"type": "text"
},
"profile": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "text"
},
"following": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"isAwaitingApproval": {
"type": "boolean"
},
"name": {
"type": "text"
},
"profilePicURL": {
"type": "text"
},
"username": {
"type": "text"
}
}
}
}
}
}
}
}
}
当前结果的示例是: 带有以下参数 (profileId:141, searchWord: "para" )
{
"res": [
{
"profilePicURL": "localimage",
"name": "donor donor",
"id": 140,
"username": "victorTesteElastic2",
"isAwaitingApproval": false
},
{
"profilePicURL": "localimage",
"name": "para ser seguido",
"id": 142,
"username": "victorprivate",
"isAwaitingApproval": true
}
]
}
想要的结果是:
{
"res": [
{
"profilePicURL": "localimage",
"name": "para ser seguido",
"id": 142,
"username": "victorprivate",
"isAwaitingApproval": true
}
]
}
【问题讨论】:
标签: node.js elasticsearch