Auto Complete Suggestions Implementation Using Apache Solr – Approach2

Apache Solr Auto Suggestions Or Auto Completion          In the PART-1 post, we discussed the basic setup to get the auto suggestions starting with the search keyword. In this article, we will discuss how to configure Apache Solr to get the auto suggestions based on the search keyword that appears at any position. For example, If user is typing for “lap“, we need to provide “laptop computers”, “dell laptop”, “hp laptop” etc as auto suggestions. If you look at the suggestions, the suggestions are not only the words starting with “lap“, but also the suggestions having “lap” at any position. The below picture depicts the same.

Sample Auto Suggestions

                     To achieve the same thing what we are seeing in the above picture, we need to change the analysis part in the schema.xml (In part-1 we discussed the setup, here we are enhancing the part-1 configuration). The configuration is given below.

<fieldType name="text_suggest" class="solr.TextField" positionIncrementGap="100">
   <analyzer type="index">
      <tokenizer class="solr.WhitespaceTokenizerFactory" />
      <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>

The the fields configuration is given below.

<fields>
   <field name="id" type="integer" indexed="true" stored="true" required="true" />
   <field name="term" type="text_suggest" indexed="true" stored="true" />
</fields>

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 key word “lap“,  http://<host name>:<port>/solr/autoSuggestApproach2/select?q=term:”lap”&wt=xml&indent=true&rows=10

The response is given below.

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <lst name="responseHeader">
      <int name="status">0</int>
      <int name="QTime">0</int>
      <lst name="params">
         <str name="q">term:"lap"</str>
         <str name="rows">10</str>
      </lst>
   </lst>
   <result name="response" numFound="940" start="0">
      <doc>
         <str name="term">e laptop</str>
         <int name="id">3228</int>
      </doc>
      <doc>
         <str name="term">laptop a</str>
         <int name="id">5180</int>
      </doc>
      <doc>
         <str name="term">laptops</str>
         <int name="id">5510</int>
      </doc>
      <doc>
         <str name="term">hp laptop laptop</str>
         <int name="id">4339</int>
      </doc>
      <doc>
         <str name="term">lap rest for laptop</str>
         <int name="id">5149</int>
      </doc>
      <doc>
         <str name="term">lap tray laptop</str>
         <int name="id">5152</int>
      </doc>
      <doc>
         <str name="term">laptop case laptop</str>
         <int name="id">5282</int>
      </doc>
      <doc>
         <str name="term">laptop laptop</str>
         <int name="id">5410</int>
      </doc>
      <doc>
         <str name="term">laptop laptop bag</str>
         <int name="id">5411</int>
      </doc>
      <doc>
         <str name="term">laptop on your lap</str>
         <int name="id">5433</int>
      </doc>
   </result>
</response>

The above response is having the auto suggestions having “lap” keyword at any position.  we  can use this to present the auto suggestions/ auto completion feature in the site.

The  Solr setup used in this article is available here.

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with: , , ,
Posted in Apache Solr, Auto Suggestions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: