如何在OpenSearch中为嵌套对象制定一个条件?

发布于 2025-02-11 10:24:48 字数 794 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2025-02-18 10:24:48

Elasticsearch查询示例:

{
    "query" : {
        "nested" : {
              "path" : "related_objects",
              "query" : {
                   "bool" : {
                      "must" : [
                          {
                             "term" : {"related_objects.type" : "MYTYPE"}
                          },
                          {
                             "term" : {"related_objects.id" : "MYID"}
                          }
                      ]
                   }
              }
        }
    }
}

基本上只需进入嵌套查询,并指定您的所有和条件,就像Bool查询中的条款一样。

Elasticsearch query example:

{
    "query" : {
        "nested" : {
              "path" : "related_objects",
              "query" : {
                   "bool" : {
                      "must" : [
                          {
                             "term" : {"related_objects.type" : "MYTYPE"}
                          },
                          {
                             "term" : {"related_objects.id" : "MYID"}
                          }
                      ]
                   }
              }
        }
    }
}

Basically just go into a nested query and specify all your AND conditions as MUST clauses inside a bool query.

春夜浅 2025-02-18 10:24:48

如果使用数组[对象],则字段类型为nested文档参考

If Array[object] is used, the field type is nested, The document reference

明月夜 2025-02-18 10:24:48

一旦宣布该领域为嵌套字段,可以运行简单的DQL查询以获取所需信息:

related_objects:{type:"user" and id:120}

这要求该字段已定义为嵌套:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "related_objects": {
        "type": "nested" 
      }
    }
  }
}

As soon as the field is declared as nested field, it is possible to run a simple DQL query to get the desired information:

related_objects:{type:"user" and id:120}

This requires that the field has been defined as nested before:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "related_objects": {
        "type": "nested" 
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文