Interface for custom field types
DSE Search implements a CustomFieldType
interface that marks Apache Solr™ custom field types and provides their actual stored field type.
The custom field type stores an integer trie field as a string representing a comma separated list of integer values.
When indexed the string is split into its integer values, each one indexed as a trie integer field.
This class effectively implements a multi-valued field based on its string representation.
A CustomFieldType
overrides this method to provide the FieldType
for the binary response writer to look at when it determines whether to call the field’s toObject()
.
This allows the binary response writer, for instance, to return java.util.Date
in place of text for a CustomFieldType
that extends TrieDateField
.
To ensure that custom field types control their serialized value, use:
public Class<? extends FieldType> getKnownType() { return getClass(); }
See the example reference implementation.
Procedure
To use the CustomFieldType
interface:
-
Implement a custom field type class something like the following reference implementation.
-
Export the class to a JAR, and place the JAR in this location:
-
Package installations:
usr/share/dse
-
Tarball and Installer-No Services installations:
<installation_location>/resources/dse/lib
-
The JAR is added to the
CLASSPATH
automatically.
-
-
Example
Reference implementation
Here is an example of a custom field type class:
package com.datastax.bdp.search.solr.functional; import com.datastax.bdp.search.solr.CustomFieldType; import java.util.ArrayList; import java.util.List; import org.apache.lucene.index.IndexableField; import org.apache.solr.schema.FieldType; import org.apache.solr.schema.SchemaField; import org.apache.solr.schema.StrField; import org.apache.solr.schema.TrieField; public class CustomTestField extends TrieField implements CustomFieldType { public CustomTestField() { this.type = TrieField.TrieTypes.INTEGER; } @Override public FieldType getStoredFieldType() { return new StrField(); } @Override public boolean multiValuedFieldCache() { return true; } @Override public ListIndexableField createFields(SchemaField sf, Object value) { String[] values = ((String) value).split(" "); ListIndexableField fields = new ArrayListIndexableField(); for (String v : values) { fields.add(createField(sf, v)); } return fields; } @Override public String toInternal(String value) { return value; } @Override public String toExternal(IndexableField f) { return f.stringValue(); } public Class<? extends FieldType> getKnownType() { return TrieField.class; } }