Apply Date Filtering on Search API
This use case covers how to apply a date filter via the Search API. We use the publish_date custom field sent in the feed to apply the filter.
Features used
We suggest that you go through the below guides to better understand this use case:
How to implement use case
Follow the steps given below:
1. Send formatted custom date attribute in product feed
Ensure the dates are sent in an accepted numeric format. This ensures that the date filtering works correctly. Suppose the custom date attribute is publish_date, and its value is April 11, 2024. The following formats are acceptable:
- YYMMDD format - “publish_date” : 20240411
- Unix timecode - “publish_date” : 1712786400
Reserved attributes
Avoid using reserved attributes like
launch_date
andstart_date
. Custom attributes are better suited for this use case and allow greater flexibility.
2. Apply date filter (fq) on Search API
Use the filter query parameter (fq) with the appropriate date field and specify the date you wish to filter by.
&fq = publish_date:1712786400
Here, we are using Unix timecodes, but you may use the YYMMDD format instead. The date format used in the filter query must match the format used in the product feed to avoid discrepancies
Ranged date filtering
For a promotion campaign, we want a filtered result set that shows newly launched products between April 11 and April 21, 2024. We pass the fq parameter with this date range in the syntax below:
&fq = publish_date:[1712786400 TO 1713650400]
For contextual date filtering, you can also combine publish_date with a finish_date custom attribute.
&fq = publish_date:[* TO ${TODAY}]
&fq = finish_date:[${TODAY} TO *]
Here * represents any value.
Updated 4 months ago