簡體   English   中英

ElasticSearch 和嵌套查詢

[英]ElasticSearch and nested query

使用相交(“和”)條件獲取記錄時遇到問題。 我有一個文檔:

{
        "uuid": "1e2a0c06-af24-42e1-a31a-0f84233521de",
        "subject": "subj",
        "relations": [
            {
                "userUuid": "0f38e576-6b1f-4c1a-86a8-67a55a06d504",
                "signed": false
            },
            {
                "userUuid": "15979293-6b04-41a9-a6aa-bba99499496f",
                "signed": true
            }
        ]
}

查詢並期望得到 EMPTY 結果,不同嵌套元素滿足原因條件:

    "bool": {
        "must": [
            {
                "nested": {
                    "query": {
                        "term": {
                            "relations.userUuid": {
                                "value": "15979293-6b04-41a9-a6aa-bba99499496f",
                                "boost": 1.0
                            }
                        }
                    },
                    "path": "relations",
                    "ignore_unmapped": false,
                    "score_mode": "none",
                    "boost": 1.0
                }
            },
            {
                "nested": {
                    "query": {
                        "term": {
                            "relations.signed": {
                                "value": false,
                                "boost": 1.0
                            }
                        }
                    },
                    "path": "relations",
                    "ignore_unmapped": false,
                    "score_mode": "none",
                    "boost": 1.0
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1.0
    }
}

如何在同一個嵌套對象中查詢該條件為“AND”?

查看您的評論更新了答案。 您需要在嵌套文檔中提及path

場景 1:如果您希望任何嵌套文檔包含5979293-6b04-41a9-a6aa-bba99499496f作為userUuidsignedtrue

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "relations",         <---- Note this
            "query": {
              "term": {
                "relations.userUuid": "15979293-6b04-41a9-a6aa-bba99499496f"
              }
            }
          }
        },
        {
          "nested": {
            "path": "relations",
            "query": {
              "term": {
                "relations.signed": false
              }
            }
          }
        }
      ]
    }
  }
}

如果有兩個嵌套文檔,這將返回 true,第一個嵌套文檔包含userUuid ,第二個嵌套文檔包含signedfalse

場景 2:如果您希望兩個字段都出現在單個嵌套文檔中

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "relations",       <---- Note this
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "relations.userUuid": "15979293-6b04-41a9-a6aa-bba99499496f"
                    }
                  },
                  {
                    "term": {
                      "relations.signed": false
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

在這種情況下,單個嵌套文檔必須包含這兩個值。

讓我知道這是否有幫助!

暫無
暫無

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

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