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

问题描述 投票:0回答:1

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的列表。

json elasticsearch elasticsearch-5
1个回答
0
投票
只需将过滤器子句中的term查询更改为terms查询并搜索多个术语。

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

© www.soinside.com 2019 - 2024. All rights reserved.