ElasticSearch布尔查询对象数组should子句

yqhsw0fo  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(168)

给定文档:书籍

[
 {
  name: "book a",
  authors: 
  [
   {
    name: "John",
   }
   {
    name: "Paul"
   }
  ]
 },
 {
  name: "book b",
  authors: 
  [
   {
    name: "Paul"
   }
  ]
 }
]

目标:返回匹配name = book a且匹配其中一个作者姓名为authors.name = John的书。
我尝试过:

"must": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "name": {
                          "value": "book a"
                        }
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "term": {
                              "authors.name": {
                                "value": "John"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]

Map名称:文本作者:动态的

d4so4syb

d4so4syb1#

您可以使用下图查询:
请注意,如果您使用的是ElasticSearch的动态Map,则使用name.keywordauthors.name.keyword。否则,如果您指定了自定义Map,则使用nameauthors.name

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "book a"
          }
        },
        {
          "term": {
            "authors.name.keyword": {
              "value": "John"
            }
          }
        }
      ]
    }
  }
}

相关问题