Class IndexSchema

  • Direct Known Subclasses:
    ManagedIndexSchema

    public class IndexSchema
    extends Object
    IndexSchema contains information about the valid fields in an index and the types of those fields.
    • Method Detail

      • getResourceLoader

        public SolrResourceLoader getResourceLoader()
        The resource loader to be used to load components related to the schema when the schema is loading / initialising. It should not be used for any other purpose or time; consider SolrCore.getResourceLoader() instead.
        Since:
        solr 1.4
      • getResourceName

        public String getResourceName()
        Gets the name of the resource used to instantiate this schema.
      • setResourceName

        public void setResourceName​(String resourceName)
        Sets the name of the resource used to instantiate this schema.
      • getSchemaName

        public String getSchemaName()
        Gets the name of the schema as specified in the schema resource.
      • getDefaultLuceneMatchVersion

        public org.apache.lucene.util.Version getDefaultLuceneMatchVersion()
        The Default Lucene Match Version for this IndexSchema
      • getVersion

        public float getVersion()
      • getFields

        public Map<String,​SchemaField> getFields()
        Provides direct access to the Map containing all explicit (ie: non-dynamic) fields in the index, keyed on field name.

        Modifying this Map (or any item in it) will affect the real schema

        NOTE: this function is not thread safe. However, it is safe to use within the standard inform( SolrCore core ) function for SolrCoreAware classes. Outside inform, this could potentially throw a ConcurrentModificationException

      • getFieldTypes

        public Map<String,​FieldType> getFieldTypes()
        Provides direct access to the Map containing all Field Types in the index, keyed on field type name.

        Modifying this Map (or any item in it) will affect the real schema. However if you make any modifications, be sure to call refreshAnalyzers() to update the Analyzers for the registered fields.

        NOTE: this function is not thread safe. However, it is safe to use within the standard inform( SolrCore core ) function for SolrCoreAware classes. Outside inform, this could potentially throw a ConcurrentModificationException

      • getFieldsWithDefaultValue

        public List<SchemaField> getFieldsWithDefaultValue()
        Provides direct access to the List containing all fields with a default value
      • getRequiredFields

        public Collection<SchemaField> getRequiredFields()
        Provides direct access to the List containing all required fields. This list contains all fields with default values.
      • getSimilarity

        public org.apache.lucene.search.similarities.Similarity getSimilarity()
        Returns the Similarity used for this index
      • getSimilarityFactory

        public SimilarityFactory getSimilarityFactory()
        Returns the SimilarityFactory that constructed the Similarity for this index
      • getIndexAnalyzer

        public org.apache.lucene.analysis.Analyzer getIndexAnalyzer()
        Returns the Analyzer used when indexing documents for this index

        This Analyzer is field (and dynamic field) name aware, and delegates to a field specific Analyzer based on the field type.

      • getQueryAnalyzer

        public org.apache.lucene.analysis.Analyzer getQueryAnalyzer()
        Returns the Analyzer used when searching this index

        This Analyzer is field (and dynamic field) name aware, and delegates to a field specific Analyzer based on the field type.

      • getUniqueKeyField

        public SchemaField getUniqueKeyField()
        Unique Key field specified in the schema file
        Returns:
        null if this schema has no unique key field
      • getUniqueKeyField

        public org.apache.lucene.index.IndexableField getUniqueKeyField​(org.apache.lucene.document.Document doc)
        The raw (field type encoded) value of the Unique Key field for the specified Document
        Returns:
        null if this schema has no unique key field
        See Also:
        printableUniqueKey(org.apache.lucene.document.Document)
      • printableUniqueKey

        public String printableUniqueKey​(org.apache.lucene.document.Document doc)
        The printable value of the Unique Key field for the specified Document
        Returns:
        null if this schema has no unique key field
      • refreshAnalyzers

        public void refreshAnalyzers()
        This will re-create the Analyzers. If you make any modifications to the Field map (getFields(), this function is required to synch the internally cached field analyzers.
        Since:
        solr 1.3
      • isMutable

        public boolean isMutable()
      • readSchema

        protected void readSchema​(InputSource is)
      • postReadInform

        protected void postReadInform()
      • isValidFieldGlob

        protected static boolean isValidFieldGlob​(String name)
        Returns true if the given name has exactly one asterisk either at the start or end of the name
      • registerDynamicFields

        public void registerDynamicFields​(SchemaField... fields)
        Register one or more new Dynamic Fields with the Schema.
        Parameters:
        fields - The sequence of SchemaField
      • registerCopyField

        public void registerCopyField​(String source,
                                      String dest)
      • registerCopyField

        public void registerCopyField​(String source,
                                      String dest,
                                      int maxChars)

        NOTE: this function is not thread safe. However, it is safe to use within the standard inform( SolrCore core ) function for SolrCoreAware classes. Outside inform, this could potentially throw a ConcurrentModificationException

        See Also:
        SolrCoreAware
      • registerExplicitSrcAndDestFields

        protected void registerExplicitSrcAndDestFields​(String source,
                                                        int maxChars,
                                                        SchemaField destSchemaField,
                                                        SchemaField sourceSchemaField)
      • getDynamicFieldPrototypes

        public SchemaField[] getDynamicFieldPrototypes()
      • getDynamicPattern

        public String getDynamicPattern​(String fieldName)
      • hasExplicitField

        public boolean hasExplicitField​(String fieldName)
        Does the schema explicitly define the specified field, i.e. not as a result of a copyField declaration? We consider it explicitly defined if it matches a field name or a dynamicField name.
        Returns:
        true if explicitly declared in the schema.
      • isDynamicField

        public boolean isDynamicField​(String fieldName)
        Is the specified field dynamic or not.
        Returns:
        true if the specified field is dynamic
      • getFieldTypeNoEx

        public FieldType getFieldTypeNoEx​(String fieldName)
        Returns the FieldType for the specified field name.

        This method exists because it can be more efficient then getField(java.lang.String) for dynamic fields if a full SchemaField isn't needed.

        Parameters:
        fieldName - may be an explicitly created field, or a name that exercises a dynamic field.
        Returns:
        null if field is not defined.
        See Also:
        getField(String), getFieldTypeNoEx(java.lang.String)
      • getCopySources

        public List<String> getCopySources​(String destField)
        Get all copy fields, both the static and the dynamic ones.
        Returns:
        Array of fields copied into this field
      • getCopyFieldsList

        public List<CopyField> getCopyFieldsList​(String sourceField)
        Get all copy fields for a specified source field, both static and dynamic ones.
        Returns:
        List of CopyFields to copy to.
        Since:
        solr 1.4
      • isCopyFieldTarget

        public boolean isCopyFieldTarget​(SchemaField f)
        Check if a field is used as the destination of a copyField operation
        Since:
        solr 1.3
      • getNamedPropertyValues

        public Map getNamedPropertyValues()
        Get a map of property name -> value for the whole schema.
      • getCopyFieldProperties

        public List<SimpleOrderedMap<Object>> getCopyFieldProperties​(boolean showDetails,
                                                                     Set<String> requestedSourceFields,
                                                                     Set<String> requestedDestinationFields)
        Returns a list of copyField directives, with optional details and optionally restricting to those directives that contain the requested source and/or destination field names.
        Parameters:
        showDetails - If true, source and destination dynamic bases, and explicit fields matched by source globs, will be added to dynamic copyField directives where appropriate
        requestedSourceFields - If not null, output is restricted to those copyField directives with the requested source field names
        requestedDestinationFields - If not null, output is restricted to those copyField directives with the requested destination field names
        Returns:
        a list of copyField directives
      • addField

        public IndexSchema addField​(SchemaField newField,
                                    boolean persist)
        Copies this schema, adds the given field to the copy Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        newField - the SchemaField to add
        persist - to persist the schema or not
        Returns:
        a new IndexSchema based on this schema with newField added
        See Also:
        newField(String, String, Map)
      • addField

        public IndexSchema addField​(SchemaField newField,
                                    Collection<String> copyFieldNames)
        Copies this schema, adds the given field to the copy Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        newField - the SchemaField to add
        copyFieldNames - 0 or more names of targets to copy this field to. The targets must already exist.
        Returns:
        a new IndexSchema based on this schema with newField added
        See Also:
        newField(String, String, Map)
      • addFields

        public IndexSchema addFields​(Collection<SchemaField> newFields,
                                     Map<String,​Collection<String>> copyFieldNames,
                                     boolean persist)
        Copies this schema, adds the given fields to the copy Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        newFields - the SchemaFields to add
        copyFieldNames - 0 or more names of targets to copy this field to. The target fields must already exist.
        persist - Persist the schema or not
        Returns:
        a new IndexSchema based on this schema with newFields added
        See Also:
        newField(String, String, Map)
      • deleteFields

        public IndexSchema deleteFields​(Collection<String> names)
        Copies this schema, deletes the named fields from the copy.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        names - the names of the fields to delete
        Returns:
        a new IndexSchema based on this schema with the named fields deleted
      • replaceField

        public IndexSchema replaceField​(String fieldName,
                                        FieldType replacementFieldType,
                                        Map<String,​?> replacementArgs)
        Copies this schema, deletes the named field from the copy, creates a new field with the same name using the given args, then rebinds any referring copy fields to the replacement field.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        fieldName - The name of the field to be replaced
        replacementFieldType - The field type of the replacement field
        replacementArgs - Initialization params for the replacement field
        Returns:
        a new IndexSchema based on this schema with the named field replaced
      • addDynamicFields

        public IndexSchema addDynamicFields​(Collection<SchemaField> newDynamicFields,
                                            Map<String,​Collection<String>> copyFieldNames,
                                            boolean persist)
        Copies this schema, adds the given dynamic fields to the copy, Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        newDynamicFields - the SchemaFields to add
        copyFieldNames - 0 or more names of targets to copy this field to. The target fields must already exist.
        persist - to persist the schema or not
        Returns:
        a new IndexSchema based on this schema with newDynamicFields added
        See Also:
        newDynamicField(String, String, Map)
      • deleteDynamicFields

        public IndexSchema deleteDynamicFields​(Collection<String> fieldNamePatterns)
        Copies this schema, deletes the named dynamic fields from the copy.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        fieldNamePatterns - the names of the dynamic fields to delete
        Returns:
        a new IndexSchema based on this schema with the named dynamic fields deleted
      • replaceDynamicField

        public ManagedIndexSchema replaceDynamicField​(String fieldNamePattern,
                                                      FieldType replacementFieldType,
                                                      Map<String,​?> replacementArgs)
        Copies this schema, deletes the named dynamic field from the copy, creates a new dynamic field with the same field name pattern using the given args, then rebinds any referring dynamic copy fields to the replacement dynamic field.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        fieldNamePattern - The glob for the dynamic field to be replaced
        replacementFieldType - The field type of the replacement dynamic field
        replacementArgs - Initialization params for the replacement dynamic field
        Returns:
        a new IndexSchema based on this schema with the named dynamic field replaced
      • addCopyFields

        public IndexSchema addCopyFields​(Map<String,​Collection<String>> copyFields,
                                         boolean persist)
        Copies this schema and adds the new copy fields to the copy Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        copyFields - Key is the name of the source field name, value is a collection of target field names. Fields must exist.
        persist - to persist the schema or not
        Returns:
        The new Schema with the copy fields added
        See Also:
        to limit the number of copied characters.
      • addCopyFields

        public IndexSchema addCopyFields​(String source,
                                         Collection<String> destinations,
                                         int maxChars)
        Copies this schema and adds the new copy fields to the copy. Requires synchronizing on the object returned by getSchemaUpdateLock()
        Parameters:
        source - source field name
        destinations - collection of target field names
        maxChars - max number of characters to copy from the source to each of the destinations. Use CopyField.UNLIMITED if you don't want to limit the number of copied chars.
        Returns:
        The new Schema with the copy fields added
      • deleteCopyFields

        public IndexSchema deleteCopyFields​(Map<String,​Collection<String>> copyFields)
        Copies this schema and deletes the given copy fields from the copy.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        copyFields - Key is the name of the source field name, value is a collection of target field names. Each corresponding copy field directives must exist.
        Returns:
        The new Schema with the copy fields deleted
      • newField

        public SchemaField newField​(String fieldName,
                                    String fieldType,
                                    Map<String,​?> options)
        Returns a SchemaField if the given fieldName does not already exist in this schema, and does not match any dynamic fields in this schema. The resulting SchemaField can be used in a call to addField(SchemaField).
        Parameters:
        fieldName - the name of the field to add
        fieldType - the field type for the new field
        options - the options to use when creating the SchemaField
        Returns:
        The created SchemaField
        See Also:
        addField(SchemaField)
      • newDynamicField

        public SchemaField newDynamicField​(String fieldNamePattern,
                                           String fieldType,
                                           Map<String,​?> options)
        Returns a SchemaField if the given dynamic field glob does not already exist in this schema, and does not match any dynamic fields in this schema. The resulting SchemaField can be used in a call to addField(SchemaField).
        Parameters:
        fieldNamePattern - the pattern for the dynamic field to add
        fieldType - the field type for the new field
        options - the options to use when creating the SchemaField
        Returns:
        The created SchemaField
        See Also:
        addField(SchemaField)
      • getSchemaUpdateLock

        public Object getSchemaUpdateLock()
        Returns the schema update lock that should be synchronized on to update the schema. Only applicable to mutable schemas.
        Returns:
        the schema update lock object to synchronize on
      • addFieldTypes

        public IndexSchema addFieldTypes​(List<FieldType> fieldTypeList,
                                         boolean persist)
        Copies this schema, adds the given field type to the copy, Requires synchronizing on the object returned by getSchemaUpdateLock().
        Parameters:
        fieldTypeList - a list of FieldTypes to add
        persist - to persist the schema or not
        Returns:
        a new IndexSchema based on this schema with the new types added
        See Also:
        newFieldType(String, String, Map)
      • deleteFieldTypes

        public IndexSchema deleteFieldTypes​(Collection<String> names)
        Copies this schema, deletes the named field types from the copy.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        names - the names of the field types to delete
        Returns:
        a new IndexSchema based on this schema with the named field types deleted
      • replaceFieldType

        public IndexSchema replaceFieldType​(String typeName,
                                            String replacementClassName,
                                            Map<String,​Object> replacementArgs)
        Copies this schema, deletes the named field type from the copy, creates a new field type with the same name using the given args, rebuilds fields and dynamic fields of the given type, then rebinds any referring copy fields to the rebuilt fields.

        The schema will not be persisted.

        Requires synchronizing on the object returned by getSchemaUpdateLock().

        Parameters:
        typeName - The name of the field type to be replaced
        replacementClassName - The class name of the replacement field type
        replacementArgs - Initialization params for the replacement field type
        Returns:
        a new IndexSchema based on this schema with the named field type replaced
      • getFieldTypeXPathExpressions

        protected String getFieldTypeXPathExpressions()
      • isUsableForChildDocs

        public boolean isUsableForChildDocs()
        Helper method that returns true if the ROOT_FIELD_NAME uses the exact same 'type' as the getUniqueKeyField()
        NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
      • savesChildDocRelations

        public boolean savesChildDocRelations()
        Helper method that returns true if the ROOT_FIELD_NAME uses the exact same 'type' as the getUniqueKeyField() and has NEST_PATH_FIELD_NAME defined as a NestPathField
        NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
      • getPayloadDecoder

        public org.apache.lucene.queries.payloads.PayloadDecoder getPayloadDecoder​(String field)