javasearchsolrsolrcloudsolrconfig

How can I highlight solr field of type StringField


I am having a problem enabling solr highlighting on some of my schema fields

For example, I have the following field types:

<fieldType name="string" class="solr.StringField" />
<fieldType name="text" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="query">
    <tokenizer class="solr.ICUTokenizerFactory" />
    <filter class="solr.ICUFoldingFilterFactory" />
    <filter class="solr.WordDelimiterFilterFactory" splitOnCaseChange="0" splitOnNumerics="0" stemEnglishPossessive="0" preserveOriginal="1" />
    <filter class="solr.TrimFilterFactory" />
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  </analyzer>
  <analyzer type="index">
    <tokenizer class="solr.ICUTokenizerFactory" />
    <filter class="solr.ICUFoldingFilterFactory" />
    <filter class="solr.WordDelimiterFilterFactory" splitOnCaseChange="0" splitOnNumerics="0" stemEnglishPossessive="0" preserveOriginal="1" />
    <filter class="solr.TrimFilterFactory" />
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  </analyzer>
</fieldType>

I would like to perform text highlighting on any field of type "String" or "Text". The problem is I am unable to make solr highlight for type "String". It only works of type "Text". I do not want to make any changes to the actual text of field of type "String" but I would like solr to pick up the highlighting.

Any thoughts?

I am using solr 9 with java 17


Solution

  • Use your string fields to do EXACT matching and facet displays. It's not a good choice for highlighting.

    For highlighting, you simply have to edit the string field in the schema (or managed-schema) and create a new field of text type:

    <copyField source="cat" dest="text" maxChars="30000" />
    

    and make sure you define the field you're copying to.

    If you're feeling lazy and don't care about ending your highlight fields with {_t}, solr does have a default schema setting to automatically copy over the field to a text type simply by naming it with a _t at the end:

    <copyField source="cat" dest="dynamic_text_example_t" maxChars="30000" />
    

    you can read all about copying fields here

    It feels like a lot of work at first, but this is why solr is fast.