elasticsearch:将一个应该变成一个过滤器

时间:2017-02-16 14:35:46

标签: elasticsearch

我希望使用过滤器进行搜索,该过滤器排除的结果与条件或其他条件不匹配:

我试图在过滤器中做一个但它失败了:

POST /my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "should": [
            {
              "match": {
                "type1_title": "searched match"
              }
            },
            {
              "match": {
                "type2_title": "searched match"
              }
            }
          ]
        }
      ]
    }
  }
}

它引发了这个错误:

 "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[should] query malformed, no start_object after query name",
        "line": 9,
        "col": 21
      }
    ],
    "type": "parsing_exception",
    "reason": "[should] query malformed, no start_object after query name",
    "line": 9,
    "col": 21
  },
  "status": 400
}

你知道我们是否可以做过滤器吗?

1 个答案:

答案 0 :(得分:1)

为什么不简单地bool/should,这里不需要filter

POST /my_index/_search
{
  "query": {
    "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "match": {
                "type1_title": "searched match"
              }
            },
            {
              "match": {
                "type2_title": "searched match"
              }
            }
          ]
    }
  }
}

如果你真的想保留bool/filter/should构造,那么你需要这样做:

POST /my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
         "bool": {
          "should": [
            {
              "match": {
                "type1_title": "searched match"
              }
            },
            {
              "match": {
                "type2_title": "searched match"
              }
            }
          ]
         }
        }
      ]
    }
  }
}