solrsolr-schema

Convert words based solr search to edge n gram


My Solr gives result based on word but it does not display a result when a character is input. Example: It gives result for "This" as "This is a book", but no result for "Th".

Can someone help me with my configurations to have edge n gram searches

here is my schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema>
   <types>
      <!-- field type used for autocomplete feature in 
              conjunction with a suggester component -->
<fieldType name="long" class="solr.TrieLongField" precisionStep="0"     positionIncrementGap="0" />
<fieldType name="edgytext" class="solr.TextField" positionIncrementGap="100">
 <analyzer type="index">
   <tokenizer class="solr.KeywordTokenizerFactory"/>
   <filter class="solr.LowerCaseFilterFactory"/>
   <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="25" />
 </analyzer>
 <analyzer type="query">
   <tokenizer class="solr.KeywordTokenizerFactory"/>
   <filter class="solr.LowerCaseFilterFactory"/>
 </analyzer>
</fieldType>
      <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
         <analyzer>
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0"     splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
      </fieldType>
      <fieldType name="text_auto" class="solr.TextField">
         <analyzer>
            <tokenizer class="solr.KeywordTokenizerFactory" />
            <filter class="solr.LowerCaseFilterFactory" />
         </analyzer>
      </fieldType>
   </types>
   <fields>
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
      <field name="id" type="text" indexed="true" stored="true" multiValued="false"     required="true" />
      <field name="name" type="text" indexed="true" stored="true" multiValued="false" />
<field name="author" type="text" indexed="true" stored="true" multiValued="false" />
      <field name="description" type="text" indexed="true" stored="true" multiValued="false" />
      <field name="name_auto" type="edgytext" indexed="true" stored="true" multiValued="true" omitNorms="true"
  omitTermFreqAndPositions="true"/>
<field name="weight" type="long" indexed="true" stored="true" multiValued="false" />
<copyField source="id" dest="name_auto" />
<copyField source="author" dest="name_auto" />
      <copyField source="name" dest="name_auto" />
   </fields>
   <uniqueKey>id</uniqueKey>
    </schema>

And here is my solrconfig file: ...

 <searchComponent name="suggest" class="solr.SpellCheckComponent">
<lst name="spellchecker">
  <str name="name">suggest</str>
  <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
  <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
  <str name="field">name</str>
  <str name="sourceLocation">dict.txt</str>
 </lst>
</searchComponent>

...
<requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
     <lst name="defaults">
<str name="echoParams">NONE</str>
<str name="defType">edismax</str>
<str name="rows">10</str>    
<str name="fl">id,author,name,description,weight,name_auto</str>
<str name="qf">name_auto</str>
<str name="sort">weight desc</str>
<str name="wt">json</str>
<str name="q.op">AND</str>
<str name="spellcheck">true</str>
<str name="spellcheck.count">1</str>
<str name="spellcheck.onlyMorePopular">true</str>
<str name="spellcheck.accuracy">0.7</str>
<str name="spellcheck.collate">true</str>
</lst>
<arr name="last-components">
<str>suggest</str>
</arr>
    </requestHandler>

I am new to Solr. Any help will be aprpeciated. :)


Solution

  • I finally got edge n grams to work . here is what my schema looks like now.

    <?xml version="1.0" encoding="UTF-8"?>
    <schema>
       <types>
          <!-- field type used for autocomplete feature in 
                  conjunction with a suggester component -->
    
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0" />
    
    <fieldType name="edgytext" class="solr.TextField" positionIncrementGap="100">
    
    <analyzer type="index">
                <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-    ISOLatin1Accent.txt" />
                    <tokenizer class="solr.StandardTokenizerFactory" />
                    <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="0"
                        catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" />
                    <filter class="solr.LowerCaseFilterFactory" />
                    <filter class="solr.EdgeNGramFilterFactory" maxGramSize="20"     minGramSize="1" side="front" />
                    <filter class="solr.PatternReplaceFilterFactory" pattern="    ([^\w\d\*æøåÆØÅ ])" replacement="" replace="all" />
                </analyzer>
                <analyzer type="query">
                    <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-    ISOLatin1Accent.txt" />
                    <tokenizer class="solr.StandardTokenizerFactory" />
                    <filter class="solr.WordDelimiterFilterFactory" generateWordParts="0"     generateNumberParts="0" catenateWords="0"
                        catenateNumbers="0" catenateAll="0" splitOnCaseChange="0" />
                    <filter class="solr.LowerCaseFilterFactory" />
                    <filter class="solr.PatternReplaceFilterFactory" pattern="    ([^\w\d\*æøåÆØÅ ])" replacement="" replace="all" />
                    <filter class="solr.PatternReplaceFilterFactory" pattern="^(.{20})(.*)?"     replacement="$1" replace="all" />
                </analyzer>
    </fieldType>
    
          <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
             <analyzer>
                <tokenizer class="solr.WhitespaceTokenizerFactory" />
                <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0"     splitOnCaseChange="1" />
                <filter class="solr.LowerCaseFilterFactory" />
             </analyzer>
            </fieldType>
    
            <fieldType name="text_auto" class="solr.TextField">
             <analyzer>
                <tokenizer class="solr.KeywordTokenizerFactory" />
                <filter class="solr.LowerCaseFilterFactory" />
             </analyzer>
          </fieldType>
    
       </types>
       <fields>
    <field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
          <field name="id" type="text" indexed="true" stored="true" multiValued="false"     required="true" />
          <field name="name" type="edgytext" indexed="true" stored="true"     multiValued="false" />
    <field name="author" type="text" indexed="true" stored="true" multiValued="false" />
          <field name="description" type="text" indexed="true" stored="true"     multiValued="false" />
          <field name="name_auto" type="edgytext" indexed="true" stored="true"     multiValued="true" omitNorms="true"
      omitTermFreqAndPositions="true"/>
    <field name="weight" type="long" indexed="true" stored="true" multiValued="false" />
    <copyField source="author" dest="name_auto" />
          <copyField source="name" dest="name_auto" />
       </fields>
       <uniqueKey>id</uniqueKey>
    </schema>
    

    The solr config is same as pasted above. This works perfectly for edge n grams.

    Please note always delete the old data and add it again whenever you change schema files.