如何在OpenSearch中为嵌套对象制定一个条件?
openSearch摄入文档类似于此示例(仅是一个最小的示例):
PUT nested_test/_doc/4
{
"log": "This is a fourth log message",
"function": "4 test function",
"related_objects": [
{ "type": "user", "id": "10" },
{ "type": "offer", "id": "120" }
]
}
PUT nested_test/_doc/5
{
"log": "This is a fifth log message",
"function": "5 test function",
"related_objects": [
{ "type": "user", "id": "120" },
{ "type": "offer", "id": "90" }
]
}
使用许多文档,我想过滤那些具有特定相关对象的文档(例如 id = 120
)。在上面的示例数据中,这仅应使用ID 5返回文档。使用以下简单过滤器(DQL语法)不起作用:
Ressect_objects.type:user and rection_objects.id:120
,
因为这将是这样。还匹配文档5,因为与类型用户有一个相关的_object和具有ID 120的相关对象,尽管它不是具有ID 120的相关用户对象,而是其相关报价。
Opensearch ingests documents similar to this example (its just a minimal example):
PUT nested_test/_doc/4
{
"log": "This is a fourth log message",
"function": "4 test function",
"related_objects": [
{ "type": "user", "id": "10" },
{ "type": "offer", "id": "120" }
]
}
PUT nested_test/_doc/5
{
"log": "This is a fifth log message",
"function": "5 test function",
"related_objects": [
{ "type": "user", "id": "120" },
{ "type": "offer", "id": "90" }
]
}
With many of these documents, I'd like to filter those which have a specific related object (e.g. type=user
and id=120
). With the example data above, this should only return the document with id 5. Using simple filters (DQL syntax) as follows does not work:
related_objects.type:user and related_objects.id:120
As this would also match a document 5, as there is a related_object with type user and a related object with id 120, although its not the related user object with id 120, its the related offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Elasticsearch查询示例:
基本上只需进入嵌套查询,并指定您的所有和条件,就像Bool查询中的条款一样。
Elasticsearch query example:
Basically just go into a nested query and specify all your AND conditions as MUST clauses inside a bool query.
如果使用数组[对象],则字段类型为
nested
,文档参考If Array[object] is used, the field type is
nested
, The document reference一旦宣布该领域为嵌套字段,可以运行简单的DQL查询以获取所需信息:
这要求该字段已定义为嵌套:
As soon as the field is declared as nested field, it is possible to run a simple DQL query to get the desired information:
This requires that the field has been defined as nested before: