OR&弹性搜索中的AND运算符

时间:2016-10-20 10:57:58

标签: elasticsearch

您好我想在Elasticsearch中实现这一目标。

select * from products where brandName = Accu or brandName = Perfor AND cat=lube(any where in any filed of an elastic search ).

我在Elasticsearch中使用此查询。

{
  "bool": {
    "must": {
      "query_string": {
        "query": "oil"
      }
    },
    "should": [
      {
        "term": {
          "BrandName": "Accu"
        }
      },
      {
        "term": {
          "BrandName": "Perfor"
        }
      }
    ]
  }
}

通过此查询,我得不到组合的确切结果。

2 个答案:

答案 0 :(得分:1)

如果您的minimum_should_match: 1字段是已分析的字符串,则需要在查询中添加match,并可能使用term代替BrandName

{
  "bool" : {
    "must" : {
      "query_string" : {
        "query" : "oil OR lube OR lubricate"
      }
    },
    "minimum_should_match": 1,         <---- add this
    "should" : [ {
      "match" : {
        "BrandName" : "Accu"
      }
    }, {
      "match" : {
        "BrandName" : "Perfor"
      }
    } ]
  }
}

答案 1 :(得分:0)

此查询符合您的条件。

{
    "bool" : {
        "must" : { "term" : { "cat" : "lube" } },
        "should" : [
            { "term" : { "BrandName" : "Accu" } },
            { "term" : { "BrandName" : "Perfor" } }
        ],
    "minimum_should_match" : 1
    }
}