solr Create a custom Solr field type from own custom Java class


Schema changes:

You will need to define a new field type in your solr schema file and then you can create fields of that type. Example schema snippet:

<!-- Source: solr/example/.../conf/schema.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="adam" version="1.3">
    <fieldType name="rank_t" class="org.apache.solr.schema.ext.RankFieldType"/>
   <field name="rank" type="rank_t" indexed="true" stored="true"/>

Java class for custom field type:

// Source: src/java/org/apache/solr/schema/ext/
package org.apache.solr.schema.ext;


import org.apache.lucene.document.Fieldable;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.SchemaField;

public class RankFieldType extends FieldType {

    public SortField getSortField(SchemaField field, boolean top) {
        return new SortField(field.getName(), new RankFieldComparatorSource(), top);

    // copied verbatim from GeoHashField method
    public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
         writer.writeStr(name, f.stringValue(), false);