

We can create solr custom field types in two ways.First one is to create field type from existing available fieldtypes and second one is to create our own field type and use that field type to field definations.
Let’s have look to both approach in details.
Table of Contents
Create custom field type from available field types:
<fieldType name="TEXT_UPPER" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="solr.WhitespaceTokenizerFactory"/> <filter class="solr.UpperCaseFilterFactory"/> </analyzer> </fieldType>
We can define query analyzer and index analyzer separately by specifying analyzer attribute type.
In below fieldType definition we have apply same tokenizer and filter classes.we can apply different tokenizer and filters in query and index type analyzer.
<fieldType name="TEXT_UPPER" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.WhitespaceTokenizerFactory"/> <filter class="solr.UpperCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.WhitespaceTokenizerFactory"/> <filter class="solr.UpperCaseFilterFactory"/> </analyzer> </fieldType>
Now our field type is ready , we can use in our field definations as below to index all document content in upper case.
<field name="DocumentText" type="TEXT_UPPER" indexed="true" stored="false" multiValued="true"/>
Create custom field from our own field types:
Solr also provide facility to create custom fieldtype from our own developed classes.
Consider a scenario where we want to index sum of all the digits from a integer number.In this type of special cases we need to create custom field type and write logic into it.
Steps :
- Extend org.apache.solr.schema.Fieldtype class and override it’s write and getSortField methods.
- Created field type with newly created class.
- Use created field type in field defination.
Example:
package com.javadevzone; import java.io.IOException; import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.SortField; import org.apache.solr.response.TextResponseWriter; import org.apache.solr.schema.FieldType; import org.apache.solr.schema.SchemaField; /** * Created by JavaDeveloperZone on 5/7/2017. */ public class IntSumField extends FieldType{ @Override public void write(TextResponseWriter writer, String name, IndexableField f) throws IOException { String s = f.stringValue(); // these values may be from a legacy lucene index, which may // not be properly formatted in some output formats, or may // incorrectly have a zero length. if (s.length()==0) { // zero length value means someone mistakenly indexed the value // instead of simply leaving it out. Write a null value instead of a numeric. writer.writeNull(name); return; } try { int sum=0; for(int index=0;index<s.length();index++){ sum+=Integer.parseInt(String.valueOf(s.charAt(index))); } writer.writeInt(name,sum); } catch (NumberFormatException e){ // can't parse - write out the contents as a string so nothing is lost and // clients don't get a parse error. writer.writeStr(name, s, true); } } @Override public SortField getSortField(SchemaField field, boolean top) { field.checkSortability(); return new SortField(field.getName(),SortField.Type.INT, top); } }
Schema.xml:
Now we need to specify our class name while creating custom field type.
<fieldType name="intSumFieldType" class="com.javadevzone.IntSumField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
use above declared field type name while defining fields.
<field name="DigitSum" type="intSumFieldType" indexed="false" stored="false"/>