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

Solr index fields not showing a field

1  Asked on January 12, 2021 by heather


Programmatically get product name by using order variable

2  Asked on January 7, 2021 by selva


Applying patch on core module failing

0  Asked on January 6, 2021 by nabil


Custom module is not working

1  Asked on December 30, 2020 by ts-guhan


Unlimited field collection pagination in node edit page

1  Asked on December 30, 2020 by dmk-i-t


Error: Private files directory not fully protected

2  Asked on December 28, 2020 by glenviewjeff


How to add Autocomplete to a Views Custom Textfield Filter

1  Asked on December 27, 2020 by laceysanderson


Cannot disable or replace logo

1  Asked on December 25, 2020 by digital-fire


Webform onchange added with hook_form_id_alter

1  Asked on December 25, 2020 by richard-luckhurst


Form API placeholder attribute rendered as “- Select -“

1  Asked on December 24, 2020 by hotwebmatter


Adding a dependencies in theme info file

1  Asked on December 23, 2020 by aryashree-pritikrishna


Javascript in Drupal

1  Asked on December 23, 2020 by zephirus


Titles not showing for tabs pages

2  Asked on December 21, 2020 by andyw


Can’t connect to remote DigitalOcean database

1  Asked on December 18, 2020 by ymdahi


Ask a Question

Get help from others!

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