按弹性搜索过滤 [英] filtering by date in elasticsearch

查看:93
本文介绍了按弹性搜索过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索范围内有日期字段的所有项目,并且它失败(不返回结果)

I'm Trying to search for all the items who have a field of date inside a range, and it fails (returns no results)

查询:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "last_updated": {
            "from": "2013-01-01 00:00:00"
          }
        }
      }
    }
  }
}

映射:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            },
            "last_updated": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}

结果:

 {
        took: 1
        timed_out: false
        _shards: {
            total: 1
            successful: 1
            failed: 0
        }
        hits: {
            total: 0
            max_score: null
            hits: [ ]
        }
    }

具有数值的整数字段(卡)工作正常。
将日期更改为很早的开始(1900-01-01 00:00:00)也没有显示结果。

The same query filtered by a range for an integer field("cards") with a numeric value works fine. Changing the date to a very early start (1900-01-01 00:00:00) also shows no results.

我做错了什么?

BTW,我知道我在映射中启用了_timestamp,但这不是我过滤的字段。

BTW, I know I have the _timestamp enabled in the mapping, but that is not the field I am filtering by.

推荐答案

似乎要对我有效:

curl -XPUT localhost:9200/test -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "_timestamp": {
                "enabled": "true"
            },
            "properties": {
                "cards": {
                    "type": "integer"
                },
                "last_updated": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                }
            }
        }
    }
}
'
curl -XPOST localhost:9200/test/doc/1 -d '{
    "last_updated": "2012-01-01 12:13:14"
}
'
curl -XPOST localhost:9200/test/doc/2 -d '{
    "last_updated": "2013-02-02 01:02:03"
}
'
curl -X POST 'http://localhost:9200/test/_refresh'
echo
curl -X GET 'http://localhost:9200/test/doc/_search?pretty' -d '{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "range": {
                    "last_updated": {
                        "gte": "2013-01-01 00:00:00"
                    }
                }
            }
        }
    }
}
'

这篇关于按弹性搜索过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆