如何从弹性搜索中获取最近30分钟的记录

时间:2019-02-07 03:25:59

标签: elasticsearch postman

I am using following query to fetch all last 30 minutes records using elastic search, but I'm getting parsable error on line "now-30m".



    Query: 
{

  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "appName": "magnus-alerting-system"
          }
        },
        {
          "match": {
            "countryCode": "US2"
          }
        },
        {
          "match": {
            "eventCode": 201
          }
        },
        {
          "match": {
            "extractName": "LaborDemand"
          }
        },{
          "range": {
            "eventPostTimestamp": {
              **"gte": "now()-30m"**
            }
          }
        }

      ]
    }
  }
}

执行服务时邮递员出错:  “根本原因”: [             {                 “ type”:“ number_format_exception”,                 “ reason”:“对于输入字符串:\” now()-30m \“”             }         ]

请让我知道如何纠正它。

2 个答案:

答案 0 :(得分:2)

原因是因为elasticsearch中的now()-30m错误,因为正确的格式只是“ now”。 Documentation

因此,正确的查询如下:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "appName": "magnus-alerting-system"
          }
        },
        {
          "match": {
            "countryCode": "US2"
          }
        },
        {
          "match": {
            "eventCode": 201
          }
        },
        {
          "match": {
            "extractName": "LaborDemand"
          }
        },{
          "range": {
          "eventPostTimestamp": {
          "gte": "now-30m"
            }
          }
        }

      ]
    }
  }
}

答案 1 :(得分:1)

data math中使用range query for date field的正确语法如下:

{
  "range": {
    "eventPostTimestamp": {
      "gte": "now-30m"
    }
  }
}