Rob Stevenson-Leggett
Rob Stevenson-Leggett

Reputation: 35679

Dealing with spell check suggestions

We are trying to use Solr's spell check to do a "Did you mean?" type suggestion.

The problem we are having is that we are replacing the original term in the query with Solr's suggestions.

For example: a search for "10ks" (we are creating an events site) will return a suggestion of "5ks".

However, it seems the spell check is using "ks" rather than "10ks" as the term so when we replace "ks" with "5ks" we get 105ks. This then causes an infinite "did you mean" loop because Solr always uses "ks" rather than "10ks" in the spell check suggestions.

Here's the code that we use to replace suggestions in the original query.

    /// <summary>
    /// Method that takes the first suggestion for all the spelling and applys them to the keyword
    /// </summary>
    private string GetSuggestedQuery(string keyword, List<SpellCheck> suggestions)
    {
        if (suggestions != null)
        {
            for (var i = 0; i < suggestions.Count; i++)
            {
                keyword = keyword.Replace(suggestions.ElementAt(i).Query,
                                          suggestions.ElementAt(i).Suggestions.First());
            }
            return keyword;
        }
        return null;
    }

This works great for two word queries for example "runnig events" would get "running events".

The only thing I can think of is to do something naive like check for spaces in the original query and then replace the whole thing if the query contained spaces.

Upvotes: 0

Views: 559

Answers (2)

Aloke
Aloke

Reputation: 165

Difficult to answer without looking at the field definition from your schema.xml. The Analyzers that will probably work for your case are:

WordDelimiterFilterFactory with split on letter-number transitions set to off (See: http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.WordDelimiterFilterFactory), along with the StandardTokenizerFactory.

Upvotes: 1

Mike R.
Mike R.

Reputation: 558

Look at the spellcheck.collate setting. It will return a re-written query in the way you are suggesting.

https://wiki.apache.org/solr/SpellCheckComponent#spellcheck.collate

Upvotes: 1

Related Questions