alter Suggestion box for apache solr autosearch

Drupal Answers Asked by Anamika on December 1, 2020

I want to restrict the suggestions for Apache Solr Search with hook_apachesolr_suggestions_alter(&$suggestions, $env_id). I have gone through the code and found the suggestion is prompt from apachesolr_autocomplete_suggest_word_completion()
called by a menu callback.

How can I override the suggestion block? I do not want to block indexing the words but just to hide them from suggestions.

Is there any hook available? Is hook_apachesolr_suggesstions_alter(&$suggestions,$env_id) useful in this case?

I am using hook_apachesolr_querry_alter(DrupalSolrQueryInterface $query);
I want to skip the parameters from the query. Is there any way to do this?

2 Answers

Drupal 7's ApacheSolr module provides the following hook:

function hook_apachesolr_suggestions_alter(&$suggestions, $env_id)

In a custom module, if you implement

[my-custom-module-name]_apachesolr_suggestions_alter($suggestions, $env_id)

You can alter the suggestions array as follows:

function [my-custom-module-name]_apachesolr_suggestions_alter(&$suggestions, $env_id) {
    foreach ($suggestions as $searchTerm) {
        $replacement = $searchTerm->suggestion[0];
        if ($searchTerm = 'test') {
            $replacement = 'testing';
        $suggestions[$searchTerm]->suggestion[0] = $replacement;

If you inspect the arrays via debugging, you'll see the following before your replacement code runs so you just need to alter what it suggests. In this case it was suggesting 'best' when searching for test, so the code above would replace 'best' with 'testing'. You'll probably want to do some optimization for error testing, but this gets you pretty far.

alter suggestion

Answered by kbrinner on December 1, 2020

I was not able to find any hooks for the same. The alternate solution which I got is to implement hook_menu alter.

As apachesolr uses hook_menu() I altered the menu callback function.

Answered by Anamika on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP