My documents have a date field with name published_timestamp. I would like to provide user possibility to filter search results on specific date (excluding time), for example 2020-12-17.
My thought was to add an date input field where user can pick a date and then this date will be sent in searches as filter. But Im not sure if this is supported in search-ui?
I have tried to use Facet for this field, the problem is that It includes the complete timestamp.
I guess what I'm asking is how to add range filter in search-ui?
If you check out the Code Sandbox that is linked in Search UI's README page, it has an example of a range filter on the date_established field. As of the time of this post, it's lines 90-108. I'm going to copy-paste the portion of that code below.
date_established: {
type: "range",
ranges: [
{
from: moment().subtract(50, "years").toISOString(),
name: "Within the last 50 years"
},
{
from: moment().subtract(100, "years").toISOString(),
to: moment().subtract(50, "years").toISOString(),
name: "50 - 100 years ago"
},
{
to: moment().subtract(100, "years").toISOString(),
name: "More than 100 years ago"
}
]
},
You could also have a date input field and have something watching for a change in value and then fire a similar query went the value changes.
Thanks, @Brian_McGue. But the example you are referring to is producing a request with facets field, in stead of filters as described in range filter and it doesn't seem to have any affect on returned result.
What Im trying to say is that I don't want to create prepopulated facet with dates, but provide the user an date input field where he can freely pick an date, and then this date is used to filter search results.
Thanks @JasonStoltz! I have tried to use setFilters as well, but didn't find a way to define filter range in this method. Could you please help me with an example on how to achieve following filter-expression in request body by using setFilters?
Good point, I noticed the documentation doesn't really explain the parameters that setFilter takes. It would be super helpful if we could get TypeScript definitions created for this project... in the meantime I updated the docs.
If you looks at the definition for FilterValue you can see what to pass in here.
Quick follow up question: calling setFilter works as expected (filters search results on specific date) when changing date in date input field, but it seems that the filter is cleared when changing search term in SearchBox. Anything I can do to keep existing filter when changing search term?
Edit: I can fix this by setting the shouldClearFilters to false in SearchBox
Nice. Glad you figured that out. Eventually we'd like to be able to mark filters as "sticky", so you can pick which filters do and do not reset when a new search term is entered.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.