Date/Time Parsers
Custom date/time parsers can be specified to allow matches to be made across date/time formats.
The Search Service expects dates to be in the format specified by RFC-3339 , which is a specific profile of ISO-8601. Since Search queries need to specify dates in RFC-3339 format, the dates that are stored in Full Text Indexes also need to be in RFC-3339 format.
A date/time parser tells the Search Service ahead of time what the date/time layout of a field will be. If the Type of a child field is set to datetime
, and the time layout(s) found in that field is not in RFC-3339 format, then you can specify a custom date/time parser that contains the layouts that the Search Service is to expect.
Add Date/Time Parsers
Date/Time Parsers can be specified to allow matches to be made across different formats:
To add the date/time parser
Left click the + Add Date/Time Parser
The Customer Date/Time Parser dialog appears.
Enter a suitable name for the custom parser into the Name field.
Left-click on the + Add button to successively add the layouts for the parser in the interactive field below the Layouts field, by after each one:
This adds the layout to a list of layouts displayed in the Layouts field.
To remove any of these, select its name in the Layouts field, and left-click on the Remove button. When the list is complete, left-click on the Save button to save.
As an example, consider the index created on a travel-sample
bucket. The format of date followed in the reviews.date
field of a document in the hotel collection is-
2006-01-02 15:04:05 +0300
rather than the RFC 339 format, so build a custom date/time parser:
Furthermore, the type mapping section must have an mapping for the reviews.date
field which looks something like this:
To see check the effect the above parser has, run the following query:
curl -XPOST -H "Content-Type: application/json" \
-u <username>:<password> http://localhost:8094/api/index/travel/query -d '{
"query": {
"start": "2001-10-09 10:20:30",
"end": "2016-10-31 18:15:30",
"inclusive_start": false,
"inclusive_end": false,
"field": "reviews.date"
}
}'
Response:
{
"status": {
"total": 1,
"failed": 0,
"successful": 1
},
"request": {
"query": {
"start": "2001-10-09T10:20:30Z",
"end": "2016-10-31T18:15:30Z",
"inclusive_start": false,
"inclusive_end": false,
"field": "reviews.date"
},
"size": 10,
"from": 0,
"highlight": null,
"fields": null,
"facets": null,
"explain": false,
"sort": [
"-_score"
],
"includeLocations": false,
"search_after": null,
"search_before": null
},
"hits": [
...
{
"index": "travel_5af762cb0820e7a2_4c1c5584",
"id": "hotel_10848",
"score": 0.9690093829140931,
"sort": [
"_score"
]
},
{
"index": "travel_5af762cb0820e7a2_4c1c5584",
"id": "hotel_25166",
"score": 0.9624070465826247,
"sort": [
"_score"
]
},
...
],
"total_hits": 833,
"max_score": 1.046790992842069,
"took": 649213,
"facets": null
}
Documentation on using the Go Programming Language to specify layouts is provided on the page Package time. In particular, see the section func Parse.