Apache Solr

Solr Spellcheck Example

In this example of Solr Spellcheck, we will discuss about how to implement the spellcheck functionality provided by Apache Solr. We will show you how to configure the spellcheck in Solr and discuss the query parameters used to get the spellcheck suggestions.

To demonstrate the Solr Spellcheck example, we will create a core in Solr using basic configuration and index a sample file shipped along with Solr installation.

Our preferred environment for this example is solr-5.0.0. Before you begin the Solr installation make sure you have JDK installed and Java_Homeis set appropriately.
 

1. Install Apache Solr

To begin with, lets download the latest version of Apache Solr from the following location:

http://lucene.apache.org/solr/downloads.html

Apache Solr has gone through various changes from 4.x.x to 5.0.0, so if you have a different version of Solr you need to download the 5.x.x. version to follow this example.

Once the Solr zip file is downloaded, unzip it into a folder. The extracted folder will look like the below:

solr_folder
Solr folders

The bin folder contains the scripts to start and stop the server. The example folder contains few example files. We will be using one of them to demonstrate how Solr indexes the data. The server folder contains the logs folder where all the Solr logs are written. It will be helpful to check the logs for any error during indexing. The solr folder under server holds different collection or core. The configuration and data for each of the core/ collection are stored in the respective core/ collection folder.

Apache Solr comes with an inbuilt Jetty server. But before we start the solr instance we must validate the JAVA_HOME is set on the machine.

We can start the server using the command line script. Lets go to the bin directory from the command prompt and issue the following command:

solr start

This will start the Solr server under the default port 8983.

We can now open the following URL in the browser and validate that our Solr instance is running. The specifics of solr admin tool is beyond the scope of the example.

http://localhost:8983/solr/

Solr admin console
Solr admin console

2. Configuring Apache Solr

In this section, we will show you how to configure the core/collection for a solr instance and how to define the fields. Apache Solr ships with an option called Schemaless mode. This option allow users to construct effective schema without manually editing the schema file. But for this example we will use the Schema configuration for understanding the internals of the Solr.

When the Solr server is started in Standalone mode, the configuration is called core and when it is started in SolrCloud mode, the configuration is called Collection. In this example we will discuss about the standalone server and core. We will park the SolrCloud discussion for later time.

First, we need to create a Core for indexing the data. The Solr create command has the following options:

  • -c <name> – Name of the core or collection to create (required).
  • -d <confdir> – The configuration directory, useful in the SolrCloud mode.
  • -n <configName> – The configuration name. This defaults to the same name as the core or collection.
  • -p <port> – Port of a local Solr instance to send the create command to; by default the script tries to detect the port by looking for running Solr instances.
  • -s <shards> – Number of shards to split a collection into, default is 1.
  • -rf <replicas> – Number of copies of each document in the collection. The default is 1.

In this example we will use the -c parameter for core name and -d parameter for the configuration directory. For all other parameters we make use of default settings.

Now navigate to the solr-5.0.0\bin folder in the command window and issue the following command:

solr create -c jcg -d basic_configs

We can see the following output in the command window.

Creating new core 'jcg' using command:
http://localhost:8983/solr/admin/cores?action=CREATE&name=jcg&instanceDir=jcg

{
 "responseHeader":{
 "status":0,
 "QTime":663},
 "core":"jcg"}

Now we navigate to the following URL and we can see jcg core being populated in the core selector. You can also see the statistics of the core.

http://localhost:8983/solr

Solr jcg core
Solr jcg core

3. Modify the schema.xml file

We need to modify the schema.xml file under the folder server\solr\jcg\conf to include the fields. We will use one of the example file “books.csv” shipped along with Solr installation for indexing. The file is located under the folder solr-5.0.0\example\exampledocs.

Now we navigate to the folder server\solr directory. You will see a folder called jcg created. The sub-folders namelyconf and data have the core’s configuration and indexed data respectively.

Now edit the schema.xml file in the \server\solr\jcg\conf folder and add the following contents after the uniqueKey element.

schema.xml

<uniqueKey>id</uniqueKey>
<!-- Fields added for books.csv load-->
<field name="cat" type="text_general" indexed="true" stored="true"/>
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="price" type="tdouble" indexed="true" stored="true"/>
<field name="inStock" type="boolean" indexed="true" stored="true"/>
<field name="author" type="text_general" indexed="true" stored="true"/>

We have set the attribute indexed to true. This specifies the field is used for indexing and the record can be retrieved using the index. Setting the value to false will make the field only stored but can’t be queried with.

Also note we have another attribute called stored and set it to true. This specifies the field is stored and can be returned in the output. Setting this field to false will make the field only indexed and can’t be retrieved in output.

We have assigned the type for the fields present in the “books.csv” file here. The first field in the CSV file “id” is automatically taken care by the uniqueKey element of schema.xml file for indexing.

4. Configuring Spellcheck

The Solr Spellcheck component is designed to provide inline query suggestions based on other, similar terms. The source for these suggestions can be terms in a field in Solr, externally created text files, or fields in other Lucene indexes. In this example we will see how to source the suggestion from the terms indexed in the Solr and will park the discussion on external files and other indexes for a later discussion.

We will use the DirectSolrSpellChecker in this example. The spellchecker uses terms from the Solr index without building a parallel index. 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.

Let’s configure the solrconfig.xml in the folder \server\solr\jcg\conf to add the component and handler. In the following configuration we have used the field name (book name) one of the term used in our example file. Other configurations are explained in the comment section for each field.

solrconfig.xml

 <searchComponent name="spellcheck" class="solr.SpellCheckComponent">
 <!-- a spellchecker built from a field of the main index -->
 <lst name="spellchecker">
 <str name="name">default</str>
 <str name="field">name</str>
 <str name="classname">solr.DirectSolrSpellChecker</str>
 <!-- minimum accuracy needed to be considered a valid spellcheck suggestion -->
 <float name="accuracy">0.5</float>
 <!-- the minimum shared prefix when enumerating terms -->
 <int name="minPrefix">1</int>
 <!-- minimum length of a query term to be considered for correction -->
 <int name="minQueryLength">3</int>
 </lst>
 </searchComponent>

Now let’s configure the requestHandler. The parameters used are defined below:

  • spellcheck.dictionary – Specifies the dictionary that should be used for spellchecking.
  • spellcheck – Turns on or off SpellCheck suggestions for the request. If true, then spelling suggestions will be generated.
  • spellcheck.extendedResults – Causes Solr to return additional information about spellcheck results, such as the frequency of each original term in the index.
  • spellcheck.count – Specifies the maximum number of spelling suggestions to be returned.
  • spellcheck.alternativeTermCount – The count of suggestions to return for each query term existing in the index and/or dictionary.
  • spellcheck.maxResultsForSuggest – The maximum number of hits the request can return in order to both generate spelling suggestions and set the “correctlySpelled” element to “false.

solrconfig.xml

<requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">
 <lst name="defaults">
 <str name="spellcheck.dictionary">default</str>
 <str name="spellcheck">on</str>
 <str name="spellcheck.extendedResults">true</str> 
 <str name="spellcheck.count">10</str>
 <str name="spellcheck.alternativeTermCount">5</str>
 <str name="spellcheck.maxResultsForSuggest">5</str> 
 </lst>
 <arr name="last-components">
 <str>spellcheck</str>
 </arr>
 </requestHandler>

Since we have modified the configuration we have to stop and start the server. To do so, we need to issue the following command from bin directory through command line:

solr stop -all

The server will be stopped now. Now to start the server issue the following command from bin directory through command line:

solr start

5. Indexing the Data

Apache Solr comes with a Standalone Java program called the SimplePostTool. This program is packaged into JAR and available with the installation under the folder example\exampledocs.

Now we navigate to the example\exampledocs folder in the command prompt and type the following command. You will see a bunch of options to use the tool.

java -jar post.jar -h

The usage format in general is as follows:

Usage: java [SystemProperties] -jar post.jar [-h|-] [<file|folder|url|arg>
[<file|folder|url|arg>...]]

As we said earlier, we will index the data present in the “books.csv” file shipped with Solr installation. We will navigate to the solr-5.0.0\example\exampledocs in the command prompt and issue the following command.

java -Dtype=text/csv -Durl=http://localhost:8983/solr/jcg/update -jar post.jar  books.csv

The SystemProperties used here are:

  • -Dtype – the type of the data file.
  • -Durl – URL for the jcg core.

The file “books.csv” will now be indexed and the command prompt will display the following output.

SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/jcg/update using content-
type text/csv...
POSTing file books.csv to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/jcg/update...
Time spent: 0:00:00.647

6. Query using spellcheck

Now, we will launch the following URL to validate the spellcheck functionality. You can note, the spelling for the “Jhereg” book is purposely queried wrongly “Jherag” to test our example. The spellcheck.q parameter is used to specify the query to be spellchecked. We can also use the q parameter instead of spellcheck.q but its suggested to use spellcheck prefix in places we do spellcheck to provide clarity.

http://localhost:8983/solr/jcg/spell?spellcheck.q=Jherag&spellcheck=true

solr_spell_output
Solr Spellcheck output

7. Download the configuration

This was an example on Solr Spellcheck.

Download
You can download the configuration files used in this example here: solr_spell_config

Veeramani Kalyanasundaram

Veera is a Software Architect working in telecom domain with rich experience in Java Middleware Technologies. He is a OOAD practitioner and interested in Performance Engineering.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button