具有特定条件的嵌套对象上的弹性搜索查询
我需要一些有关弹性搜索查询的帮助。我的索引有一个名为部门
的嵌套字段,假设嵌套对象具有dept-id-id
字段。我想对这个嵌套对象进行搜索,以及下面我将要突出显示的条件。
Index/_mapping
{
"items": {
"mappings": {
"properties": {
"item-id": {"type": "text"},
"departments": {
"type": "nested",
"properties": {
"dept-id": {
"type": "text"
}
}
}
}
}
}
}
Sample Data
{
"hits": [
{
"_index": "items",
"_source": {
"item-id": "item-1",
"departments": [
{
"dept-id": "dept-1"
},
{
"dept-id": "dept-2"
},
{
"dept-id": "dept-3"
}
]
}
},
{
"_index": "items",
"_source": {
"item-id": "item-2",
"departments": [
{
"dept-id": "dept-1"
}
]
}
},
{
"_index": "items",
"_source": {
"item-id": "item-3",
"departments": [
{
"dept-id": "dept-2"
}
]
}
}
]
}
条件:
- 我需要根据此无活动部门ID列表过滤上述数据。
- 不活动的部门:[“ 1”]
- 我正在尝试查询索引以使用item-id 1和3返回数据。
- 我在嵌套对象上尝试了
ass_not
,但不是答案i' m寻找,因为它仅返回item-3。将活动字段(布尔类型)添加到嵌套对象中,这主要是解决我的问题的方法。但是,我很好奇是否有办法在没有其他字段的情况下查询此问题?
I need some help with the elastic search query. My index has a nested field called departments
, let's say the nested object has a dept-id
field. I would like to do a search on this nested object and the condition I will highlight below.
Index/_mapping
{
"items": {
"mappings": {
"properties": {
"item-id": {"type": "text"},
"departments": {
"type": "nested",
"properties": {
"dept-id": {
"type": "text"
}
}
}
}
}
}
}
Sample Data
{
"hits": [
{
"_index": "items",
"_source": {
"item-id": "item-1",
"departments": [
{
"dept-id": "dept-1"
},
{
"dept-id": "dept-2"
},
{
"dept-id": "dept-3"
}
]
}
},
{
"_index": "items",
"_source": {
"item-id": "item-2",
"departments": [
{
"dept-id": "dept-1"
}
]
}
},
{
"_index": "items",
"_source": {
"item-id": "item-3",
"departments": [
{
"dept-id": "dept-2"
}
]
}
}
]
}
Condition:
- I need to filter the data above based on this list of inactive-department ids.
- inactive-department: ["1"]
- I am trying to query for the index to return data with item-id 1 and 3.
- I tried
must_not
on the nested object but it is not the answer I'm looking for as it only returns item-3. Adding an active field (boolean type) to the nested object which mostly will be a solution to my problem. However, I am curious is there a way to query this without an additional field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道我是否正确理解,但是您想通过“ dept-1”过滤(哪些是不活动的),并与文档返回:item-1和item-3?
我要做的第一件事是将Dept-ID字段的类型作为关键字。
我使用的查询是:
结果:
I don't know if I understand correctly but you want to filter by "dept-1" (which are the inactive ones) and have the return with the docs: item-1 and item-3?
The first thing I did was put the type of the dept-id field as a keyword.
The query I used was this:
Results: