ElasticSearch 嵌套布尔查询

ElasticSearch nested bool queries

我正在使用 ElasticSearch 5.6。我有以下 JSON 文档。

    {
          "cards": [{
                        "tag_categories": [
                            {
                                "is_sensitive": true,
                                "category": "Users",
                                "tags": [
                                    {
                                        "is_selected": true,
                                        "name": "user1"
                                    },
                                    {
                                        "is_selected": true,
                                        "name": "user2"
                                    },
                                    {
                                        "is_selected": false,
                                        "name": "user3"
                                    }
                                ]
                            }
                        ],
                        "risk": "medium",
                        "position": 1,
                        "placement": 4,
                        "title": "test title",

                    }, ...]
       }

如果所有给定的用户名和相应的 is_selected 值都为真,我想 return 此文档。

这是我的查询。

{
  "_source": {
    "excludes": ["cards.pages"]
  },
  "query": {
    "bool": {
      "must": [{
        "match": {
          "_all": "hello world"
    }
  }],
  "filter": [{
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "cards.tag_categories.tags.name": "user2"
                      }
                    },
                    {
                      "match": {
                        "cards.tag_categories.tags.is_selected": true
                      }
                    }

                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "cards.tag_categories.tags.name": "user1"
                      }
                    },
                    {
                      "match": {
                        "cards.tag_categories.tags.is_selected": true
                      }
                    }

                  ]
                }
              }

            ]
          }
        }
      }
    },
    {
      "term": {
        "name.keyword": "hello name"
      }
    },
    {
      "term": {
        "title.keyword": "hello title"
      }
    }

  ]
   }
  }
}

我添加了两个子布尔查询来匹配每组用户名和 is_selected 值。如果为真,父 bool 查询将获得 AND 和 return 文档。

在上面的示例查询中,文档应该 returned 为 user1 和 user2 匹配条件。但它不会发生。

如果我将单个用户与他的 is_selected 值进行比较,文档 returns。例如:用户 1。

如果有人能告诉我哪里出错了,我将不胜感激。

我添加了单独的嵌套块并且成功了!

{
  "_source": {
  "excludes": ["cards.pages"]
 },
 "query": {
"bool": {
  "must": [{
    "match": {
      "_all": "hello world"
    }
  }],
  "filter": [
    {
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                  "term": {
                    "cards.tag_categories.tags.name.keyword": "user1"
                  }
                },
                {
                  "term": {
                    "cards.tag_categories.tags.is_selected": true
                  }
                }

            ]
          }
        }
      }
    },
    {
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                  "term": {
                    "cards.tag_categories.tags.name.keyword": "user2"
                  }
                },
                {
                  "term": {
                    "cards.tag_categories.tags.is_selected": true
                  }
                }

            ]
          }
        }
      }
    },

    {
      "term": {
        "name.keyword": "hello name"
      }
    },
    {
      "term": {
        "title.keyword": "hello title"
      }
    }

  ]
}

} }