Auto Complete Suggestions Implementation Using Apache Solr- Approach1

Apache Solr Auto Suggestions Or Auto Completion

                  In this article, we are going to discuss “how to set up auto suggest or auto-completion feature by using Apache Solr?“. Before getting into the setup, we will see “Why we require auto-suggest feature?“.

                      In today’s internet world, most of the websites are running with data and they are having a search feature to find out the content. If the user doesn’t know, what keyword to be used to search for the content, the user requires some suggestions from the site. These suggestions will improve users interactivity. Auto-suggestions help the users to find what they are looking for, by typing the least possible number of characters. The below picture shows how the auto-suggestions will be provided in the site.

Auto Suggestions

Now, We will see the auto-suggestions feature to set up. In the previous articles, we knew the Solr set up. So, here, Straight away  I am going to explain the auto-suggest feature configuration. Follow the below given steps.

Step 1:

Set up the data for auto-suggestions. The data model is given below.

Data model for auto suggestions or auto completion

                  In the table, the TERM column will contain the search keywords that the user is used to. The VOLUME column contains the number of times the user has used the search keyword. Based on this volume, we will come to know, the popular search key words users use. The sample data dump is available here.

                Basically, in the enterprise applications, we require user generated content(the keywords which users are using to search) to drive the auto-suggestions. Here, we are going to capture all the user entered keywords into a database table or we will find out the used keywords for search from the application log files.

Once the data set up is done, we need to configure the Solr’s data config file required for the data import handler.

Step 2:

Solr’s data configuration is given below.


<!–
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>
<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://<host name>:<port>/<database name>" user="<user name>" password="<password>"/>
<document>
<entity name="autosuggestions" query="SELECT ID, TERM FROM auto_suggestions">
<field column="ID" name="Id" />
<field column="TERM" name="term" />
</entity>
</document>
</dataConfig>

Step 3:

Configure the schema.xml. In the schema.xml, the analyzer portion is very important. The configuration is given below.


<fieldType name="text_suggest" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="1000"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.WhitespaceTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>

view raw

schemal.xml

hosted with ❤ by GitHub

Step 4: 

Do the full import of the data. Once the full import is done, perform some searches to get the auto-suggestions.

For example, try to get the auto suggestions for the keyword “lap“,  http://:/solr/autoSuggestApproach1/select?q=term%3A%22lap%22&wt=xml&indent=true&rows=10

The response is given below.






 0
 1
 
 true
 term:"lap"
 xml
 10
 


 
 laptops
 5510
 
 laptop 15r
 5168
 
 laptop 199
 5173
 
 laptop 2gb
 5175
 
 laptop 400
 5176
 
 laptop 5i
 5177
 
 laptop a
 5180
 
 laptop dv7
 5365
 
 laptop gsm
 5384
 
 lap tray laptop
 5152



                     The above response is having the auto suggestions starting with “lap” . This response , we  can use to present the auto suggestions/ auto completion feature in the site.

                     The  Solr set up used in this article is available here. The set up which we have done in this article, will provide the auto suggestions starting with the search key word. In the coming article, we will discuss, the configuration to provide the auto suggestions matching with the search key word any where.

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: ,
Posted in Apache Solr, Auto Suggestions
5 comments on “Auto Complete Suggestions Implementation Using Apache Solr- Approach1
  1. Anonymous says:

    I tried to implement as your suggestion but the result is not good.
    The system informed :
    org.apache.solr.common.SolrException: SolrCore ‘search’ is not available due to init failure: RequestHandler init failure
    org.apache.solr.common.SolrException: SolrCore ‘search’ is not available due to init failure: RequestHandler init failure
    at org.apache.solr.core.CoreContainer.getCore(CoreContainer.java:745)
    at org.apache.solr.client.solrj.embedded.EmbeddedSolrServer.request(EmbeddedSolrServer.java:110)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
    at com.homedirect.moneydirect.search.DataSearch.top(DataSearch.java:257)

    I don’t know exactly about this. Can you know the way to solve this ?
    Thanks.

    • Siva Prasad says:

      These settings are my working examples. You can download the setup from the github and tryout.

      Meanwhile post the entire stacktrace to understand the issue better.

  2. Lasha says:

    Great blog, continue the good work!

  3. Liam says:

    Your web site provided us with valuable information to work on. You’ve
    done an impressive job and our entire community will be grateful to you.

  4. Kacey says:

    Good explanation and informative steps to implement the auto suggetsions. Thank you

Leave a comment

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

Dzone.com
DZone

DZone MVB

Java Code Geeks
Java Code Geeks
OpenSourceForYou