弹性搜索查询:(A 或 B)和(C 或 D)

Elastic Search Query: (A or B) and (C or D)

我有一个弹性搜索索引,其中列是国家和专业知识。我需要写一个查询,在那里我可以得到记录 国家是(美国或英国) 和 专长是(植物学或物理学)

如何编写此查询?

您可以使用 boolterms 查询来执行此操作,如下所示:

{
    "query": {
        "bool": {
            "must": [
               {
                   "terms": {
                      "Country": [
                         "USA",
                         "UK"
                      ]
                   }
               },
               {
                   "terms": {
                      "Expertise": [
                         "Botany",
                         "Physics"
                      ]
                   }
               }
            ]
        }
    }
}

另一方面,您只能使用 bool 查询来执行此操作:

{
    "query": {
        "bool": {
            "must": [
               {
                   "bool": {
                       "should": [
                          {
                              "term": {
                                 "Country": {
                                    "value": "USA"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "Country": {
                                    "value": "UK"
                                 }
                              }
                          }
                       ]
                   }
               },
               {
                   "bool": {
                       "should": [
                          {
                              "term": {
                                 "Expertise": {
                                    "value": "Botany"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "Expertise": {
                                    "value": "Physics"
                                 }
                              }
                          }
                       ]
                   }
               }
            ]
        }
    }
}

但是,请先看 documentation 再问任何其他问题。也许,您会发现一个更短的查询。