代码之家  ›  专栏  ›  技术社区  ›  Om Sao

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

  •  0
  • Om Sao  · 技术社区  · 5 年前

    ElasticSearch新手。

    我有文件在 index : myindex 在使用映射的弹性搜索中: 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 如下所示,工作正常:

    http://host:port/myindex/_search

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

    我的问题是 : 如何在一个搜索查询中查询2个或多个子产品,如SupProducts p1。1. p1.2 副产品 p1 ? 查询应返回所有子管道的列表 p1。1. 和子管道 p1。2. 具有 p1 产品

    0 回复  |  直到 5 年前
        1
  •  0
  •   Om Sao    5 年前

    只需更改 term -在筛选子句中查询到 terms -查询和搜索多个术语。

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