Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

filtering by date in elasticsearch

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

The query:

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

The mapping:

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

results in:

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

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.

What am I doing wrong?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems to be working fine to me:

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"
                    }
                }
            }
        }
    }
}
'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...