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!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eidoscom’s picture

Issue summary: View changes

some bad english! ;)

drunken monkey’s picture

If 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.

BTMash’s picture

When 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:

function MYTHEME_preprocess_search_api_page_results(&$variables) {
  foreach ($results['results'] as $key => $value) {
    $variables['search_results']['node'][$key]['#weight'] = $value['score'] * -1;
  }
}

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?

drunken monkey’s picture

Status: Active » Needs review
FileSize
2.21 KB

Ah, 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:

  • the loaded items aren't in the same order as the passed IDs (please specify the data source you're using)
  • the entity_view() return value is not ordered the same as the passed entities but still has the '#sorted' => TRUE key
  • something else?

Also, 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.

gun_dose’s picture

Is 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.

drunken monkey’s picture

Title: Score is not affecting on view mode search results » Score is ignored when view mode is not "Themed as search results"

Is 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.

That seems to be an entirely different issue, then. Please report it as a new issue and supply a proper description there.