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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 1403916--facets_with_groupBy-4.patch | 430 bytes | drunken monkey |
Comments
Comment #1
HitbyI'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
Comment #2
HitbyActually, 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...Comment #3
HitbyAS you say, removing
seems to fix the issue without adding any of the extra code...
Comment #4
drunken monkeyThanks 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 needGROUP BYs, either. However, when there areHAVINGclauses, 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.
Comment #5
gtlitc commentedPatch 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
Comment #6
HitbyHi, Will this patch be in beta4?
Comment #7
drunken monkeyCommitted. 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).
Comment #9
lesliewagner1165 commentedI 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!