0

I want to alter the results of a Drupal 8.x search_api_solr search using hook_search_api_solr_search_results():

function sbn_search_api_solr_search_results_alter(\Drupal\search_api\Query\ResultSetInterface $result_set, 
                                                     \Drupal\search_api\Query\QueryInterface $query, 
                                                       \Solarium\QueryType\Select\Result\Result $result) {
 $resultset_data = $result->getData();
  // Get docs in response results
  $results = $resultset_data['response']['docs'];
  foreach ($results as $id => &$item) {
    // get the doc and array id
  }
}

I know that the docs I seek are in the search results:

dsm($result);

returns

stdClass Object
(
    [__CLASS__] => Solarium\QueryType\Select\Result\Result
    [numfound:protected] => 5
    [maxscore:protected] => 1.8644972
    [nextcursormark:protected] => 
    [documents:protected] => Array
        (
            [0] => stdClass Object
                (
                    [__CLASS__] => Solarium\QueryType\Select\Result\Document
                    [fields:protected] => Array
                        (
                            [ss_search_api_id] => entity:node/47:en
                            [ss_search_api_language] => en
                            [score] => 1.8644972
                        )

 ...
    [data:protected] => Array
        (
            [response] => Array
                (
                    [numFound] => 5
                    [start] => 0
                    [maxScore] => 1.8644972
                    [docs] => Array
                        (
                            [0] => Array
                                (
                                    [ss_search_api_id] => entity:node/47:en
                                    [ss_search_api_language] => en
                                    [score] => 1.8644972
                                )

                            [1] => Array
                                (
                                    [ss_search_api_id] => entity:file/85:en
                                    [ss_search_api_language] => en
                                    [score] => 1.784599
                                )

Let's say that the doc in question that I want to remove from $result object is:

$resultset_data['response']['docs'][$id]

How do I alter the $result object to remove this doc from results? Every attempt I have made to access the $result object as an array has resulted in these types of errors:

Error: Cannot use object of type Solarium\QueryType\Select\Result\Result as array 

Error: Cannot access protected property Solarium\QueryType\Select\Result\Result::$response

Any suggestions or documentation on this would be highly appreciated.

0

I have installed search_api, search_api_solr and search_api_attachments. The View that I am reading with the hook is a Solr indexed view returning solr index extracts.

I discovered this was how to read the result rows:

  $result_items = $result_set->getResultItems();
  // Iterate through doc ids
  foreach ($result_items as $docid => $value) {
    // process info from $docid and/or $value
  }

$docid will be the Solr document id, i.e.: entity:node/47:en

In my case, all the information I need, entity type and entity id, are in the document id.

If I want to remove the result row:

unset($result_items[$docid]);

And at the end of the hook:

$result_set->setResultItems($result_items);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.