Doing a multi word search (e.g. "Drupal Modules", but not "Drupal") results in incorrect counts in the search_api_facets array. Actually it always returns 1 in total (that is 1 in count and 1 item in each type.) I had a "content type" facets turned on while doing this.

The reason for this is how the SQL Query looks, after being changed on lines 1085-1087:

    $group_by = &$db_query->getGroupBy();
    $group_by = array();

After emptying the GROUP BY, the HAVING COUNT(DISTINCT t.word) will not function as intended. This is because if it's not grouped by t.item_id, only 1 result is possible.

The solution could possibly be to remove these 2 lines, but I'm not sure what the removing of GROUP BY is intended to produce. If it's needed we have to remove the GROUP BY and then add the GROUP BY t.item_id to the DB Select Object, if there are multiple words in the search. On possible solution could then be to replace the above lines with:

    // Remove group by because of the reason... 
    $group_by = &$db_query->getGroupBy();
    $group_by = array();
    // Count the number of words in the search query
    $keys = $query->getKeys();
    $word_count = 0;
    foreach ($keys as $i => $key) {
      if (!element_child($i)) {
        continue;
      }
      if (is_scalar($key)) {
        $word_count++;
      }
    }
   // If multi word search, then group by t.item_id.
    if ($word_count > 1) {
      $db_query->groupBy('t.item_id');
    }

Sorry, if there's any confusion in how I interpret what is actually happening in this portion of the code.

Comments

Hitby’s picture

I'm having this issue as well - What file are you changing this code in? Did you manage to figure it out? Thanks

edit: never mind, I have found it and edited service.inc. It seems to be working correctly. Is there any reason I shouldn't be doing this and how can we get this rolled into the dev version?

Cheers

Hitby’s picture

Actually, although it does seem to fix the wrong count issue it throws the following error if there is less than 2 keywords (including 0)

Warning: Invalid argument supplied for foreach() in SearchApiDbService->getFacets() (line 1083 of...

Hitby’s picture

AS you say, removing

$group_by = &$db_query->getGroupBy();
$group_by = array();

seems to fix the issue without adding any of the extra code...

drunken monkey’s picture

Status: Active » Needs review
StatusFileSize
new430 bytes

Thanks for reporting this, and for even tracking it down to the root of the issue!

Seeing this, I probably removed the GROUP BYs because we also remove all expressions and I thought we then wouldn't need GROUP BYs, either. However, when there are HAVING clauses, I guess this supposition is flawed.

After a bit of testing, I can't find anything wrong with just removing those two lines. Patch attached.

gtlitc’s picture

Version: 7.x-1.0-beta1 » 7.x-1.0-beta3
Status: Needs review » Reviewed & tested by the community

Patch in comment #4.

We patched ‘7.x-1.0-beta3’ rather than ‘7.x-1.0-beta1’ as indicated in the original post.
We have reviewed this and it’s working for us.
Thanks for the patch.

+1 RBTC

Hitby’s picture

Hi, Will this patch be in beta4?

drunken monkey’s picture

Version: 7.x-1.0-beta3 » 7.x-1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed. Thanks again for providing this detailed bug report and analysis, and thanks @ gtlitc for testing!

@ Hitby: No, Beta 4 is already out, but it will be in the next release now (Beta 5 or RC 1).

Status: Fixed » Closed (fixed)

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

lesliewagner1165’s picture

I am using version 7.x-1.3 and currently having this same issue. I assume this patch has been added by now? If not could I get some very explicit directions about adding it? (I have very little knowledge of coding!) Or any other suggestions for fixing the problem?

Thank you!