【问题标题】:Can a single field take multiple queries in elasticsearch, using "multi_match"?单个字段可以在弹性搜索中使用“multi_match”进行多个查询吗?
【发布时间】:2021-03-23 22:09:44
【问题描述】:

我正在尝试使用 elasticsearch 中的查询。

{
  "query":{
    "bool":{
      "should":[
        {
          "multi_match":{
            "query":"History",
            "fields":[
              "service_category.keyword"
            ]
          }
        },
        {
          "terms":{
            "company_id":[
              "6"
            ]
          }
        }
      ]
    }
  }
}

这是我的查询。 结果是:所有采用service_category=History 的字段和所有采用company_id = 6service_category

现在我想将结果更改为:所有接受 service_category=Historycompany_id=2 以及 service_categoriescompany_id = 6 的字段。 我希望我的答案是这样的: (service_category.keyword:History AND company_id:2)AND (company_id:6)

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    我想你可以选择:

    {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "multi_match": {
                      "query": "History",
                      "fields": [ "service_category.keyword" ]
                    }
                  },
                  {
                    "terms": {
                      "company_id": [ "2" ]
                    }
                  }
                ]
              }
            },
            {
              "terms": {
                "company_id": [ "6" ]
              }
            }
          ]
        }
      }
    }
    

    可以理解为:

    (service_category.keyword:History AND company_id:2) OR (company_id:6)
    

    【讨论】:

    • 感谢乔的回答!我已经尝试过这种方式,结果它只给了我带有 company_id:6 的 service_category,即使我使用的是“必须”
    • (service_category.keyword:History AND company_id:2) AND(company_id:6) ,也就是说,我希望我的答案如何。对不起,我是新学习弹性搜索的人
    • 好的,然后在上面的查询中将should 替换为must
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多