【问题标题】:Elastic Search: Multiple term search in one query弹性搜索:一个查询中的多个术语搜索
【发布时间】:2020-06-12 03:06:19
【问题描述】:

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" }
      }
    }
  }
}

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

【问题讨论】:

    标签: json elasticsearch elasticsearch-5


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢@Daniel。有没有一种方法可以让每个 term 只获得一个回报?应该只选择字段中空格数最少的术语?
    • 没有。您的条款包含空格?您需要通过使用不同的查询、不同的分析器(远离完全匹配)来解决这样的要求。请举一个具体的例子为此提出一个新问题,如果我没记错的话 - cmets 不应该用于新问题
    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多