1. Overview

Solr Suggest component provides facility to auto complete/ auto suggestion for user’s query term.We can use this component to build powerful search application. In this big data era, users don’t know how the actual data is, by solr suggest component we can provide auto compete view facility to our application which gives a better idea about existing indexed data.

In this article, we will discuss configuration, parameters, lookup dictionaries with example.

2. Configuring Suggester/Auto Complete in solrconfig.xml

To enable solr auto complete functionality we need to configure search component and request handler as below.

2.1 Add Search Component

First step to configure suggester in solr is to add search component as below.

<searchComponent name="jdzonesuggest" class="solr.SuggestComponent">
    <lst name="suggester">
      <str name="name">mainSuggester</str>
      <str name="lookupImpl">FuzzyLookupFactory</str>      
      <str name="dictionaryImpl">DocumentDictionaryFactory</str>
      <str name="field">cat</str>
      <str name="weightField">price</str>
      <str name="suggestAnalyzerFieldType">string</str>
      <str name="buildOnStartup">false</str>
    </lst>
</searchComponent>

now let’s have a look at all the parameters of Solr.SuggestComponent.

2.1.1 SearchComponent name

Name of our search component.In above example we created search component with name jdzonesuggest.

2.1.2 Suggester name

A symbolic name of a suggester.We can use this name in any suggest query request under suggest.dictionary parameters.

2.1.3 lookupImpl

Lookup implementations to use for this suggestor.There are several implementations available as below:

  1. AnalyzingLookupFactory
  2. FuzzyLookupFactory
  3. AnalyzingInfixLookupFactory
  4. BlendedInfixLookupFactory
  5. FreeTextLookupFactory
  6. FSTLookupFactory
  7. TSTLookupFactory
  8. WFSTLookupFactory
  9. JaspellLookupFactory

2.1.4 dictionaryImpl

Dictionary implementations to use for this suggestor.There are several implementations available as below.

  1. DocumentDictionaryFactory
  2. DocumentExpressionDictionaryFactory
  3. HighFrequencyDictionaryFactory
  4. FileDictionaryFactory

2.1.5 field

Field that are used as a basis of suggestion terms. Given Field must be store. Generally, people create one copy field and configure that field here.

2.2 Add Request Handler

The second step is to add one request handler and configure previously created search component here.

<requestHandler name="/jdzonesuggest" class="solr.SearchHandler" 
                  startup="lazy" >
    <lst name="defaults">
      <str name="suggest">true</str>
      <str name="suggest.count">10</str>
    </lst>
    <arr name="components">
      <str>jdzonesuggest</str>
    </arr>
</requestHandler>

now let’s have a look at some basic parameters of suggest request handler.

2.2.1 suggest=true

This parameter should always be true, because we always want to run the Suggester for queries submitted to this handler

2.2.2 suggest.dictionary

The name of the dictionary component that configured in suggest component.In our example, it will be like suggest.name=mainSuggestor.We can use more than one suggestor dictionary also.

2.2.3 suggest.q

Need to specify any suggest query over here.

2.2.4 suggest.count

Specificy number of suggestion solr return for given query.In our example, we have set to 10 as a default in our request handler.

2.2.5 suggest.cfq

A Context Filter Query used to filter suggestion based on context field.

2.2.6 suggest.build

This parameter is used to tell solr whether we want to build suggester index or not.Need to build suggester index first time.

2.2.6 suggest.reload

This parameter is used to reload suggester index.

3. Syntax

http://localhost:8983/solr/AutoCompleteExample/{Request_Handler_Name}?
  suggest=true&
  suggest.build=true&
  suggest.dictionary={Suggester_Name}&
  suggest.q={User_Query}

4. Example

In this example we have use only one dictionary, we can use multiple suggestor dictionary also.

http://localhost:8983/solr/AutoCompleteExample/jdzonesuggest?
  wt=json&
  suggest=true&
  suggest.build=true&
  suggest.dictionary=mainSuggester&
  suggest.q=elec

5. Output

{
  "responseHeader":{
    "status":0,
    "QTime":34},
  "command":"build",
  "suggest":{"mainSuggester":{
      "elec":{
        "numFound":3,
        "suggestions":[{
            "term":"electronics and computer1",
            "weight":2199,
            "payload":""},
          {
            "term":"electronics",
            "weight":649,
            "payload":""},
          {
            "term":"electronics and stuff2",
            "weight":279,
            "payload":""}]}}}}

6. Conclusion

In this article we have discussed how to setup and configure solr auto complete functionality using solr suggest component with example.

7. References

Refer Solr Reference Guide for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *