Reputation: 435
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?
Upvotes: 1
Views: 262
Reputation: 435
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]"
}
}
}
]
}
}
Upvotes: 1