The SpellCheck component is designed to provide inline query suggestions based on other, similar, terms. The basis for these suggestions can be terms in a field in Solr, externally created text files, or fields in other Lucene indexes.
Configuring the SpellCheckComponent
Define Spell Check in
The first step is to specify the source of terms in
solrconfig.xml. There are three approaches to spell checking in Solr, discussed below.
IndexBasedSpellChecker uses a Solr index as the basis for a parallel index used for spell checking. It requires defining a field as the basis for the index terms; a common practice is to copy terms from some fields (such as
body, etc.) to another field created for spell checking. Here is a simple example of configuring
solrconfig.xml with the
The first element defines the
searchComponent to use the
classname is the specific implementation of the SpellCheckComponent, in this case
solr.IndexBasedSpellChecker. Defining the
classname is optional; if not defined, it will default to
spellcheckIndexDir defines the location of the directory that holds the spellcheck index, while the
field defines the source field (defined in the Schema) for spell check terms. When choosing a field for the spellcheck index, it's best to avoid a heavily processed field to get more accurate results. If the field has many word variations from processing synonyms and/or stemming, the dictionary will be created with those variations in addition to more valid spelling data.
Finally, buildOnCommit defines whether to build the spell check index at every commit (that is, every time new documents are added to the index). It is optional, and can be omitted if you would rather set it to
DirectSolrSpellChecker uses terms from the Solr index without building a parallel index like the
IndexBasedSpellChecker. This spell checker has the benefit of not having to be built regularly, meaning that the terms are always up-to-date with terms in the index. Here is how this might be configured in
When choosing a
field to query for this spell checker, you want one which has relatively little analysis performed on it (particularly analysis such as stemming). Note that you need to specify a field to use for the suggestions, so like the
IndexBasedSpellChecker, you may want to copy data from fields like
body, etc., to a field dedicated to providing spelling suggestions.
Many of the parameters relate to how this spell checker should query the index for term suggestions. The
distanceMeasure defines the metric to use during the spell check query. The value "internal" uses the default Levenshtein metric, which is the same metric used with the other spell checker implementations.
Because this spell checker is querying the main index, you may want to limit how often it queries the index to be sure to avoid any performance conflicts with user queries. The
accuracy setting defines the threshold for a valid suggestion, while
maxEdits defines the number of changes to the term to allow. Since most spelling mistakes are only 1 letter off, setting this to 1 will reduce the number of possible suggestions (the default, however, is 2); the value can only be 1 or 2.
minPrefix defines the minimum number of characters the terms should share. Setting this to 1 means that the spelling suggestions will all start with the same letter, for example.
maxInspections parameter defines the maximum number of possible matches to review before returning results; the default is 5.
minQueryLength defines how many characters must be in the query before suggestions are provided; the default is 4.
maxQueryFrequency sets the maximum threshold for the number of documents a term must appear in before being considered as a suggestion. This can be a percentage (such as .01, or 1%) or an absolute value (such as 4). A lower threshold is better for small indexes. Finally,
tresholdTokenFrequency sets the minimum number of documents a term must appear in, and can also be expressed as a percentage or an absolute value.
FileBasedSpellChecker uses an external file as a spelling dictionary. This can be useful if using Solr as a spelling server, or if spelling suggestions don't need to be based on actual terms in the index. In
solrconfig.xml, you would define the searchComponent as so:
The differences here are the use of the
sourceLocation to define the location of the file of terms and the use of
characterEncoding to define the encoding of the terms file.
In the previous example, name is used to name this specific definition of the spellchecker. Multiple definitions can co-exist in a single
solrconfig.xml, and the name helps to differentiate them. If only defining one spellchecker, no name is required.
WordBreakSolrSpellChecker offers suggestions by combining adjacent query terms and/or breaking terms into multiple words. It is a
SpellCheckComponent enhancement, leveraging Lucene's
WordBreakSpellChecker. It can detect spelling errors resulting from misplaced whitespace without the use of shingle-based dictionaries and provides collation support for word-break errors, including cases where the user has a mix of single-word spelling errors and word-break errors in the same query. It also provides shard support.
Here is how it might be configured in
Some of the parameters will be familiar from the discussion of the other spell checkers, such as
field. New for this spell checker is
combineWords, which defines whether words should be combined in a dictionary search (default is true);
breakWords, which defines if words should be broken during a dictionary search (default is true); and
maxChanges, an integer which defines how many times the spell checker should check collation possibilities against the index (default is 10).
The spellchecker can be configured with a traditional checker (ie:
DirectSolrSpellChecker). The results are combined and collations can contain a mix of corrections from both spellcheckers.
Add It to a Request Handler
Queries will be sent to a RequestHandler. If every request should generate a suggestion, then you would add the following to the
requestHandler that you are using:
One of the possible parameters is the
spellcheck.dictionary to use, and multiples can be defined. With multiple dictionaries, all specified dictionaries are consulted and results are interleaved. Collations are created with combinations from the different spellcheckers, with care taken that multiple overlapping corrections do not occur in the same collation.
Here is an example with multiple dictionaries:
Spell Check Parameters
The SpellCheck component accepts the parameters described in the table below.
Turns on or off SpellCheck suggestions for the request. If true, then spelling suggestions will be generated.
Selects the query to be spellchecked.
Instructs Solr to build a dictionary for use in spellchecking.
Causes Solr to build a new query based on the best suggestion for each term in the submitted query.
This parameter specifies the maximum number of collations to return.
This parameter specifies the number of collation possibilities for Solr to try before giving up.
This parameter specifies the maximum number of word correction combinations to rank and evaluate prior to deciding which collation candidates to test against the index.
If true, returns an expanded response detailing the collations found. If
The maximum number of documents to collect when testing potential Collations
|spellcheck.collateParam.*||Specifies param=value pairs that can be used to override normal query params when validating collations|
Specifies the maximum number of spelling suggestions to be returned.
Specifies the dictionary that should be used for spellchecking.
Causes Solr to return additional information about spellcheck results, such as the frequency of each original term in the index (origFreq) as well as the frequency of each suggestion in the index (frequency). Note that this result format differs from the non-extended one as the returned suggestion for a word is actually an array of lists, where each list holds the suggested term and its frequency.
Limits spellcheck responses to queries that are more popular than the original query.
The maximum number of hits the request can return in order to both generate spelling suggestions and set the "correctlySpelled" element to "false".
The count of suggestions to return for each query term existing in the index and/or dictionary.
Reloads the spellchecker.
Specifies an accuracy value to help decide whether a result is worthwhile.
Specifies a key/value pair for the implementation handling a given dictionary.
This parameter turns on SpellCheck suggestions for the request. If true, then spelling suggestions will be generated.
This parameter specifies the query to spellcheck. If
spellcheck.q is defined, then it is used; otherwise the original input query is used. The
spellcheck.q parameter is intended to be the original query, minus any extra markup like field names, boosts, and so on. If the
q parameter is specified, then the
SpellingQueryConverter class is used to parse it into tokens; otherwise the
WhitespaceTokenizer is used. The choice of which one to use is up to the application. Essentially, if you have a spelling "ready" version in your application, then it is probably better to use
spellcheck.q. Otherwise, if you just want Solr to do the job, use the
The SpellingQueryConverter class does not deal properly with non-ASCII characters. In this case, you have either to use
spellcheck.q, or implement your own QueryConverter.
If set to true, this parameter creates the dictionary that the SolrSpellChecker will use for spell-checking. In a typical search application, you will need to build the dictionary before using the SolrSpellChecker. However, it's not always necessary to build a dictionary first. For example, you can configure the spellchecker to use a dictionary that already exists.
The dictionary will take some time to build, so this parameter should not be sent with every request.
If set to true, this parameter reloads the spellchecker. The results depend on the implementation of
SolrSpellChecker.reload(). In a typical implementation, reloading the spellchecker means reloading the dictionary.
This parameter specifies the maximum number of suggestions that the spellchecker should return for a term. If this parameter isn't set, the value defaults to 1. If the parameter is set but not assigned a number, the value defaults to 5. If the parameter is set to a positive integer, that number becomes the maximum number of suggestions returned by the spellchecker.
If true, Solr will to return suggestions that result in more hits for the query than the existing query. Note that this will return more popular suggestions even when the given query term is present in the index and considered "correct".
For example, if this is set to 5 and the user's query returns 5 or fewer results, the spellchecker will report "correctlySpelled=false" and also offer suggestions (and collations if requested). Setting this greater than zero is useful for creating "did-you-mean?" suggestions for queries that return a low number of hits.
Specify the number of suggestions to return for each query term existing in the index and/or dictionary. Presumably, users will want fewer suggestions for words with docFrequency>0. Also setting this value turns "on" context-sensitive spell suggestions.
This parameter causes to Solr to include additional information about the suggestion, such as the frequency in the index.
If true, this parameter directs Solr to take the best suggestion for each token (if one exists) and construct a new query from the suggestions. For example, if the input query was "jawa class lording" and the best suggestion for "jawa" was "java" and "lording" was "loading", then the resulting collation would be "java class loading".
The spellcheck.collate parameter only returns collations that are guaranteed to result in hits if re-queried, even when applying original
fq parameters. This is especially helpful when there is more than one correction per query.
This only returns a query to be used. It does not actually run the suggested query.
The maximum number of collations to return. The default is 1. This parameter is ignored if
spellcheck.collate is false.
This parameter specifies the number of collation possibilities for Solr to try before giving up. Lower values ensure better performance. Higher values may be necessary to find a collation that can return results. The default value is
0, which maintains backwards-compatible (Solr 1.4) behavior (do not check collations). This parameter is ignored if
spellcheck.collate is false.
This parameter specifies the maximum number of word correction combinations to rank and evaluate prior to deciding which collation candidates to test against the index. This is a performance safety-net in case a user enters a query with many misspelled words. The default is 10,000 combinations, which should work well in most situations.
If true, this parameter returns an expanded response format detailing the collations Solr found. The default value is false and this is ignored if
spellcheck.collate is false.
This parameter specifies the maximum number of documents that should be collect when testing potential collations against the index. A value of 0 indicates that all documents should be collected, resulting in exact hit-counts. Otherwise an estimation is provided as a performance optimization in cases where exact hit-counts are unnecessary – the higher the value specified, the more precise the estimation.
The default value for this parameter is 0, but when
spellcheck.collateExtendedResults is false, the optimization is always used as if a 1 had been specified.
spellcheck.collateParam.* Parameter Prefix
This parameter prefix can be used to specify any additional parameters that you wish to the Spellchecker to use when internally validating collation queries. For example, even if your regular search results allow for loose matching of one or more query terms via parameters like
mm=20%" you can specify override params such as "
spellcheck.collateParam.q.op=AND&spellcheck.collateParam.mm=100%" to require that only collations consisting of words that are all found in at least one document may be returned.
This parameter causes Solr to use the dictionary named in the parameter's argument. The default setting is "default". This parameter can be used to invoke a specific spellchecker on a per request basis.
Specifies an accuracy value to be used by the spell checking implementation to decide whether a result is worthwhile or not. The value is a float between 0 and 1. Defaults to
Specifies a key/value pair for the implementation handling a given dictionary. The value that is passed through is just
spellcheck.<DICT_NAME>. is stripped off.
For example, given a dictionary called
spellcheck.foo.myKey=myValue would result in
myKey=myValue being passed through to the implementation handling the dictionary
Using Solr's "
bin/solr -e techproducts" example, this query shows the results of a simple request that defines a query using the
spellcheck.q parameter, and forces the collations to require all input terms must match:
SpellCheckComponent also supports spellchecking on distributed indexes. If you are using the SpellCheckComponent on a request handler other than "/select", you must provide the following two parameters:
Specifies the shards in your distributed indexing configuration. For more information about distributed indexing, see Distributed Search with Index Sharding
Specifies the request handler Solr uses for requests to shards. This parameter is not required for the
In case of a distributed request to the SpellCheckComponent, the shards are requested for at least five suggestions even if the
spellcheck.count parameter value is less than five. Once the suggestions are collected, they are ranked by the configured distance measure (Levenstein Distance by default) and then by aggregate frequency.