Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've added 'search:search terms' as the 'filter' criteria for the a view but if I type in more then one word no results are shown. eg if I type in 'summer' results with summer show but if I type in 'summer wedding' no results show.
I am using this filter criteria on another site and it works fine. Is there a setting I need to set for it to work. The search is using drupal default search index and the block with this search is also working fine. How to I make it work on the view for more then one word?
Thanks
Comment | File | Size | Author |
---|---|---|---|
#9 | views_search_index_multiple_terms-1948510-7676455.patch | 888 bytes | pdcarto |
Comments
Comment #1
alanpeart CreditAttribution: alanpeart commentedSame problem here. No solution found yet but will post if I find it. Same search works fine using Drupal default search but fails when using views keyword search.
Comment #2
alanpeart CreditAttribution: alanpeart commentedIt appears that views is incorrectly adding a Group By clause to group on the "score" field when using this filter. I can't find where this is happening, since it is not visible at any of the normal points where I might debug a views query, such as hook_views_query_alter() or hook_views_pre_execute(). Also, since it's no longer possible to modify the raw SQL of a views query, I can't just remove it using a string replacement which was one of my moves of desperation in Drupal 6.
I'll keep trying to track it down but it would make my day if a views expert could jump in and identify how this extra grouping field is getting added.
My query as shown in views preview is:
The part that is added that is messing up the results is the "score" field in the GROUP BY clause. There's no reason to group by this field since it's involved in a SUM expression in the SELECT clause.
Comment #3
alanpeart CreditAttribution: alanpeart commentedOK, I think I have found the problem. The bug is in views/modules/search/views_handler_filter_search.inc on line 153:
$this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
There are actually two bugs here for the price of one, but you don't find the second until you resolve the first! First of all, the 3rd variable being sent to add_field is the name of the field - in this case, because it's a formula this name, 'score', is an alias. However, this causes a problem for the default views query plugin, because there's already a field in search_index called 'score'. Because this query has aggregation, it thinks it's being asked to group on the 'score' field in search_index, so it throws the wrong results.
I tested this, eliminating the naming confusion by changing the line to:
$this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'aggscore', array('aggregate' => TRUE));
This is when I encountered the second bug. I received the error message "SQLSTATE[42000]: Syntax error or access violation: 1056 Can't group on 'aggscore'".
Eventually I tracked this down to the function compile_fields in views_plugin_query_default.inc. It turns out that if a field does not have a table name associated with it, it is assumed that it is a formula, and it is furthermore (incorrectly I believe) assumed that grouping is required for this field. So it tries to group on our field that already has a SUM operation being performed.
The simple workaround to fix this behaviour is to add a table name when adding the field. We have two tables in the formula so let's pick one:
$this->search_score = $this->query->add_field('search_total', "SUM($search_index.score * $search_total.count)", 'aggscore', array('aggregate' => TRUE));
Now the field is identified correctly as one that should be excluded from grouping. This fixed my problem. It seems to me that a patch would be useful here but honestly, I'm not sure if fixing this one line of code is the correct way of resolving the underlying problem. Very interested in anyone else's comments and whether this fix also resolves the OP's issue.
Comment #4
LTech CreditAttribution: LTech commentedThanks so much. Your code fix worked for me.
Comment #5
LTech CreditAttribution: LTech commentedComment #6
LTech CreditAttribution: LTech commentedI've re-opened this bug because I am still having issues with it.
I have a painting called 'with an open heart' on my site. If I search for 'with an open heart' nothing comes up but if I search for 'with open heart' something comes up.
I want to use the drupal main search filter with my view. I thought search:serach terms filter was the main search index but I am getting different results eg 'with an open heart' works on the main search filter for the site.
How do I use the main search filter but be able to display the results in a nice grid and have extra filters for advanced searching - like I can built easily with a view?
Thanks so much
Comment #7
LTech CreditAttribution: LTech commentedIt seems one of my other filters was not allowing the search:search term filter to work correctly. On a fresh view it works as designed.
Comment #8
pdcarto CreditAttribution: pdcarto commentedFor anyone who doesn't want to hack views or wait for this to be fixed in a release, this can be fixed with a hook_views_query_alter function as follows (be sure to change the function name to reflect the name of the module you are putting it in, save, and clear cache):
Comment #9
pdcarto CreditAttribution: pdcarto commentedAnd here's a patch against current 7.x-3.x-dev. I found that it was unnecessary to rename 'score' as 'aggscore'. Simply substituting 'search_total' for the blank table name was sufficient.
Comment #10
jjchinquist CreditAttribution: jjchinquist commentedA big thank you on that patch. It works for me and still applies cleanly to the newest DEV version.
Tested OK on our DEV server copy (1::1 mirror of the prod server). I do not see any issues. We plan to install views-dev and this patch on the production site tonight to fix the issue.
Please apply the patch. Thanks.
Comment #11
rickmanelius CreditAttribution: rickmanelius commentedThe patch in #9 applied cleanly but did not provide the intended functionality for 7.x-3.7. I realize that #9 and #10 state this is against the -dev branch, so in the meantime I'll continue to use the quick fix in #8, which works for the stable version.
Looking forward to seeing this committed!
Comment #12
jjchinquist CreditAttribution: jjchinquist commentedThere have been many commits to views since the last release. It cannot be used on that version.
Comment #13
rogernyc CreditAttribution: rogernyc commentedGuess maybe this has been addressed in Views 7.x-3.7 ?
Checking 'Remove search score' in my 'Search: Search Terms' exposed filter got things working with 2 or more search terms...
Comment #14
phenaproximaThe fix in #8 worked for me with 7.x-3.7.
Comment #15
RoloDMonkey CreditAttribution: RoloDMonkey commentedComment #16
studiozut CreditAttribution: studiozut commentedI tossed the code from pdcarto's #8 above into a module: https://github.com/gwu-libraries/views-patch (attributed in the .info)
I'm on drupal 7.27 with views 7.x-3.8, seems to have improved my results, but they still don't match the default site search and now getting this when I run cron:
Notice: Uninitialized string offset: 0 in search_index() (line 604 of /var/www/modules/search/search.module).
Notice: Undefined index: in search_index() (line 629 of /var/www/modules/search/search.module).
Comment #17
RoloDMonkey CreditAttribution: RoloDMonkey commentedBased on the information in #16, I think this needs more review.
Comment #18
happysnowmantech CreditAttribution: happysnowmantech commentedClosing as a duplicate of Search terms: nodes dont appear when using multiple arguments. That issue also has some additional patches.
Comment #19
CD CreditAttribution: CD commentedThanks rogernyc,
This just got it working for me too:
Guess maybe this has been addressed in Views 7.x-3.7 ?
Checking 'Remove search score' in my 'Search: Search Terms' exposed filter got things working with 2 or more search terms...
On Views 7.x-3.13
Comment #20
jebeze_alexander CreditAttribution: jebeze_alexander commentedAdvice in #13, checking 'Remove search score' in my 'Search: Search Terms' exposed filter fixed the issue for me as well.
Drupal 7.23; Views 7.x-3.7, and also using Better Exposed Filters 7.x-3.2.
Comment #21
grahamvalue CreditAttribution: grahamvalue commentedHello studiozut,
Those log entries now seem to occur whenever cron runs after a comment is created or updated.
Were you ever able to resolve them?