You can filter your search results by date. This is useful if you want to find all the documents that were created or modified in a specific time period.
Setup a date field
To filter by date, you need to add a date field to your index. You can do this by adding a date field to your schema.
Indexing the following document into Elasticsearch will create a date field called date
:
{
"date": "2015-01-01"
}
and your mapping will look like this:
{
"properties": {
"date": {
"type": "date"
}
}
}
Filter Attribute Configuration
Add a filter attribute to your index configuration. The filter attribute will be used to filter your search results by date.
{
"filter_attributes": [
{ attribute: 'date', field: 'date', type: 'date' },
]
}
NumericFilters Usage
If you want to filter by date using numericFilters
, for example having a component adjusting the filters, you can use the following syntax:
numericFilters: [
`availabilities.start<=${(new Date("2015-01-01")).getTime())}`,
`availabilities.end<=${(new Date("2019-01-01")).getTime())}`
]
See camping-sites-demo for an example of a date numeric filter component example.
Using Filter Attributes
You can use the filter attribute in your search query to filter your search results by date within configure
.
Syntax Examples
"date:2015-01-01" # date is 2015-01-01
"date:[2015-01-01 TO 2015-12-31]" # date is between 2015-01-01 and 2015-12-31
"date:[2015-01-01 TO *]" # date is greater than 2015-01-01
"date:[* TO 2015-12-31]" # date is less than 2015-12-31
With configure
client.configure({
filters: 'date:[2015-01-01 TO 2015-12-31]'
})