簡體   English   中英

嵌套查詢彈性搜索

[英]Nested Query Elastic Search

目前我正在嘗試在 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"
      }
    ]
  }
}

現在我們需要過濾掉有噪音問題的 policy_data,如果沒有有噪音問題的政策數據,則 policy_data 在父文檔中將為空。

我曾嘗試使用此查詢

{
  "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
  }

如果未找到任何嵌套文檔,則不會返回父文檔。

您可以對 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
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM