a blog for those who code

Wednesday 20 February 2019

How I configured SOLR for Partial Search

Solr Partial Search

Sometimes the users will not remember the full string to search mainly for the user names, so they wanted me to add Partial Search only on the name and not other fields.

To do that at first we need to create a new fieldType (copy from the existing one) and add NGarmFilterFactory to it as shown below :

<fieldType name="text_partial" class="solr.TextField">
  <analyzer type="index">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.KeywordMarkerFilterFactory" protected="protwords.txt"/>
    <filter class="solr.NGramFilterFactory" minGramSize="4" maxGramSize="10"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.WhitespaceTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.KeywordMarkerFilterFactory" protected="protwords.txt"/>
  </analyzer>    
</fieldType>

As you can see we have only added at the type of index time and not query time so that it will be faster and the SOLR should not spend time again to create those tokens. The minGramSize is 4 whereas the maxGramSize is 10 in our case. So for example if the name is "coding" it will generate following tokens :

codi, codin, coding, odin, oding, ding

Thus if we search any words from above my "coding" username will be search or get a hit.   

No comments:

Post a Comment