Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2013 10:19
Show Gist options
  • Save anonymous/4482696 to your computer and use it in GitHub Desktop.
Save anonymous/4482696 to your computer and use it in GitHub Desktop.
curl -XPUT http://localhost:9200/business_index/business_time_span_slot/_mapping -d '{"business_time_span_slot": {"properties": {"hour_from": {"index": "not_analyzed", "type": "date", "store": "no", "format": "HH:mm:ss"}, "hour_till": {"index": "not_analyzed", "type": "date", "store": "no", "format": "HH:mm:ss"}}}}'
curl -XPUT http://localhost:9200/business_index/business_time_span_slot/1 -d '{"hour_from": "11:10:23", "hour_till": "12:10:23"}'
curl -XPUT http://localhost:9200/business_index/business_time_span_slot/2 -d '{"hour_from": "12:10:00", "hour_till": "12:15:00"}'
curl -XPOST http://localhost:9200/business_index/business_time_span_slot/_search/ -d '{
"filter": {
"and": [
{
"range": {
"hour_from": {
"lte": "10:59:00"
}
}
},
{
"range": {
"hour_till": {
"gte": "10:58:00"
}
}
}]
},
"query": {"match_all": {}}
}'
@clintongormley
Copy link

OK - I think this should be considered a bug. The reason for this is the inconsistent handling of times when no date has been specified. Have a look at the actual values stored for hour_from and hour_till:

curl -XGET 'http://127.0.0.1:9200/business_index/business_time_span_slot/_search?pretty=1'  -d '
{
   "facets" : {
      "hour_from" : {
         "terms" : {
            "field" : "hour_from"
         }
      },
      "hour_till" : {
         "terms" : {
            "field" : "hour_till"
         }
      }
   },
   "size" : 0
}
'

# [Tue Jan  8 11:27:25 2013] Response:
# {
#    "hits" : {
#       "hits" : [],
#       "max_score" : 1,
#       "total" : 2
#    },
#    "timed_out" : false,
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 5,
#       "total" : 5
#    },
#    "facets" : {
#       "hour_from" : {
#          "other" : 0,
#          "terms" : [
#             {
#                "count" : 1,
#                "term" : 43800000
#             },
#             {
#                "count" : 1,
#                "term" : 40223000
#             }
#          ],
#          "missing" : 0,
#          "_type" : "terms",
#          "total" : 2
#       },
#       "hour_till" : {
#          "other" : 0,
#          "terms" : [
#             {
#                "count" : 1,
#                "term" : 44100000
#             },
#             {
#                "count" : 1,
#                "term" : 43823000
#             }
#          ],
#          "missing" : 0,
#          "_type" : "terms",
#          "total" : 2
#       }
#    },
#    "took" : 1
# }

For instance 12:10:00 is stored as 43800000, which is Thu Jan 1 12:10:00 1970.

However, your query is translated to:

curl -XGET 'http://127.0.0.1:9200/business_index/business_time_span_slot/1/_explain?pretty=1'  -d '
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "and" : [
               {
                  "range" : {
                     "hour_from" : {
                        "lte" : "10:59:00"
                     }
                  }
               },
               {
                  "range" : {
                     "hour_till" : {
                        "gte" : "10:58:00"
                     }
                  }
               }
            ]
         }
      }
   }
}
'

# [Tue Jan  8 11:29:13 2013] Response:
# {
#    "ok" : true,
#    "explanation" : {
#       "value" : 1,
#       "details" : [
#          {
#             "value" : 1,
#             "description" : "boost"
#          },
#          {
#             "value" : 1,
#             "description" : "queryNorm"
#          }
#       ],
#       "description" : "ConstantScore(NotDeleted(+cache(hour_from:
# >       [* TO 32535169140999]) +cache(hour_till:[39480000 TO *]))
# >       ), product of:"
#    },
#    "_index" : "business_index",
#    "_id" : "1",
#    "_type" : "business_time_span_slot",
#    "matched" : true
# }

So the lte value is 32535169140999, which is: Wed Dec 31 10:59:00 3000

This is obviously inconsistent. I'm not sure what is required to fix this: whether we need a time field type, or whether we can just consider missing year/month/day to always be Jan 1 1970.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment