在弹性搜索中使用GET API进行搜索查询

时间:2019-12-03 08:02:43

标签: elasticsearch logstash elastic-stack

我正在使用7.4版。 我想主要实现三件事。 我想按日期范围进行过滤,然后在“标题”字段中搜索关键字并输出所选字段。以下是我尝试但无法使用的代码,请帮助。

POST test/_search
{
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "query": "Market",
          "fields": [
            "title",
            "message"
          ]
        }
      },
      "filter": {
        "range": {
          "published": {
            "gte": "now-300d/d",
            "lt": "now/d"
          }
        }
      }
    }
  },
  "_source": [
    "title",
    "message",
    "published"
  ]
}

我得到如下

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "no [query] registered for [filtered]",
        "line": 3,
        "col": 17
      }
    ],
    "type": "parsing_exception",
    "reason": "no [query] registered for [filtered]",
    "line": 3,
    "col": 17
  },
  "status": 400
}

上面还有其他方法或更正吗?

1 个答案:

答案 0 :(得分:1)

ES 7.4 不支持过滤,取而代之的是,我们将使用必须查询

POST <index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "Market",
            "fields": [
              "title",
              "message"
            ]
          }
        },
        {
          "range": {
            "published": {
              "gte": "now-300d/d",
              "lt": "now/d"
            }
          }
        }
      ]
    }
  },
  "_source": [
    "title",
    "message",
    "published"
  ]
}