elasticsearch - 如何在Elasticsearch中使用日期过滤器

标签 elasticsearch elasticsearch-5 elasticsearch-query

我正在使用elasticsearch的客户端对象。

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host:"https://********",
    log: 'trace',
});


"hits": [
        {
            "_index": "abc",
            "_type": "_doc",
            "_id": "39KueHEBnbKK-Fpwq9wG",
            "_score": 1.0,
            "_source": {
                "videoId": "EV-IOHABXh-qOCdiWVbW",
               "createddate": "2020-04-14 18:04:05"


            }
        },
        {
            "_index": "abc",
            "_type": "_doc",
            "_id": "29KueHEBnbKK-Fpwq9wG",
            "_score": 1.0,
            "_source": {
                "videoId": "zV-IOHABXh-qOCdiWVbW",
                 "createddate": "2020-03-14 18:04:05",


            }
        }
    ]

我正在尝试根据 createddate 进行过滤。这是我的查询,我正在尝试过滤出数据,但不起作用。请帮我。

注意:我需要过滤两个日期之间的数据。
{
   "query": {
    "bool": {
        "filter": [
            {

                "range": {
                    "createddate": {
                        "gte": "2020-04-11 00:00:00",
                        "lte": "2020-04-16 23:59:59"
                    }
                }
            }
        ]
    }
}

}

最佳答案

映射:

{
  "mappings": {
    "properties": {
      "createddate": {
        "type": "date",
        "format":"yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

索引两个文档
{
    "videoId": "EV-IOHABXh-qOCdiWVbW",
    "createddate":"2020-04-14 18:04:05"
 }

 {
 "videoId": "zV-IOHABXh-qOCdiWVbW",
 "createddate": "2020-03-14 18:04:05"
 }

您提供的相同搜索查询:
{
   "query": {
    "bool": {
        "filter": [
            {

                "range": {
                    "createddate": {
                        "gte": "2020-04-11 00:00:00",
                        "lte": "2020-04-16 23:59:59"
                    }
                }
            }
        ]
    }
}
}

结果:
"hits": [
         {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
               "videoId": "EV-IOHABXh-qOCdiWVbW",
               "createddate": "2020-04-14 18:04:05"
            }
         }
      ]

搜索查询将过滤两个日期之间的数据,并给出预期结果。

关于elasticsearch - 如何在Elasticsearch中使用日期过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61212989/

相关文章:

elasticsearch - Elasticsearch集群运行状况间歇性地在 'GREEN'和 'YELLOW'之间摆动

java - Elasticsearch 索引包含并以搜索开头

php - 如果字母数少,为什么 Elasticsearch 不起作用?

elasticsearch - elasticsearch与neo4j数据库的集成

ElasticSearch 查找带空格的术语

elasticsearch - 备份elasticsearch索引

python - 读取 CSV 并将数据上传到 Elasticsearch

elasticsearch - 查询获取平均速度

elasticsearch - 当按特定字段搜索时,ElasticSearch不返回结果

Elasticsearch聚合顺序与搜索结果相同