1

The following sequence of HTTP requests sent from Postman to an Elasticsearch 7.17.5 server yields two search hits corresponding to objects with dates 2022-07-27T00:00:00.000Z and 2022-07-28T23:59:59.999Z.

PUT http://localhost:9200/commit
{
    "mappings": {
        "properties": {
            "date": {
                "type": "date",
                "format": "uuuu-MM-dd[['T'][ ]HH:mm[:ss[.[SSS][SS][S]][,[SSS][SS][S]]]][XXXXX][XXXX][X]"
            }
        }
    }
}

PUT http://localhost:9200/commit/_doc/commit20220727?refresh
{
    "date": "2022-07-27T00:00:00.000Z"
}

PUT http://localhost:9200/commit/_doc/commit20220728?refresh
{
    "date": "2022-07-28T00:00:00.000Z"
}

POST http://localhost:9200/commit/_search
{
    "query" : {
        "range" : {
            "date" : { 
                "gte": "2022-07-27T00:00:00.000Z",
                "lte": "9999-12-31T23:59:59.999Z"
            }
        }
    }
}

DELETE http://localhost:9200/commit

Similarly, the following sequence of HTTP requests yields two search hits corresponding to objects with date ranges [2022-07-27T00:00:00.000Z TO 2022-07-27T23:59:59.999Z] and [2022-07-28T00:00:00.000Z TO 2022-07-28T23:59:59.999Z].

PUT http://localhost:9200/commit
{
    "mappings": {
        "properties": {
            "dateTimeRange": {
                "type": "date_range",
                "format": "uuuu-MM-dd[['T'][ ]HH:mm[:ss[.[SSS][SS][S]][,[SSS][SS][S]]]][XXXXX][XXXX][X]"
            }
        }
    }
}

PUT http://localhost:9200/commit/_doc/commit20220727?refresh
{
    "dateTimeRange": {
        "gte": "2022-07-27T00:00:00.000Z", 
        "lte": "2022-07-27T23:59:59.999Z"
    }
}

PUT http://localhost:9200/commit/_doc/commit20220728?refresh
{
    "dateTimeRange": {
        "gte": "2022-07-28T00:00:00.000Z", 
        "lte": "2022-07-28T23:59:59.999Z"
    }
}

Same search and delete requests

Similarly, the following sequence of HTTP requests yields two search hits corresponding to objects with dates 2022-07-27T00:00:00.000Z and 2022-07-28T23:59:59.999Z.

PUT http://localhost:9200/commit
{
    "mappings": {
        "dynamic_templates": [
            {
                "date": {
                    "match": "date_*",
                    "match_mapping_type": "string",
                    "mapping": {
                    "type": "date",
                    "format": "uuuu-MM-dd[['T'][ ]HH:mm[:ss[.[SSS][SS][S]][,[SSS][SS][S]]]][XXXXX][XXXX][X]"
                    }
                }
            }
        ]
    }
}

PUT http://localhost:9200/commit/_doc/commit20220727?refresh
{
    "date_/date": "2022-07-27T00:00:00.000Z"
}

PUT http://localhost:9200/commit/_doc/commit20220728?refresh
{
    "date_/date": "2022-07-28T00:00:00.000Z"
}

Same search and delete requests

But the following sequence of HTTP requests yields zero search hits.

PUT http://localhost:9200/commit
{
    "mappings": {
        "dynamic_templates": [
            {
                "date": {
                    "match": "date_*",
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "date_range",
                        "format": "uuuu-MM-dd[['T'][ ]HH:mm[:ss[.[SSS][SS][S]][,[SSS][SS][S]]]][XXXXX][XXXX][X]"
                    }
                }
            }
        ]
    }
}

PUT http://localhost:9200/commit/_doc/commit20220727?refresh
{
    "date_/date": {
        "gte": "2022-07-27T00:00:00.000Z", 
        "lte": "2022-07-27T23:59:59.999Z"
    }
}

PUT http://localhost:9200/commit/_doc/commit20220728?refresh
{
    "date_/date": {
        "gte": "2022-07-28T00:00:00.000Z", 
        "lte": "2022-07-28T23:59:59.999Z"
    }
}

Same search and delete requests

How can I use dynamic templates, index objects with date ranges, and search with date-range queries?

1 Answer 1

1

The date dynamic template's match mapping type must be object, not string.

PUT http://localhost:9200/commit
{
    "mappings": {
        "dynamic_templates": [
            {
                "date": {
                    "match": "date_*",
                    "match_mapping_type": "object",
                    "mapping": {
                        "type": "date_range",
                        "format": "uuuu-MM-dd[['T'][ ]HH:mm[:ss[.[SSS][SS][S]][,[SSS][SS][S]]]][XXXXX][XXXX][X]"
                    }
                }
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.