Skip to main content
3 of 4
added 937 characters in body
x3mxray
  • 3.2k
  • 12
  • 33

Replace

<str name="classname">solr.DirectSolrSpellChecker</str>

with

<str name="classname">solr.WordBreakSolrSpellChecker</str>

in <searchComponent name="spellcheck"> node of solrconfig.xml

rebuild dictionary and request spell checking like:

var results = ctx.GetSpellCheck(new SolrQuery(Term), new SpellCheckHandlerQueryOptions()
{
    SpellCheck = new SpellCheckingParameters()
    {
        Count = 10,
        Build = true,
        Collate = true
    }
});

if (results != null && results.SpellChecking != null && results.SpellChecking.Count > 0)
{
    var words = terms.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    if (words.Length < 2)
    {
        return results.SpellChecking.SelectMany(x => x.Suggestions);
    }

    var correct = "";
    foreach (var w in words)
    {
        var suggest = results.SpellChecking.FirstOrDefault(x => x.Query.Equals(w));
        if (suggest != null && suggest.Suggestions.Any())
        {
            correct += suggest.Suggestions.First() + " ";
        }
        else
        {
            correct += w + " ";
        }
    }
    return new List<string> { correct.Trim() };
}
return new List<string>();

Update

As you mentioned in comments, /spell?q=title:term works only if term is one word. It is not so. When you spell checking by multiple words you can catch error undefined field _text_ because it is default configuration of <initParams path="/spell>. Just relpace initparams for spellchecking in solrconfig.xml with:

  <initParams path="/spell">
    <lst name="defaults">
      <str name="df">title</str>
    </lst>
  </initParams>

Your query to Solr will look similar to

/spell?q=:word1 word2 word3&start=0&rows=5&fl=title&fq=_indexname:(your_index_name)&spellcheck=true&spellcheck.count=5

and it should works fine (it works for me).

enter image description here

I also noticed that your use field name title without any postfix, it is ok if it is your custom configuration. If not, your should use it with postfix like title_t (the same name as you see in solr) in all difinitions in solrconfig.xml.

x3mxray
  • 3.2k
  • 12
  • 33