org.apache.solr.schema
Class FieldType

java.lang.Object
  extended by org.apache.solr.schema.FieldProperties
      extended by org.apache.solr.schema.FieldType
Direct Known Subclasses:
AbstractSubTypeFieldType, BinaryField, CurrencyField, ExternalFileField, GeoHashField, PrimitiveFieldType, RandomSortField, TextField, UUIDField

public abstract class FieldType
extends FieldProperties

Base class for all field types used by an index schema.

Version:
$Id: FieldType.java 1206916 2011-11-27 23:23:00Z erick $

Nested Class Summary
protected  class FieldType.DefaultAnalyzer
          Default analyzer for types that only produce 1 verbatim token...
 
Field Summary
protected  Analyzer analyzer
          Analyzer set by schema for text types to use when indexing fields of this type, subclasses can set analyzer themselves or override getAnalyzer()
protected  Map<String,String> args
          additional arguments specified in the field type declaration
protected  int falseProperties
          properties explicitly set to false
static org.slf4j.Logger log
           
static String POLY_FIELD_SEPARATOR
          The default poly field separator.
protected  Analyzer queryAnalyzer
          Analyzer set by schema for text types to use when searching fields of this type, subclasses can set analyzer themselves or override getAnalyzer()
protected  int trueProperties
          properties explicitly set to true
protected  String typeName
          The name of the type (not the name of the field)
 
Fields inherited from class org.apache.solr.schema.FieldProperties
BINARY, INDEXED, MULTIVALUED, OMIT_NORMS, OMIT_POSITIONS, OMIT_TF_POSITIONS, REQUIRED, SORT_MISSING_FIRST, SORT_MISSING_LAST, STORE_TERMOFFSETS, STORE_TERMPOSITIONS, STORE_TERMVECTORS, STORED, TOKENIZED
 
Constructor Summary
FieldType()
           
 
Method Summary
 Fieldable createField(SchemaField field, String externalVal, float boost)
          Used for adding a document when a field needs to be created from a type and a string.
protected  Fieldable createField(String name, String val, Field.Store storage, Field.Index index, Field.TermVector vec, boolean omitNorms, FieldInfo.IndexOptions options, float boost)
          Create the field from native Lucene parts.
 Fieldable[] createFields(SchemaField field, String externalVal, float boost)
          Given a SchemaField, create one or more Fieldable instances
 Analyzer getAnalyzer()
          Returns the Analyzer to be used when indexing fields of this type.
protected  String getArg(String n, Map<String,String> args)
           
protected  Field.Index getFieldIndex(SchemaField field, String internalVal)
           
 Query getFieldQuery(QParser parser, SchemaField field, String externalVal)
          Returns a Query instance for doing searches against a field.
protected  Field.Store getFieldStore(SchemaField field, String internalVal)
           
protected  Field.TermVector getFieldTermVec(SchemaField field, String internalVal)
           
protected  FieldInfo.IndexOptions getIndexOptions(SchemaField field, String internalVal)
           
 Analyzer getQueryAnalyzer()
          Returns the Analyzer to be used when searching fields of this type.
 Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive)
          Returns a Query instance for doing range searches on this field type.
abstract  SortField getSortField(SchemaField field, boolean top)
          Returns the SortField instance that should be used to sort fields of this type.
protected  SortField getStringSort(SchemaField field, boolean reverse)
          Utility usable by subclasses when they want to get basic String sorting using common checks.
 String getTypeName()
          The Name of this FieldType as specified in the schema file
 ValueSource getValueSource(SchemaField field)
          Deprecated. use getValueSource(SchemaField, QParser)
 ValueSource getValueSource(SchemaField field, QParser parser)
          called to get the default value source (normally, from the Lucene FieldCache.)
protected  boolean hasProperty(int p)
          Check if a property is set
 String indexedToReadable(String indexedForm)
          Given an indexed term, return the human readable representation
protected  void init(IndexSchema schema, Map<String,String> args)
          subclasses should initialize themselves with the args provided and remove valid arguments.
 boolean isMultiValued()
          Returns true if fields can have multiple values
 boolean isPolyField()
          A "polyField" is a FieldType that can produce more than one Fieldable instance for a single value, via the createFields(org.apache.solr.schema.SchemaField, String, float) method.
 boolean isTokenized()
          Returns true if fields of this type should be tokenized
 boolean multiValuedFieldCache()
          Returns true if a single field value of this type has multiple logical values for the purposes of faceting, sorting, etc.
 String readableToIndexed(String val)
          Given the readable value, return the term value that will match it.
protected  void restrictProps(int props)
          :TODO: document this method
 void setAnalyzer(Analyzer analyzer)
          Sets the Analyzer to be used when indexing fields of this type.
 void setQueryAnalyzer(Analyzer analyzer)
          Sets the Analyzer to be used when querying fields of this type.
 String storedToIndexed(Fieldable f)
          Given the stored field, return the indexed form
 String storedToReadable(Fieldable f)
          Given the stored field, return the human readable representation
 String toExternal(Fieldable f)
          Convert the stored-field format to an external (string, human readable) value
 String toInternal(String val)
          Convert an external value (from XML update command or from query string) into the internal format for both storing and indexing (which can be modified by any analyzers).
 Object toObject(Fieldable f)
          Convert the stored-field format to an external object.
 String toString()
           
abstract  void write(TextResponseWriter writer, String name, Fieldable f)
          calls back to TextResponseWriter to write the field value
abstract  void write(XMLWriter xmlWriter, String name, Fieldable f)
          Renders the specified field as XML
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

public static final org.slf4j.Logger log

POLY_FIELD_SEPARATOR

public static final String POLY_FIELD_SEPARATOR
The default poly field separator.

See Also:
createFields(SchemaField, String, float), isPolyField(), Constant Field Values

typeName

protected String typeName
The name of the type (not the name of the field)


args

protected Map<String,String> args
additional arguments specified in the field type declaration


trueProperties

protected int trueProperties
properties explicitly set to true


falseProperties

protected int falseProperties
properties explicitly set to false


analyzer

protected Analyzer analyzer
Analyzer set by schema for text types to use when indexing fields of this type, subclasses can set analyzer themselves or override getAnalyzer()

See Also:
getAnalyzer(), setAnalyzer(org.apache.lucene.analysis.Analyzer)

queryAnalyzer

protected Analyzer queryAnalyzer
Analyzer set by schema for text types to use when searching fields of this type, subclasses can set analyzer themselves or override getAnalyzer()

See Also:
getQueryAnalyzer(), setQueryAnalyzer(org.apache.lucene.analysis.Analyzer)
Constructor Detail

FieldType

public FieldType()
Method Detail

isTokenized

public boolean isTokenized()
Returns true if fields of this type should be tokenized


isMultiValued

public boolean isMultiValued()
Returns true if fields can have multiple values


hasProperty

protected boolean hasProperty(int p)
Check if a property is set


isPolyField

public boolean isPolyField()
A "polyField" is a FieldType that can produce more than one Fieldable instance for a single value, via the createFields(org.apache.solr.schema.SchemaField, String, float) method. This is useful when hiding the implementation details of a field from the Solr end user. For instance, a spatial point may be represented by multiple different fields.

Returns:
true if the createFields(org.apache.solr.schema.SchemaField, String, float) method may return more than one field

multiValuedFieldCache

public boolean multiValuedFieldCache()
Returns true if a single field value of this type has multiple logical values for the purposes of faceting, sorting, etc. Text fields normally return true since each token/word is a logical value.


init

protected void init(IndexSchema schema,
                    Map<String,String> args)
subclasses should initialize themselves with the args provided and remove valid arguments. leftover arguments will cause an exception. Common boolean properties have already been handled.


getArg

protected String getArg(String n,
                        Map<String,String> args)

restrictProps

protected void restrictProps(int props)
:TODO: document this method


getTypeName

public String getTypeName()
The Name of this FieldType as specified in the schema file


toString

public String toString()
Overrides:
toString in class Object

createField

public Fieldable createField(SchemaField field,
                             String externalVal,
                             float boost)
Used for adding a document when a field needs to be created from a type and a string.

By default, the indexed value is the same as the stored value (taken from toInternal()). Having a different representation for external, internal, and indexed would present quite a few problems given the current Lucene architecture. An analyzer for adding docs would need to translate internal->indexed while an analyzer for querying would need to translate external->indexed.

The only other alternative to having internal==indexed would be to have internal==external. In this case, toInternal should convert to the indexed representation, toExternal() should do nothing, and createField() should *not* call toInternal, but use the external value and set tokenized=true to get Lucene to convert to the internal(indexed) form.

:TODO: clean up and clarify this explanation.

See Also:
toInternal(java.lang.String)

createField

protected Fieldable createField(String name,
                                String val,
                                Field.Store storage,
                                Field.Index index,
                                Field.TermVector vec,
                                boolean omitNorms,
                                FieldInfo.IndexOptions options,
                                float boost)
Create the field from native Lucene parts. Mostly intended for use by FieldTypes outputing multiple Fields per SchemaField

Parameters:
name - The name of the field
val - The _internal_ value to index
storage - Field.Store
index - Field.Index
vec - Field.TermVector
omitNorms - true if norms should be omitted
options - options for what should be indexed in the postings
boost - The boost value
Returns:
the Field.

createFields

public Fieldable[] createFields(SchemaField field,
                                String externalVal,
                                float boost)
Given a SchemaField, create one or more Fieldable instances

Parameters:
field - the SchemaField
externalVal - The value to add to the field
boost - The boost to apply
Returns:
An array of Fieldable
See Also:
createField(SchemaField, String, float), isPolyField()

getFieldTermVec

protected Field.TermVector getFieldTermVec(SchemaField field,
                                           String internalVal)

getFieldStore

protected Field.Store getFieldStore(SchemaField field,
                                    String internalVal)

getFieldIndex

protected Field.Index getFieldIndex(SchemaField field,
                                    String internalVal)

getIndexOptions

protected FieldInfo.IndexOptions getIndexOptions(SchemaField field,
                                                 String internalVal)

toInternal

public String toInternal(String val)
Convert an external value (from XML update command or from query string) into the internal format for both storing and indexing (which can be modified by any analyzers).

See Also:
toExternal(org.apache.lucene.document.Fieldable)

toExternal

public String toExternal(Fieldable f)
Convert the stored-field format to an external (string, human readable) value

See Also:
toInternal(java.lang.String)

toObject

public Object toObject(Fieldable f)
Convert the stored-field format to an external object.

Since:
solr 1.3
See Also:
toInternal(java.lang.String)

indexedToReadable

public String indexedToReadable(String indexedForm)
Given an indexed term, return the human readable representation


storedToReadable

public String storedToReadable(Fieldable f)
Given the stored field, return the human readable representation


storedToIndexed

public String storedToIndexed(Fieldable f)
Given the stored field, return the indexed form


readableToIndexed

public String readableToIndexed(String val)
Given the readable value, return the term value that will match it.


getAnalyzer

public Analyzer getAnalyzer()
Returns the Analyzer to be used when indexing fields of this type.

This method may be called many times, at any time.

See Also:
getQueryAnalyzer()

getQueryAnalyzer

public Analyzer getQueryAnalyzer()
Returns the Analyzer to be used when searching fields of this type.

This method may be called many times, at any time.

See Also:
getAnalyzer()

setAnalyzer

public void setAnalyzer(Analyzer analyzer)
Sets the Analyzer to be used when indexing fields of this type.

The default implementation throws a SolrException. Subclasses that override this method need to ensure the behavior of the analyzer is consistent with the implementation of toInternal.

See Also:
toInternal(java.lang.String), setQueryAnalyzer(org.apache.lucene.analysis.Analyzer), getAnalyzer()

setQueryAnalyzer

public void setQueryAnalyzer(Analyzer analyzer)
Sets the Analyzer to be used when querying fields of this type.

The default implementation throws a SolrException. Subclasses that override this method need to ensure the behavior of the analyzer is consistent with the implementation of toInternal.

See Also:
toInternal(java.lang.String), setAnalyzer(org.apache.lucene.analysis.Analyzer), getQueryAnalyzer()

write

public abstract void write(XMLWriter xmlWriter,
                           String name,
                           Fieldable f)
                    throws IOException
Renders the specified field as XML

Throws:
IOException

write

public abstract void write(TextResponseWriter writer,
                           String name,
                           Fieldable f)
                    throws IOException
calls back to TextResponseWriter to write the field value

Throws:
IOException

getSortField

public abstract SortField getSortField(SchemaField field,
                                       boolean top)
Returns the SortField instance that should be used to sort fields of this type.

See Also:
SchemaField.checkSortability()

getStringSort

protected SortField getStringSort(SchemaField field,
                                  boolean reverse)
Utility usable by subclasses when they want to get basic String sorting using common checks.

See Also:
SchemaField.checkSortability()

getValueSource

public ValueSource getValueSource(SchemaField field,
                                  QParser parser)
called to get the default value source (normally, from the Lucene FieldCache.)


getValueSource

@Deprecated
public ValueSource getValueSource(SchemaField field)
Deprecated. use getValueSource(SchemaField, QParser)


getRangeQuery

public Query getRangeQuery(QParser parser,
                           SchemaField field,
                           String part1,
                           String part2,
                           boolean minInclusive,
                           boolean maxInclusive)
Returns a Query instance for doing range searches on this field type. SolrQueryParser currently passes part1 and part2 as null if they are '*' respectively. minInclusive and maxInclusive are both true currently by SolrQueryParser but that may change in the future. Also, other QueryParser implementations may have different semantics.

Sub-classes should override this method to provide their own range query implementation. They should strive to handle nulls in part1 and/or part2 as well as unequal minInclusive and maxInclusive parameters gracefully.

Parameters:
parser -
field - the schema field
part1 - the lower boundary of the range, nulls are allowed.
part2 - the upper boundary of the range, nulls are allowed
minInclusive - whether the minimum of the range is inclusive or not
maxInclusive - whether the maximum of the range is inclusive or not
Returns:
a Query instance to perform range search according to given parameters
See Also:
SolrQueryParser.getRangeQuery(String, String, String, boolean)

getFieldQuery

public Query getFieldQuery(QParser parser,
                           SchemaField field,
                           String externalVal)
Returns a Query instance for doing searches against a field.

Parameters:
parser - The QParser calling the method
field - The SchemaField of the field to search
externalVal - The String representation of the value to search
Returns:
The Query instance. This implementation returns a TermQuery but overriding queries may not