Problem/Motivation
In #3551591-14: Filter does not work with date range fields we discovered that the "= Is equal to" and "!= Is not equal to" operators were not working for dates (i.e.: did not match when they should match), because Elasticsearch needs the UNIX timestamps to be encoded as strings in the query.
At the time, @mparker17 did not want to blindly cast $condition->getValue() to a string in \Drupal\elasticsearch_connector\SearchAPI\Query\FilterBuilder::buildFilterTerm() because he worried that solution might break other field types (Decimal, Integer, Latitude/Longitude, etc.). He wanted to be more-selective about when $condition->getValue() was casted to a string.
Steps to reproduce
- Follow the docs to Setup for working on an issue. Log in as an administrator.
- Go to
/admin/modules, and enable Core's Datetime Range (datetime_rangemodule) and its dependencies. - Go to
/admin/structure/types/add, and add anEventcontent type (machine nameevent). - Go to
/admin/structure/types/manage/event/fields/add-field, click Date and time, enter Label =Event date(machine namefield_event_date), Choose a field type =Date range, click theContinuebutton, then click theSavebutton to accept the defaults on the second page. - Go to
/node/add/event, set Title =Workshops, Start date =2026-04-07 09:00:00, End date =2026-04-08 17:00:00, then click theSavebutton. - Go to
/node/add/event, set Title =Sessions, Start date =2026-04-09 09:30:00, End date =2026-04-09 16:30:00, then click theSavebutton. - Go to
/node/add/event, set Title =Sprints, Start date =2026-04-10 09:45:00, End date =2026-04-11 16:15:00, then click theSavebutton. - Go to
/admin/config/search/search-api/add-index, set Index name =Events, Datasources =Content, Server =elasticsearch_server, then click theSavebutton. - Go to
/admin/config/search/search-api/index/events/fields/add/nojs; and click theAddbutton in the Title (title) row and the Event date (field_event_date) row. Click theExpandbutton in the Event date (field_event_date) row, then click theAddbutton in the End date value (field_event_date:end_value) field. Then click theDonebutton. On theManage fields for search index Events, in both thefield_event_daterow andfield_event_date:end_valuerows, set Type =Date. Click theSave changesbutton. - Go to
/admin/config/search/search-api/index/events. Under Start indexing now, click theIndex nowbutton. Wait for the batch job to complete. You should see the Status message Successfully indexed 3 items. - Go to
/admin/structure/views/add. Set View name =Event search(machine nameevent_search). Under View settings, ShowIndex Eventssorted by:Unsorted. Under Page settings, checkCreate a page, and make sure that Path is set toevent-search. Click theSave and editbutton. - You are taken to the view/edit page at
/admin/structure/views/view/event_search. Under Fields, remove all existing fields, then click theAddbutton, and checkTitle (indexed field)in the Content data-source category. Click theAdd and configure fieldsbutton, then click theApplybutton to accept the defaults on the second page. Under Filter criteria, click theAddbutton, and checkEvent datein the Content datasource category. Click theAdd and configure filter criteriabutton, then checkExpose this filter to visitors, to allow them to change it. Also checkExpose operator. Click theApplybutton. Don't forget to clickSaveon the view itself. - Go to
/event-search. You see the Event search view you created. You see 3 results: Workshops, Sessions, and Sprints. - Set Event Date =
Is equal to2026-04-09 09:30:00, then click theApplybutton.
Expected result: You see 1 result, Sessions.
Actual result: You see 0 results - Set Event Date =
Is not equal to2026-04-09 09:30:00, then click theApplybutton.
Expected result: You see 2 results, Workshops, and Sprints.
Actual result: You see 0 results
Proposed resolution
Determine if casting $condition->getValue() to a string will break Decimal, Integer, Latitude/Longitude, etc. results.
Determine how easy it would be to determine the type of $condition->getValue() and cast it when it is a UNIX timestamp.
Remaining tasks
9.0.x merge request- done by @mparker17 in #48.0.x merge request- done by @mparker17 in #5Maintainer review and testing- can skip because the code and tests were written by @mparker17, a maintainerCommunity review and testing, move to RTBC if passes- skipped by @mparker17 in #7Merge to 9.0.x- merged by @mparker17 in #8Merge to 8.0.x- merged by @mparker17 in #10Release 9.0.x- released in 9.0.0-alpha3 by @mparker17Release 8.0.x- released in 8.0.0-alpha7 by @mparker17
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Fixed an issue where the "Is equal", and "Is not equal" filters did not work for Date fields.
Issue fork elasticsearch_connector-3584265
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mparker17Manual testing both string and non-string query values for integer and float fields on the data that gets inserted by
\Drupal\elasticsearch_connector_test\Drush\Commands\ElasticsearchConnectorTestCommands::addSearchApiTestContent()did not change the search results, which is promising.Looking at the JSON specification and the Elasticsearch 9 documentation...
true,false,null, objects, and arrays as values. (i.e.: so Latitude/Longitude should already be encoded as one of those types)true,false) to be passed as strings.nullvalues.Comment #3
mparker17Re-reading the Elasticsearch documentation for term queries, the value in a term query must be a string, i.e.: it cannot be an object or array.
Given that
\Drupal\elasticsearch_connector\SearchAPI\Query\FilterBuilder::buildFilterTermbuilds term queries for Equals and not-Equals...... then I'm satisfied we can always cast
$condition->getValue()to a string in this function.Comment #6
mparker17Created merge request !200 for 9.0.x
Created merge request !201 for 8.0.x
Leaving "needs backport to 8.0.x" tag to remind me to merge both MRs
Moving to Needs review: @sweetchuck, @dewalt, @fathershawn, if you have time for a review, I would greatly appreciate it! If it works, please change the issue status to "Reviewed & tested by the community"!
Comment #7
mparker17I haven't heard back from anyone in 7 days, but I want to make a release with this issue included, so I'm going to merge this change. Thanks everyone.
Comment #9
mparker17Merged to 9.0.x; merging to 8.0.x next.
Comment #11
mparker17Merged 8.0.x now too. I will update this issue when I make a release.
Comment #13
mparker17The changes in this issue were released in elasticsearch_connector-9.0.0-alpha3, and elasticsearch_connector-8.0.0-alpha7