solrautocompletesolr-schemasolrconfig

Returning single word from Solr Suggester


I am developing a web application, and am using Solr as search engine. I would like to add autocomplete functionality. To do this, I have added the Suggester component, and configured a separate field for it. This works ok.

The problem is that Suggester returns the whole value of the field. For example, if the name of an article is "A newsworthy item" and I search for "new", it will return the whole "A newsworthy item", where I would like it to just return "newsworthy". In other words, return the individual word tokens.

The schema looks like this:

<fieldType name="text_autocomplete" class="solr.TextField" positionIncrementGap="100">
  <analyzer>
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

<field name="term" type="text_autocomplete" indexed="true" stored="true" multiValued="false" />
<field name="weight" type="float" indexed="true" stored="true" />

<copyField source="name" dest="term"/>

The values are copied into the "term" field. The Solr config:

<!-- Search component -->
<searchComponent name="suggest" class="solr.SuggestComponent">
  <lst name="suggester">
    <str name="name">suggester</str>
    <str name="lookupImpl">AnalyzingLookupFactory</str>
    <str name="dictionaryImpl">DocumentDictionaryFactory</str>
    <str name="field">term</str>
    <str name="weightField">weight</str>
    <str name="suggestAnalyzerFieldType">text_autocomplete</str>
    <str name="buildOnStartup">false</str>
  </lst>
</searchComponent>

<!-- Search handler -->
<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
  <lst name="defaults">
    <str name="suggest">true</str>
    <str name="suggest.count">10</str>
    <str name="suggest.dictionary">suggester</str>
    <str name="suggest.build">true</str>
  </lst>
  <arr name="components">
    <str>suggest</str>
  </arr>
</requestHandler>

Can anyone suggest a schema and/or configuration that will make the Suggester return a single word?


Solution

  • Instead of solr.SuggestComponent try making use of solr.SpellCheckComponent. As SuggestComponent is meant to suggest the full phrase. You can look into the details of solr.SpellCheckComponent over here.

    http://wiki.apache.org/solr/SpellCheckComponent

    For you quick reference, you can try with this.

    <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.fst.FSTLookupFactory</str>
        <str name="distanceMeasure">org.apache.lucene.search.spell.JaroWinklerDistance</str>
        <str name="field">term</str>
        <str name="accuracy">0.7</str>
        <float name="thresholdTokenFrequency">.0001</float>
    
    </lst>
    </searchComponent>