Problem/Motivation
When I add a facet based on a boolean field, I only see the "TRUE" value facet back in the interface.
Steps to reproduce
- Set up a local 8.0.x environment for development
- Go to
/admin/structure/types/add and add a Movie content type. Mine had the machine name movie. Click the Save button.
- Go to
/admin/structure/types/manage/movie/fields/add-field. Under Choose a type of field, click Boolean. Set Label = Closed captioning (machine name, field_closed_captioning), then click the Continue button. Click the Save button on the second page to ensure accept the defaults.
- Go to
/node/add/movie. Set Title = The Gold Rush, Closed captioning = FALSE (i.e.: unchecked), then click the Save button.
- Go to
/node/add/movie. Set Title = MacGyver, Closed captioning = TRUE (i.e.: checked), then click the Save button.
- Go to
/admin/config/search/search-api/add-index. Set Index name = Movies; Datasources = Content, Server = elasticsearch_server, click Save
- Go to
/admin/config/search/search-api/index/movies/fields/add/nojs, and click the Add button in the title and field_closed_captioning rows. Then click the Done button. Onthe next page, ensure the field_closed_captioning row has Type = Boolean. Click the Save change button.
- Go to
/admin/config/search/search-api/index/movies. Click the Index now button. You should see the Status message Successfully indexed 2 items.
- Go to
/admin/structure/views/add. Set View name = Movie search. Under View settings, "Show Index movies sorted by Unsorted". Under Page settings, check Create a page, and ensure Path = movie-search. Click the Save and edit button.
- You are taken to the View-edit page. Under Fields, you should already see Content datasource: Closed captioning (indexed field). Next to Fields, click Add. In the popup, check
Title (indexed field) in the Content data-source category. Click the Add and configure fields button. Click the Apply button on the second page to accept the defaults. Click the Save button on the View-edit page. You should see the Status message The view Movie search has been saved..
- Go to
/admin/config/search/facets/add-facet. Set Facet source = View Movie search, display Page. Set Field = Closed captioning (field_closed_captioning). Set Name = Closed captioning (machine name closed_captioning. Click the Save button.
- Go to
/admin/structure/block/library/stark?region=content. Click the Place block button in the Closed captioning row, Facets category. Click the Save block button on the second page to accept the defaults. You are taken back to the Block layout page; click the Save blocks button at the bottom. You should see the Status message The block settings have been updated.
- Go to
/movie-search.
Expected behavior: You see a Closed captioning facet with links to 0 and 1.
Actual behavior: You see a Closed captioning facet with links to ! and 1. You see search results for The Gold Rush and MacGyver.
- Assuming that
! refers to the "FALSE" value, in the Closed captioning filter, click the ! option. The page reloads.
Expected behavior: You see 1 search result, The Gold Rush.
Actual behavior: You see 1 search result, MacGyver.
Proposed resolution
The facet parser should be changed. It checks on an empty() statement. However the value we receive back from ES is 0. This is considered empty and therefore discarded.
2 solutions:
Different check:
'filter' => (!isset($value['key']) || $value['key'] === '') ? '!' : \sprintf('"%s"', $value['key']),
Or change the default value to 0.
'filter' => empty($value['key']) ? '0' : \sprintf('"%s"', $value['key']),
The patch in the attachement uses solution 1 since I do not know if the "!" is used for something specific.
Remaining tasks
Write a merge request for 8.0.x - merge request !94 created in #4 by @mparker17 with @marckwee's code
Write a merge request for 9.0.x - merge request !194 created by @mparker17 in #11
Add tests - added by @mparker17 in #10
Community review - reviewed by @fathershawn in #13
Maintainer review - done by @mparker17 in #7
Commit to 9.0.x - done by @mparker17 in #15
Commit to 8.0.x - done by @mparker17 in #17
Release 9.0.x - released in 9.0.0-alpha3 by @mparker17
Release 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 FALSE values would not show for boolean facets.
Comments
Comment #2
marckwee commentedPatch to solve issue above using method 1
Comment #5
mparker17@marckwee, thank you for the contribution!
I committed your code to a merge request to make it easier for me to review.
I took a very quick look at your patch: the changes seem clear to me, but I haven't yet had a chance to run the changed code... I may have more feedback when I do.
I didn't see any automated tests in your patch... the Elasticsearch Connector module maintainers prefer to accept merge requests that have passing automated tests. Automated tests ultimately benefit you, because they ensure that future changes to this module (i.e.: by other people) will not break the functionality that your site depends on!
I don't think that we have any automated tests for Facets yet (I wrote a manual test but haven't had the time to turn it into code yet).
Are you interested in learning how to write an automated test?
If you describe how to run a test manually, then I may be able to suggest how to automate it (i.e.: if you're interested in learning to write a test), or write a test myself (if you aren't interested in learning to write a test).
Comment #6
marckwee commentedHi MR Parker,
I wouldn't mind to write tests for the issues. However I am a bit lost with using MR/PR with Drupal instead of patches + what kind of tests to write. Can you give me some pointers.=)
Comment #7
mparker17@marckwee, apologies for the delay in getting back to you! I'm happy to help how I can!
I can't see any metadata in your patch file to indicate how you created it, but I found that the merge-request workflow was pretty similar to how I was generating patches before, and it was also pretty similar to how my teams were working on client sites, so it was an easy transition for me. (hopefully those anecdotes are encouraging?)
At a high level, in the MR-based workflow, you:
git diffagainst the main branch to generate the patch fileFor more-detailed documentation, see the guide on Using GitLab to contribute to Drupal. The part of the guide about Creating issue forks explains the d.o issue UI, with screenshots (which I found helpful!)
Please let me know if you have any questions! (I'll try to reply to you more-quickly in the future!)
Comment #8
mparker17(updated the status (Active -> Needs work), and updated the version to 8.0.x as this issue applies to the dev branch as well)
Comment #9
mparker17Briefly checking the status of this issue after releasing 8.0.0-alpha5...
Comment #10
mparker17I added a comment, wrote an automated test, and I have updated the issue summary and added steps to reproduce.
@marckwee if you'd be willing to test the latest patch, I would very much appreciate it! (And, if it works, please change this issue's status to "Reviewed and tested by the community")
Thank you very much for your patience with me!
Comment #12
mparker17(Created a merge request for the 9.0.x branch, and tagged a reminder so that I don't forget to merge it to 8.0.x)
Comment #13
fathershawnThe code change is a well structured and targeted replacement for
empty()which would have screened outfalseComment #14
mparker17Thank you @fathershawn for the review!
Comment #16
mparker17Merged to 9.0.x; merging to 8.0.x next.
Comment #18
mparker17Merged to 8.0.x. I'll update the issue when this issue is released.
Comment #20
mparker17The changes in this issue were released in elasticsearch_connector-9.0.0-alpha3, and elasticsearch_connector-8.0.0-alpha7