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!

Related Questions

Undefined function in module

0  Asked on December 19, 2021 by idjit


How to get group id from custom url

1  Asked on December 17, 2021 by leopathu


Some fields are not sorting properly in view

1  Asked on December 17, 2021 by weblink


Drush Hangs on wget but wget Works

1  Asked on December 14, 2021 by marshmellow1328


How to get menu title inside menu–main.html.twig file

1  Asked on December 14, 2021 by siddiq


hook_form_alter add field after submit button to exposed view

1  Asked on December 14, 2021 by user7355700


Images uploaded with CKeditor disappear after cron run

1  Asked on December 12, 2021 by alexander-kim


How have multiple instances of a Custom Block?

2  Asked on December 10, 2021 by jagmitg


Node view mode in page template suggestions

1  Asked on December 10, 2021 by sarah-c


How to sort translation under Translate tab?

1  Asked on December 10, 2021


Migration with default values in the yml file

1  Asked on December 10, 2021 by webuser57


menu_get_active_trail returns page not found

1  Asked on December 8, 2021 by jurgenr


hook_field_formatter_view does not get called

1  Asked on December 8, 2021 by alexei-rayu


Ask a Question

Get help from others!

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