1

I'm currently trying to enable spellcheck results with the search api. If I visit my apache solr server directly with http://myserver:8983/solr/d8/select?q=goood+worksd&spellcheck=true and provide the keywords and set spellcheck to true in the url parameters

I receive expected results

{ "response":{"numFound":0,"start":0,"docs":[] }, "spellcheck":{ "suggestions":[ "goood",{ "numFound":1, "startOffset":0, "endOffset":5, "suggestion":["good"]}, "worksd",{ "numFound":1, "startOffset":6, "endOffset":12, "suggestion":["worked"]}]}}

In the response I can see the spellcheck information I need in the spellcheck section.

However when I set the options programmatically before doing the query with the search api I don't receive any of this information.

 $index_id = 'default_solr_index'; $index = \Drupal\search_api\Entity\Index::load($index_id); $query = $index->query(); $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode') ->createInstance('terms'); $parse_mode->setConjunction('AND'); $query->setParseMode($parse_mode); $query->keys($search_terms); $query->setFulltextFields(['title', 'body_field_parse', 'type','field_description','name','body','field_title','field_summary']); $query->addCondition('status', 1); $query->sort('search_api_relevance', 'DESC'); $query->setOption('spellcheck','true'); $executed_query = $query->execute(); var_dump($executed_query->getAllExtraData()); 

When I var_dump($executed_query->getAllExtraData()) all I get is

{ ["search_api_solr_response"]=> array(1) { ["response"]=> array(4) { ["numFound"]=> int(0) ["start"]=> int(0) ["maxScore"]=> float(0) ["docs"]=> array(0) { } } } }

I've done all kinds of variations giving true instead of 'true' and adding other options like

$query->setOption('spellcheck','true'); $query->setOption('spellcheck.build','true'); $query->setOption('search_api_spellcheck','true'); $query->setOption('spellcheck.extendedResults','true'); 

But nothing seems to work. I saw a spellcheck search api module someone posted but I couldn't get it to work and it seemed to do things very similar to how I'm doing it now. Is there a way to pass these parameters to my query to my apache solr? Or is my method correct and getAllExtraData not the proper way to get this information back?

1 Answer 1

1

After digging deep into the search_api_solr module I found a spot where it's applying options to the solr parameters inside the search method of the SearchApiSolrBackend class here

 foreach ($options as $option => $value) { if (strpos($option, 'solr_param_') === 0) { $solarium_query->addParam(substr($option, 11), $value); } } 

I noticed that it only applies the parameter if it's preceded by 'solr_param_' and if it is it strips out the 'solr_param_' before applying it to the actual parameter. All I had to do was add 'solr_param_' to the beginning of my option like so.

$query->setOption('solr_param_spellcheck','true'); 

Once I did that, getAllExtraData() returned what I needed.

array(1) { ["search_api_solr_response"]=> array(2) { ["response"]=> array(4) { ["numFound"]=> int(0) ["start"]=> int(0) ["maxScore"]=> float(0) ["docs"]=> array(0) { } } ["spellcheck"]=> array(1) { ["suggestions"]=> array(16) { [0]=> string(5) "goood" [1]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(11) ["endOffset"]=> int(16) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [2]=> string(5) "goood" [3]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(42) ["endOffset"]=> int(47) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [4]=> string(5) "goood" [5]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(60) ["endOffset"]=> int(65) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [6]=> string(5) "goood" [7]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(93) ["endOffset"]=> int(98) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [8]=> string(5) "goood" [9]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(111) ["endOffset"]=> int(116) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [10]=> string(5) "goood" [11]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(129) ["endOffset"]=> int(134) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [12]=> string(5) "goood" [13]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(154) ["endOffset"]=> int(159) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } [14]=> string(5) "goood" [15]=> array(4) { ["numFound"]=> int(1) ["startOffset"]=> int(182) ["endOffset"]=> int(187) ["suggestion"]=> array(1) { [0]=> string(4) "good" } } } } } }

Not entirely sure why I have so many duplicate results here so if anyone has input on that that would be helpful. Otherwise I do have the data I need.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.