【问题标题】:Nested Query Elastic Search嵌套查询弹性搜索
【发布时间】:2021-09-01 08:49:24
【问题描述】:

目前我正在尝试在 Elastic Search Spring Data 中搜索/过滤嵌套文档。

当前的文档结构是:

{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": [
      {
        "id": 1,
        "status": true,
        "issue": "Variation Issue"
      },
      {
        "id": 32,
        "status": false,
        "issue": "NoiseIssue"
      }
    ]
  }
}

现在我们需要过滤掉具有噪声问题的策略数据,如果没有具有噪声问题的策略数据,则策略数据在父文档中将为空。

我已尝试使用此查询

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "customername": "Cust@345"
          }
        },
        {
          "nested": {
            "path": "policiesDetails.policy_data",
            "query": {
              "bool": {
                "must": {
                  "terms": {
                    "policiesDetails.policy_data.issue": [
                      "Noise Issue"
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

这可以很好地过滤嵌套文档。但如果嵌套文档没有匹配项,则会从视图中删除整个文档。

我想要的是如果嵌套过滤器不匹配:-

{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": null
  }

【问题讨论】:

    标签: spring elasticsearch spring-data-elasticsearch


    【解决方案1】:

    如果没有找到任何嵌套文档,则不会返回父文档。

    您可以对 policy_data 使用 should 子句。如果找到嵌套文档,它将在inner_hits 下返回,否则将返回父文档

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "customername": "Cust@345"
              }
            }
          ],
          "should": [
            {
              "nested": {
                "path": "policydetails.policy_data",
                "inner_hits": {},  --> to return matched policy_data
                "query": {
                  "bool": {
                    "must": {
                      "terms": {
                        "policydetails.policy_data.issue": [
                          "Noise Issue"
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "_source": ["id","customername","policydetails.address"] --> selected fields
    }
    

    结果:

    {
      "_index" : "index116",
      "_type" : "_doc",
      "_id" : "f1SxGHoB5tcHqHDtAkTC",
      "_score" : 0.2876821,
      "_source" : {
        "policydetails" : {
          "address" : {
            "city" : "Irvine",
            "address2" : "23994384, Out OF World",
            "post_code" : "92617",
            "state" : "CA"
          }
        },
        "id" : 1,
        "customername" : "Cust@123"
      },
      "inner_hits" : {
        "policydetails.policy_data" : {
          "hits" : {
            "total" : {
              "value" : 0,
              "relation" : "eq"
            },
            "max_score" : null,
            "hits" : [ ]  -->  nested query result , matched document returned
          }
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2017-06-11
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多