The expected behavior of "contains any word" would be to match the whole word, not just a part of the word. For example, if you were trying to filter for 'foo' and 'bar', one would not expect results to include 'football' and 'barbie'.
Though I realize that fixing this expectation might cause some issues for people who were happy to get partial results, I think a better solution would be to add a "contains any matching string" filter handler. Though my patch will simply focus on the desired behavior of the current handler, I'm happy to add the additional handler if you think it would be helpful.
Thanks for your consideration.
Comments
Comment #1
a-fro commentedComment #2
a-fro commentedAny update on this patch? I'm very interested to know whether it will be considered. Thanks.
Comment #3
a-fro commentedComment #4
a-fro commentedOk, given that 1) my last patch returned crickets and 2) I don't think it worked, trying a different approach that I believe actually fixes the issue.
Comment #5
robgreeniowaI just wanted to say thanks for your work on this. I am dealing with the same weirdness in my university museum's new Drupal site, and it really surprised me to find out that (1) by default, Views filters return 'football' on a 'foo' search and that (2) there's not a way to change that! Seems like a no-brainer that Views would offer an option to 'require whole word'. So thanks again for helping to make this happen.
Comment #6
j0e commentedThis would be great for contains any word and for contains all words operators (whether exposed or not), so that we can specify to match whole words and not partial strings.
For example, searching an exposed filter title for men is returning results for women, so it is certainly a problem.
When I try your patch for contains any word, i'm getting the error:
Notice: Undefined variable: string in views_handler_filter_string->op_word()
Thanks for giving this some attention!
Comment #7
a-fro commentedOops. Sorry about that. Please try this updated patch.
Comment #8
j0e commentedthis worked well on regular single field filters containing all or any words...
i tried it on combined fields filter and couldn't get it to work there... i'm assuming some similar patch needs to apply to views_handler_filter_combine.inc to get it to work there. i tried myself hacking the file a bit based on your patch but couldn't get it to work... perhaps you can take a look, thanks so much...
Comment #9
hockey2112 commentedPatch in #7 worked for me. Thanks!
Comment #10
elpino commentedPatch works great!
But I was wondering if it could be added as separate handler or an extension to the class via a custom module.
Because unless everyone agrees this should be the new behaviour the patch won't get commited, and having to remember to repatch everytime views gets updated is not an option.
Comment #11
gadaniels72 commentedPatch #7 worked for me. When filtering on a taxonomy view containing these terms: "child care", "a car loan", "day care", filtering for car returns just a car loan and filtering for care returns just child care and day care.
When I first tried "contains any word," this is the results I expected. If I wanted just a partial match, the contains filter would have handled that case just fine. I could not see a difference between "contains any word" and "contains" prior to installing this patch.
Comment #12
riddhi.addweb commentedComment #13
robertom commentedattached a modified patch that add this capability also for 'combine' filters.
Comment #14
stephen ollmanIs it at all possible to do this at the theme level by overriding the function?
I've also tried placing ([[:<:]]|^)([[:>:]]|$) into the view regular expression filter, but I'm not sure how to get the search words.
If I add the combined filter value to the regex string it works a treat without the patch, but I'm having trouble passing the combined value dynamically in the to regex string.
Example that works well:
([[:<:]]|^)foo([[:>:]]|$) - works for all instances of 'foo' but not 'food' which is correct.
Question is, is there a way of dynamically getting the value of 'foo'?
I've tried ([[:<:]]|^)!1([[:>:]]|$) and ([[:<:]]|^)%1([[:>:]]|$), but neither work.
Thoughts?
Comment #15
bbcDespite its old age, patch #13 applied cleanly and works as advertised. Thanks @robertom and @a-fro!
Comment #16
christoforos korifidis commentedIs there a version of the above module for Drupal 8?
Comment #17
wombatbuddy commentedSince the issue is relevant for Drupal 8, I've duplicated it here: https://www.drupal.org/project/drupal/issues/3173367