Chapter 11. Case Study of Using Solr in E-Commerce

In the previous chapter, we saw how we can use SolrCloud to set up a cluster of Solr servers, which can be used to provide a fault-tolerant and high availability environment. Now let's see how we can use Solr in e-commerce websites to improve user experience while searching for a relevant product. Providing the best user experience is the main concern for e-commerce websites these day, and we can use Solr's inbuilt features to provide a much better search experience for the user.

In this chapter, we'll cover the following topics:

  • Creating an AutoSuggest feature
  • Result grouping and facet search
  • Search filtering and sorting
  • Relevancy boosting

Creating an AutoSuggest feature

One of the most common features that we can see in today's e-commerce websites is an AutoSuggest feature, which provides users with a list of available content. Let's see how we can create this feature using Solr.Todo this, we'll use the following steps.

Let's create a new core in Solr and call it musicStore. After we have created the core, we'll create a new schema.xml. It will contain the following fields:

<!-- Unique Id -->
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
<!-- Song name -->
<field name="songName" type="string" indexed="true" stored="true" required="true" multiValued="false"/>

<!-- Artist name -->
<field name="artistName" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
<!-- Album Artist -->
<field name="albumArtist" type="string" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Album name -->
<field name="albumName" type="string" indexed="true" stored="true" required="true" multiValued="false"/>

<!-- Duration of the Song -->
<field name="songDuration" type="double" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Duration of the Song -->
<field name="composer" type="string" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Song rating -->
<field name="rating" type="float" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Year which the song has been published -->
<field name="year" type="int" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Genre of the song (e.g. rock, pop, indie, etc)-->
<field name="genre" type="string" indexed="true" stored="true" required="false" multiValued="false"/>

<!-- Price -->
<field name="price" type="float" indexed="true" stored="true" required="false" multiValued="false"/>
<!-- Sale -->
<field name="sale" type="boolean" indexed="true" stored="true" required="false" default="false" multiValued="false"/>

<!--Suggestions field -->
<field name="txtSuggestions" type="suggestType" indexed="true" stored="true" multiValued="true"/>

We'll also create a new fieldType in schema.xml; it will hold the tokenized values of songName. This will use WhitespaceTokenizerFactory to create tokens of the indexed value:

<!--Suggestion FieldType -->
  <fieldType name="suggestType" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <charFilter class="solr.PatternReplaceCharFilterFactory" pattern="[^a-zA-Z0-9]" replacement=" "/>
      <tokenizer class="solr.WhitespaceTokenizerFactory"/>
      <filter class="solr.StandardFilterFactory"/>
      <filter class="solr.LowerCaseFilterFactory"/>
      <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
    </analyzer>
  </fieldType>

In the suggestType field type, we've used filters to tokenize the words that are getting indexed into Solr and make sure that we're not indexing any duplicate data in them. More information about the filters used can be found on the Solr Wiki at https://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters.

After making the changes in schema.xml, we'll add a searchComponent of the solr.SuggestComponent type in solrconfig.xml. The searchComponent will provide us with suggestions depending on the field that we specify. In this case, we're using the txtSuggestions field, which will contain the song name that has been tokenized during indexing:

<!--SearchComponent -->
  <searchComponent name="suggestComponent" class="solr.SuggestComponent">
    <lst name="suggester">
      <str name="name">customSuggester</str>
      <str name="lookupImpl">BlendedInfixLookupFactory</str>
      <str name="suggestAnalyzerFieldType">suggestType</str>
      <str name="blenderType">linear</str>
      <str name="minPrefixChars">1</str>
      <str name="doHighlight">false</str>
      <str name="weightField">score</str>
      <str name="dictionaryImpl">DocumentDictionaryFactory</str>
      <str name="field">txtSuggestions</str>
      <str name="buildOnStartup">true</str>
      <str name="buildOnCommit">true</str>
    </lst>
  </searchComponent>

Once we have added the searchComponent, we'll add a search handler with the path as /suggest, which we can use to suggest the song name:

  <!--Suggest Request Handler -->
  <requestHandler class="solr.SearchHandler" name="/suggest" startup="lazy" >
    <lst name="defaults">
      <str name="suggest">true</str>
      <str name="suggest.count">10</str>
      <str name="suggest.dictionary">customSuggester</str>
    </lst>
    <arr name="components">
      <str>suggestComponent</str>
    </arr>
  </requestHandler>

In this searchHandler, we're referencing the suggestComponent that we added in the previous step. The following attributes are set in the custom search handler:

  • suggest: If this is set to true, it will use suggestComponent.
  • suggest.count: This is the maximum number of suggestions that the handler should return.
  • suggest.dictionary: This is the name of the dictionary to use. In our case, it's customSuggester, which we've specified in searchComponent.

    Note

    More information about the attributes can be found at https://cwiki.apache.org/confluence/display/solr/Suggester.

After setting up schema.xml and solrconfig.xml, we can index some data into the core. We can use the sampleMusicStoreData.csv included in the Chapter 11 code ZIP file by executing the following command:

curl 'http://localhost:8983/solr/musicStore/update?commit=true' --data-binary @sampleMusicStoreData.csv -H 'Content-type:application/csv'

After indexing the documents, we can navigate to the following URL, which will suggest song names that start with "A":

http://localhost:8983/solr/musicStore/suggest?q=A&wt=json

We'll get this response from the Solr server:

{
 "responseHeader":{
      "status":?0,
      "QTime":?0
   },
   "suggest":{
      "analyzing":{
         "A":{
            "numFound":?2,
            "suggestions":[
               {
                  "term":"<b>A</b>ll I Want For Christmas Is You",
                  "weight":?0,
                  "payload":""
               },
               {
                  "term":"<b>A</b> Thousand Years",
                  "weight":?0,
                  "payload":""
               }
            ]
         }
      }
   }
}

We can see from the response that the component has returned two suggestions that start with "A".

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.236.231