In views_handler_filter_search.inc the search filter uses $this->query->add_groupby("search_index.sid");. If your search view uses a sort (mine sorts by search score DESC), then function add_orderby adds the sort into the grouping. This has the effect of preventing multiple word searches from working.

The query ends up like

SELECT node.nid AS nid,
    SUM(search_index.score * search_total.count) AS score,
    search_index.score AS search_index_score,
    search_index.*
  FROM node node 
  LEFT JOIN book book ON node.nid = book.nid
  INNER JOIN node node_book ON book.bid = node_book.nid
  LEFT JOIN search_index search_index ON node.nid = search_index.sid
  LEFT JOIN search_total search_total ON search_index.word = search_total.word
  WHERE (node.status <> 0) AND (node.type in ('book')) AND (search_index.word = 'yet' OR search_index.word = 'published') AND (search_index.type = 'node') AND (node_book.nid = 1653)
  GROUP BY search_index.sid, search_index_score
  HAVING COUNT(*) >= 2
  ORDER BY search_index_score ASC

Because my two different words have different search_index_scores, the group by search_index_score prevents the intended results.
If I change the query to just GROUP BY search_index.sid then I get my results.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kleinmp’s picture

I added the following hook into a module and it helped correct this particular search problem:

/**
 * Implementation of hook_views_query_alter().
 */
function MODULENAME_views_query_alter(&$view, &$query) {
  $handler = $view->display_handler->get_option('sorts');
  if($handler['score']['id'] == 'score' && $handler['score']['table'] == 'search_index') {
    $count = 0;
    foreach($query->groupby as $key) {
      if ($key == 'search_index_score') {
        unset($query->groupby[$count]);
      }
      $count++;
    }
  }
}
gagarine’s picture

I don't use a sort but have the same issue if i search words from two differents fields or taxonomy.
The seach filter are my unique exposed filter.

gagarine’s picture

Version: 6.x-2.2 » 6.x-2.3
merlinofchaos’s picture

That hack, while clever, only fixes part of the problem. That sort is completely inappropriate; it sorts by the score of each term, not the aggregated score. So it doesn't actually sort at all. And doesn't work on postgres anyway.

Working on a fix; for right now, the workaround is...don't sort on score. :P

merlinofchaos’s picture

Status: Active » Needs review
FileSize
8.36 KB

Ok, I believe this patch fixes it. It also fixes the score rendering. It also makes search relationship safe, which it was not previously. This includes the patch from #373760: Views 2 Search Term Exposed Filter does not provide results when searched 2+ multiple words as well, even though they're different bugs they affect the same code so having two different patches is a pain. I'm marking that one a dup and continuing all work on that here.

gagarine’s picture

Status: Needs review » Needs work

I quikly test the patch 362830-search-score-totally-broken.patch but they don't solve my problem. If I search two world from two different field i have no result.

merlinofchaos’s picture

Status: Needs work » Needs review

gagarine: Wow, can you be any less descriptive? How about you actually use reasonably troubleshooting and analysis rather than shooting down hours of my work without saying anything of value?

kleinmp’s picture

I tried out the patch and it worked nicely :) Thanks!

merlinofchaos’s picture

Status: Needs review » Fixed

Ok, I identified a couple of interaction problems with the patch. Particularly when using tables and table sort. I think I have it all worked out, and it worked in every situation I tested, so I went ahead and committed it.

If you're having a problem with this continuing to not work after getting the newest code, be sure to include your exact view setup and paste a query to demonstrate it not working.

dww’s picture

Note, I ported the project issue advanced search views to use this functionality and deployed it on d.o. For example:

http://drupal.org/project/issues/search?text=stale

Seems to be working fine. If there are any problems, I'm sure the d.o user-base will flesh them out. ;)

Thanks, Earl!
-Derek

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

xsean’s picture

hi,

i still got this problem where there are no result when i try search multiple word or special character like (, ), - and it not include in the query. when i use core search, there are record found but not with views search-term.

below is my query:
SELECT node.nid AS nid,
SUM(search_index.score * search_total.count) AS score,
node.title AS node_title,
node.type AS node_type,
node_revisions.body AS node_revisions_body,
node_revisions.format AS node_revisions_format,
node_data_field_features.field_features_value AS node_data_field_features_field_features_value,
RAND() AS _random
FROM node node
LEFT JOIN search_index search_index ON node.nid = search_index.sid
LEFT JOIN search_total search_total ON search_index.word = search_total.word
LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
LEFT JOIN content_field_features node_data_field_features ON node.vid = node_data_field_features.vid
WHERE (node.type in ('dining_directory', 'hotels_directory')) AND (node.status <> 0) AND (search_index.word = 'address' OR search_index.word = '123') AND (search_index.type = 'node')
GROUP BY search_index.sid, node_data_field_features_field_features_value, nid, node_title, node_type, node_revisions_body, node_revisions_format, _random
HAVING COUNT(*) >= 2
ORDER BY node_data_field_features_field_features_value ASC, _random ASC

i tried to update with patch but seem the latest version 6x-2.8 already include from the patch.
i'm very appreciate if anyone can let me know what's the problem? all i want the search term can search by multiple word or is good can search using 'LIKE' instead of 'OR'

thanks

franz’s picture

Status: Closed (fixed) » Active

Same issue here. The GROUP BY _random causes the results to be null. Removing it on the query solves the issue.

Not sure about how to fix this on code.

dawehner’s picture

@Franz

Did you tryed the latest version of views and cck?

esmerel’s picture

Status: Active » Closed (fixed)

IF this is still a problem against the most recent release, open a new issue against that release.