Elasticsearch:仅包含过滤对象的嵌套字段过滤基于嵌套字段的记录

发布于 2025-02-07 00:23:24 字数 3002 浏览 1 评论 0 原文

我正在尝试根据嵌套字段过滤记录,并希望该数组中的匹配对象作为记录的一部分显示。 以下是我要求的详细说明。

因此,我拥有这样的elasticsearch数据:

[{
  "basicInfo": {
    "requestId": 123,
  },
  "managerInfo": {
    "manager": "John",
    
  },
  "groupInfo": [
    {
      "id": "id1",
      "name": "abc",
      "status": "Approved"
    },
    {
      "id": "id2",
      "name": "abc",
      "status": "Pending"
    }
  ]
},
{
  "basicInfo": {
    "requestId": 233,
  },
  "managerInfo": {
    "manager": "John Sr",
    
  },
  "groupInfo": [ 
    {
      "id": "id3",
      "name": "abc",
      "status": "Pending"
    }
  ]
}
]

我只想用groupInfo.status作为批准的and basicinfo.requestid AS 123过滤记录,但是我的状况是我只能在GroupInfo中获得批准的记录,而不是待定的记录。因此,我期望的输出是:

{
  "took": 23,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 3.0602708,
    "hits": [
      {
        "_index": "my_index",
        "_type": "request",
        "_id": "123",
        "_score": 3.0602708,
        "_source": {
          "basicInfo": {
            "requestId": 123
          },
          "managerInfo": {
            "manager": "John"
          },
          "groupInfo": [
            {
              "id": "id1",
              "name": "abc",
              "status": "Approved"
            }
            // No id2 here as it is in pending state
          ]
        }
      }
    ]
  }
}

但是我能够实现:

{
  "took": 23,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 3.0602708,
    "hits": [
      {
        "_index": "my_index",
        "_type": "request",
        "_id": "123",
        "_score": 3.0602708,
        "_source": {
          "basicInfo": {
            "requestId": 123
          },
          "managerInfo": {
            "manager": "John"
          },
          "groupInfo": [
            {
              "id": "id1",
              "name": "abc",
              "status": "Approved"
            },
            {
              "id": "id2",
              "name": "abc",
              "status": "Pending"
            }
          ]
        }
      }
    ]
  }
}

这是我使用的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "basicInfo.requestId": "123"
          }
        },
        {
          "nested": {
            "path": "groupInfo",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "groupInfo.status": "Approved"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

因此,我的问题首先是我期望的,甚至可能吗?我们可以过滤结果并确保我们只从该结果获得匹配的数组吗? 如果是,我们该怎么做? 提前致谢。

I am trying to filter the records based on nested field and want only the matching object in that array to be shown as part of the record.
Below is the detailed explanation of my requirement.

So, I have Elasticsearch data like this:

[{
  "basicInfo": {
    "requestId": 123,
  },
  "managerInfo": {
    "manager": "John",
    
  },
  "groupInfo": [
    {
      "id": "id1",
      "name": "abc",
      "status": "Approved"
    },
    {
      "id": "id2",
      "name": "abc",
      "status": "Pending"
    }
  ]
},
{
  "basicInfo": {
    "requestId": 233,
  },
  "managerInfo": {
    "manager": "John Sr",
    
  },
  "groupInfo": [ 
    {
      "id": "id3",
      "name": "abc",
      "status": "Pending"
    }
  ]
}
]

I want to filter the records only with groupInfo.status as Approved and basicInfo.requestId as 123, but my condition is I should only get the Approved record in the groupInfo and not the pending ones. So, the output I am expecting is:

{
  "took": 23,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 3.0602708,
    "hits": [
      {
        "_index": "my_index",
        "_type": "request",
        "_id": "123",
        "_score": 3.0602708,
        "_source": {
          "basicInfo": {
            "requestId": 123
          },
          "managerInfo": {
            "manager": "John"
          },
          "groupInfo": [
            {
              "id": "id1",
              "name": "abc",
              "status": "Approved"
            }
            // No id2 here as it is in pending state
          ]
        }
      }
    ]
  }
}

But instead I am able to achieve:

{
  "took": 23,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 3.0602708,
    "hits": [
      {
        "_index": "my_index",
        "_type": "request",
        "_id": "123",
        "_score": 3.0602708,
        "_source": {
          "basicInfo": {
            "requestId": 123
          },
          "managerInfo": {
            "manager": "John"
          },
          "groupInfo": [
            {
              "id": "id1",
              "name": "abc",
              "status": "Approved"
            },
            {
              "id": "id2",
              "name": "abc",
              "status": "Pending"
            }
          ]
        }
      }
    ]
  }
}

This is the query I am using:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "basicInfo.requestId": "123"
          }
        },
        {
          "nested": {
            "path": "groupInfo",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "groupInfo.status": "Approved"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

So, my question is first what I am expecting, is that even possible? Can we filter the result and make sure that we get only the matched array from that result?
If yes, how can we do it?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

海风掠过北极光 2025-02-14 00:23:24

也许您正在寻找

在许多情况下,知道哪些内嵌套对象非常有用(在
嵌套的情况)或子女/家长文件(如果是
父母/孩子)导致返回某些信息。内心
命中功能可用于此功能。此功能将返回每个搜索点击
在搜索响应中
匹配不同的范围。

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "basicInfo.requestId": "123"
          }
        },
        {
          "nested": {
            "path": "groupInfo",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "groupInfo.status": "Approved"
                    }
                  }
                ]
              }
            },
            "inner_hits":{}
          }
        }
      ]
    }
  }
}

Maybe you are looking for Inner Hits.

In many cases, it’s very useful to know which inner nested objects (in
the case of nested) or children/parent documents (in the case of
parent/child) caused certain information to be returned. The inner
hits feature can be used for this. This feature returns per search hit
in the search response additional nested hits that caused a search hit
to match in a different scope.

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "basicInfo.requestId": "123"
          }
        },
        {
          "nested": {
            "path": "groupInfo",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "groupInfo.status": "Approved"
                    }
                  }
                ]
              }
            },
            "inner_hits":{}
          }
        }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文