【问题标题】:Elasticsearch returns all results with Should and term queryElasticsearch 使用应该和术语查询返回所有结果
【发布时间】:2017-05-13 03:33:20
【问题描述】:

我想在 Elasticsearch 中执行以下查询

SELECT * FROM TABLE WHERE feed_id =1 AND brand='Samsung' OR brand='Apple iPhone'

我为此使用了这个 JSON 查询

``

"bool" : {



 "should":[
    {
        "multi_match":
            {
            "query":"LG",
            "operator":"and",
            "fields":"brand"
            }
    },

        {
        "multi_match":
            {
            "query":"Samsung",
            "operator":"and",
            "fields":"brand"
            }
    },

    {
        "multi_match":
    {
        "query":"Nokia",
        "operator":"and",
        "fields":"brand"
    }
    }


    ],



     "filter": {
      "bool": { 
          "must": [

              { "term": { "feed_id":1}}
          ]

      }
    }

}``

这会返回所有结果..

这是我的地图

"model": { "type": "text", "fields": { "autocomplete": { "type": "text", "analyzer": "autocomplete" }, "keyword": { "type": "keyword" } } }

但是,当我删除“过滤器”术语时,我会得到想要的结果。

我的查询有什么问题?

我使用的是 ES 5.0

【问题讨论】:

  • 您能否提供包含在brandfeed_id 映射中的完整映射?

标签: elasticsearch


【解决方案1】:

试试这个查询。当您在单个字段上搜索时,不要认为您需要 multi_match

{
 "query": {
  "bool": {
     "must": [
        {
           "term": {
              "feed_id": 1
           }
        },
        {
           "terms": {
              "brand": [
                 "LG",
                 "Samsung",
                 "Nokia"
              ]
           }
        }
       ]
    }
  }
 }

【讨论】:

  • 谢谢,但是我怎样才能达到精确匹配的相同结果呢?那我也有同样的问题。
  • @erwinnandpersad。请查看编辑后的答案
  • 感谢@Rich,当我将字段更改为 Model.keyword 时,它不适用于一个句子。例如“Galaxy S7”不返回任何记录
  • 请分享映射。 GET index/type/_mapping
  • 您确定您有一个文档,其feed_id 为 1,模型为 Galaxy s7
【解决方案2】:

现在可以了:

我添加了 minimum_should_match 过滤器,现在它可以工作了..

如果有人遇到同样的问题,请查看查询

`

    {

        "multi_match":
            {
            "query":"Nokia",
            "operator":"and",
            "fields":"brand"
            }
    },

        {

        "multi_match":
            {
            "query":"Wifi",
            "operator":"and",
            "fields":"name"
            }
    },

    {
        "multi_match":
    {
        "query":"LG K10",
        "operator":"and",
        "fields":"name"
    }
    }


    ],
    "minimum_should_match":"1"

}

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2015-03-09
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多