弹性搜索:一次查询中的多个术语搜索

Elastic Search: Multiple term search in one query

ElasticSearch 新手。

我在 index 下有文档:myindex 在带有映射的 Elastic 搜索中: http://host:port/myindex/_mapping

{
"mappings":{
   "properties": {
        "en_US":  {
             "type": "keyword"
                  }
                 }
          }
}

假设我的 3 个文档如下所示:

{
"product": "p1",
"subproduct": "p1.1"
}

{
"product": "p1",
"subproduct": "p1.2"
}

{
"product": "p2",
"subproduct": "p2.1"
}

现在,我查询使用单个子产品 p1.1 和产品 p1 如下,它工作正常:

POST: http://host:port/myindex/_search

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "product" : "p1" }
      },
      "filter": {
        "term" : { "subproduct" : "p1.1" }
      }
    }
  }
}

我的问题是: 如何在一个 _search 查询中查询 2 个或更多子产品,例如 product p1 下的 suproducts p1.1p1.2? 查询应该 return 所有子产品 p1.1 和子产品 p1.2p1 产品的列表。

只需将过滤器子句中的 term-query 更改为 terms-query 并搜索多个术语。

{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "product" : "p1" }
      },
      "filter": {
        "terms" : { "subproduct" : ["p1.1", "p1.2"] }
      }
    }
  }
}