“Exact” phrase matching with wildcards + SOLR

455 views Asked by At

Actually I want to do exact matching with 'B T Patil' and I have created a following filed-type

<fieldType name="text_name" class="solr.TextField" omitNorms="false">
  <analyzer>
     <tokenizer class="solr.WhitespaceTokenizerFactory"/>
     <filter class="solr.WordDelimiterFilterFactory" preserveOriginal="1" splitOnNumerics="1" splitOnCaseChange="1" catenateWords="1"
      catenateNumbers="1" catenateAll="1" generateWordParts="1" generateNumberParts="1" stemEnglishPossessive="1" />
  </analyzer>
</fieldType>

Also my text field and ngram filed also applied on the string. So Whenever I am entering the B T Patil query in a my field at that time its also returning me irrelevant results i.e Its returning me

 b t Agrawal 
 Jaykumar B. Patil
 BHASKAR B. PATIL

I am expecting it should return me b t patil or b.t.patil or b. t. patil results. Is any need to change the tokenizer ?

1

There are 1 answers

0
Datt On

You can try something like this. Create a field type for exact match like following

    <fieldType name="text_exact" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>


<dynamicField name="*_exact" stored="false" type="text_exact" multiValued="true" indexed="true"/>

boost this field and phrase field comparatively than text and text_name fields.

searchable do
   text :field1_exact, as: :field1_exact, default_boost: 5.0 do
     field1
   end

   text :field1_name, as: :field1_name, default_boost: 3.0 do
     field1
   end
end

Sunspot.search(Model) do
  fulltext 'B T Patil'do
    fields (field1_exact, field1_name, field1)
    phrase_fields (field1_exact: 8, field1_name: 4, field1: 2)
  end
end

Give least priority to ngram fields if any.