在获取相交(“和”)条件的记录时遇到问题。
我有一个文件:

{
        "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作为userUuidsigned作为true

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,第二个嵌套文档包含signed作为false
方案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
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

在这种情况下,一个嵌套的文档必须包含两个值。

让我知道这是否有帮助!

关于elasticsearch - ElasticSearch和嵌套查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60528559/

10-13 07:46
查看更多