Select Page

I couldn’t seem to get the Solr Suggester feature to work from the official docs examples so thought I’d document my working example.

In my case I wanted to suggest locations based on data currently within the index.

within solrconfig.xml

 

<searchComponent name=”suggest” class=”solr.SpellCheckComponent”>
<lst name=”spellchecker”>
<str name=”name”>suggest</str>
<str name=”classname”>org.apache.solr.spelling.suggest.Suggester</str>
<str name=”lookupImpl”>org.apache.solr.spelling.suggest.tst.TSTLookup</str>
<str name=”field”>myFieldForAutocomplete</str>
</lst>
</searchComponent>

<requestHandler name=”/suggest” class=”org.apache.solr.handler.component.SearchHandler”>
<lst name=”defaults”>
<str name=”spellcheck”>true</str>
<str name=”spellcheck.dictionary”>suggest</str>
<str name=”spellcheck.count”>10</str>
</lst>
<arr name=”components”>
<str>suggest</str>
</arr>
</requestHandler>

and then schema.xml

 

<types>

<fieldType class=”solr.TextField” name=”text_auto”>
<analyzer>
<tokenizer class=”solr.KeywordTokenizerFactory”/>
<filter class=”solr.LowerCaseFilterFactory”/>
</analyzer>
</fieldType>
</types>

and in schema.xml fields:

 

<field name=”myFieldForAutocomplete” type=”text_auto” indexed=”true” stored=”true” multiValued=”false” />
<copyField source=”city” dest=”myFieldForAutocomplete” />

then its a simple case of building the index with a call to:

http://localhost:4569/solr/myCore/suggest?spellcheck.build=true

after that we can use suggest with:

 

http://localhost:4569/solr/myCore/suggest?q=myQuery

Solr should then give you a nice response like:

 

<response>
<lst name=”responseHeader”>
<int name=”status”>0</int>
<int name=”QTime”>1</int>
</lst>
<lst name=”spellcheck”>
<lst name=”suggestions”>
<lst name=”abe”>
<int name=”numFound”>1</int>
<int name=”startOffset”>0</int>
<int name=”endOffset”>3</int>
<arr name=”suggestion”>
<str>aberdeen</str>
</arr>
</lst>
</lst>
</lst>
</response>
%d bloggers like this: