I was trying to do a Search page but the boost variable setted was not affecting the search results order.
I added some code to assign the score to the weight of each element on the render ready array:
foreach ($results['results'] as $key => $value) {
$variables['search_results']['node'][$key]['#weight'] = 1-$value['score'];
}
inside the template_preprocess_search_api_page_results
function about line 243 of search_api_page.pages.inc
file.
As the weight is higher as lowest number I did it a a correction on the score value.
I don't know if this is a good way to slove the problem but it has to be corrected in order to get the correct order of the search results.
I can try to do a patch if we decide that it is a good solution.
Thanks for the module!
Comment | File | Size | Author |
---|---|---|---|
#4 | 2341319-4--sorting_rendered_entities.patch | 2.21 KB | drunken monkey |
Comments
Comment #1
eidoscomsome bad english! ;)
Comment #2
drunken monkeyIf I understand you correctly, you are saying that the results aren't ordered by score in your search page, if you select the view mode "Themed as search results" – correct?
Then I cannot reproduce this problem, for me this works fine. Do you have any modules installed that could influence this, i.e., alter the search results in some way?
Please describe the steps that lead to reproducing the problem, and what the problem is.
Comment #3
BTMash CreditAttribution: BTMash commentedWhen the view mode is not 'Themed as search results', the sort order seems to get screwy.
The snippet that I used was as a preprocessor in my theme instead:
Which also seemed to do the trick.
Is there something specific going on in the themed as search results view mode that is not in the other view modes?
Comment #4
drunken monkeyAh, OK, so it's the other way round …
I still can't reproduce the problem, but I see now that
entity_view()
doesn't actually guarantee that the returned entity render arrays will be in the same order as the originally passed entities. (Also,entity_load()
doesn't explicitly guarantee to preserve sorting, either, but seems to do it internally nevertheless.)I don't think we should explicitly order by the score there, though, as this might not be what people want (some might be using the Search API Sorts module to apply different sorting). The correct fix seems to me to just loop through the loaded items and apply increasing weitghts to them.
However, we also have to take into account that Search API item IDs might not be the same as the entity IDs.
Also, for me the render array contains a
'#sorted' => TRUE
key, which seems to signify that it's already sorted correctly. So I think we can skip this step if that key is set.Attached is a patch that would implement this, please test whether it fixes your problem!
If not, please find out the reason, which might be:
entity_view()
return value is not ordered the same as the passed entities but still has the'#sorted' => TRUE
keyAlso, in general, please tell me what kind of entities you're displaying, whether you use some modification of the normal "entity view" system, etc. With vanilla Core node viewing it seems to work fine.
Comment #5
gun_dose CreditAttribution: gun_dose commentedIs there any progress in this issue? I see, that there are no sorting in search results at all. And also patch couldn't work properly, because it sorts only entities of one page, but if there are many pages in our results set we will get different entities at every page reload. So I suppose, we need some default sorting, may be even by search api item id - it will be better than no sorting at all.
Comment #6
drunken monkeyThat seems to be an entirely different issue, then. Please report it as a new issue and supply a proper description there.