弹性搜索:在一个查询中进行多项搜索

时间:2020-02-28 02:32:31

标签: json elasticsearch elasticsearch-5

ElasticSearch的新手。

在具有映射的弹性搜索中,我在indexmyindex下有文档: 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个或多个子产品,例如产品p1.1下的suproducts p1.2p1? 查询应返回所有子产品p1.1和带有p1.2产品的子产品p1的列表。

1 个答案:

答案 0 :(得分:0)

只需将过滤器子句中的term查询更改为terms查询,然后搜索多个词。

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