Add a custom field type in solr

java, solr

Solution

You can define new custom fields in solr by adding them to the `schema.xml` in the `<fields>` element:

<fields>
...
<field name="newfieldname" type="text_general" indexed="true" stored="true" multiValued="true"/>
...
</fields>

To have an overview on the supported types (used to define the `<fieldType>` in Solr, which then are used to define the `field` elements), take a look to this link: https://cwiki.apache.org/confluence/display/solr/Field+Types+Included+with+Solr. For example, the `type` "text_general" is defined in schema.xml as:

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
    <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

You can see it uses the `class="solr.TextField"` as its basic field type. The Solr schema already has several `types` defined by default... but you can define more, not sure if you can defined the ones you are asking for.

Problem

In solr schema.xml many field types are defined. for example ``` <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> ``` is a definition for an integer field. How we define custom field types inside solr? Is it possible to add java types such as BigInteger, BigDecimal, Map and other types as field type in solr?

Original source