Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class DefaultPostingsFormatFactory

    Implements the default functionality of IPostingsFormatFactory.

    To replace the DefaultPostingsFormatFactory instance, call SetPostingsFormatFactory(IPostingsFormatFactory) at application start up. DefaultPostingsFormatFactory can be subclassed or passed additional parameters to register additional codecs, inject dependencies, or change caching behavior, as shown in the following examples. Alternatively, IPostingsFormatFactory can be implemented to provide complete control over postings format creation and lifetimes.

    Register Additional PostingsFormats

    Additional codecs can be added by initializing the instance of DefaultPostingsFormatFactory and passing an array of PostingsFormat-derived types.

    // Register the factory at application start up.
    PostingsFormat.SetPostingsFormatFactory(new DefaultPostingsFormatFactory {
        CustomPostingsFormatTypes = new Type[] { typeof(MyPostingsFormat), typeof(AnotherPostingsFormat) }
    });

    Only Use Explicitly Defined PostingsFormats

    PutPostingsFormatType(Type) can be used to explicitly add codec types. In this example, the call to base.Initialize() is excluded to skip the built-in codec registration. Since AnotherPostingsFormat doesn't have a default constructor, the NewPostingsFormat(Type) method is overridden to supply the required parameters.

    public class ExplicitPostingsFormatFactory : DefaultPostingsFormatFactory
    {
        protected override void Initialize()
        {
            // Load specific codecs in a specific order.
            PutPostingsFormatType(typeof(MyPostingsFormat));
            PutPostingsFormatType(typeof(AnotherPostingsFormat));
        }
    
        protected override PostingsFormat NewPostingsFormat(Type type)
        {
            // Special case: AnotherPostingsFormat has a required dependency
            if (typeof(AnotherPostingsFormat).Equals(type))
                return new AnotherPostingsFormat(new SomeDependency());
    
            return base.NewPostingsFormat(type);
        }
    }
    
    // Register the factory at application start up.
    PostingsFormat.SetPostingsFormatFactory(new ExplicitPostingsFormatFactory());

    See the Lucene.Net.Codecs namespace documentation for more examples of how to inject dependencies into PostingsFormat subclasses.

    Use Reflection to Scan an Assembly for PostingsFormats

    ScanForPostingsFormats(Assembly) or ScanForPostingsFormats(IEnumerable<Assembly>) can be used to scan assemblies using .NET Reflection for codec types and add all subclasses that are found automatically.

    public class ScanningPostingsFormatFactory : DefaultPostingsFormatFactory
    {
        protected override void Initialize()
        {
            // Load all default codecs
            base.Initialize();
    
            // Load all of the codecs inside of the same assembly that MyPostingsFormat is defined in
            ScanForPostingsFormats(typeof(MyPostingsFormat).Assembly);
        }
    }
    
    // Register the factory at application start up.
    PostingsFormat.SetPostingsFormatFactory(new ScanningPostingsFormatFactory());

    Postings formats in the target assembly can be excluded from the scan by decorating them with the ExcludePostingsFormatFromScanAttribute.

    Inheritance
    System.Object
    NamedServiceFactory<PostingsFormat>
    DefaultPostingsFormatFactory
    Implements
    IPostingsFormatFactory
    IServiceListable
    Inherited Members
    NamedServiceFactory<PostingsFormat>.m_initializationLock
    NamedServiceFactory<PostingsFormat>.EnsureInitialized()
    NamedServiceFactory<PostingsFormat>.CodecsAssembly
    NamedServiceFactory<PostingsFormat>.IsServiceType(Type)
    NamedServiceFactory<PostingsFormat>.GetServiceName(Type)
    NamedServiceFactory<PostingsFormat>.GetCanonicalName(Type)
    NamedServiceFactory<PostingsFormat>.IsFullyTrusted
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.ToString()
    Namespace: Lucene.Net.Codecs
    Assembly: Lucene.Net.dll
    Syntax
    public class DefaultPostingsFormatFactory : NamedServiceFactory<PostingsFormat>, IPostingsFormatFactory, IServiceListable

    Constructors

    | Improve this Doc View Source

    DefaultPostingsFormatFactory()

    Creates a new instance of DefaultPostingsFormatFactory.

    Declaration
    public DefaultPostingsFormatFactory()

    Properties

    | Improve this Doc View Source

    AvailableServices

    Gets a list of the available PostingsFormats (by name).

    Declaration
    public virtual ICollection<string> AvailableServices { get; }
    Property Value
    Type Description
    System.Collections.Generic.ICollection<System.String>

    A ICollection{string} of PostingsFormat names.

    | Improve this Doc View Source

    CustomPostingsFormatTypes

    An array of custom PostingsFormat-derived types to be registered. This property can be initialized during construction of DefaultPostingsFormatFactory to make your custom codecs known to Lucene.

    These types will be registered after the default Lucene types, so if a custom type has the same name as a Lucene PostingsFormat (via PostingsFormatNameAttribute) the custom type will replace the Lucene type with the same name.

    Declaration
    public IEnumerable<Type> CustomPostingsFormatTypes { get; set; }
    Property Value
    Type Description
    System.Collections.Generic.IEnumerable<System.Type>

    Methods

    | Improve this Doc View Source

    GetPostingsFormat(String)

    Gets the PostingsFormat instance from the provided name.

    Declaration
    public virtual PostingsFormat GetPostingsFormat(string name)
    Parameters
    Type Name Description
    System.String name

    The name of the PostingsFormat instance to retrieve.

    Returns
    Type Description
    PostingsFormat

    The PostingsFormat instance.

    | Improve this Doc View Source

    GetPostingsFormat(Type)

    Gets the PostingsFormat instance from the provided type.

    Declaration
    protected virtual PostingsFormat GetPostingsFormat(Type type)
    Parameters
    Type Name Description
    System.Type type

    The System.Type of PostingsFormat to retrieve.

    Returns
    Type Description
    PostingsFormat

    The PostingsFormat instance.

    | Improve this Doc View Source

    GetPostingsFormatType(String)

    Gets the PostingsFormat System.Type from the provided name.

    Declaration
    protected virtual Type GetPostingsFormatType(string name)
    Parameters
    Type Name Description
    System.String name

    The name of the PostingsFormat System.Type to retrieve.

    Returns
    Type Description
    System.Type

    The PostingsFormat System.Type.

    | Improve this Doc View Source

    Initialize()

    Initializes the codec type cache with the known PostingsFormat types. Override this method (and optionally call base.Initialize()) to add your own PostingsFormat types by calling PutPostingsFormatType(Type) or ScanForPostingsFormats(Assembly).

    If two types have the same name by using the PostingsFormatNameAttribute, the last one registered wins.

    Declaration
    protected override void Initialize()
    Overrides
    Lucene.Net.Util.NamedServiceFactory<Lucene.Net.Codecs.PostingsFormat>.Initialize()
    | Improve this Doc View Source

    NewPostingsFormat(Type)

    Instantiates a PostingsFormat based on the provided type.

    Declaration
    protected virtual PostingsFormat NewPostingsFormat(Type type)
    Parameters
    Type Name Description
    System.Type type

    The System.Type of PostingsFormat to instantiate.

    Returns
    Type Description
    PostingsFormat

    The new instance.

    | Improve this Doc View Source

    PutPostingsFormatType(Type)

    Adds a PostingsFormat type to the Lucene.Net.Codecs.DefaultPostingsFormatFactory.postingsFormatNameToTypeMap, using the name provided in the PostingsFormatNameAttribute, if present, or the name of the codec class minus the "Codec" suffix as the name by default.

    Note that if a PostingsFormat with the same name already exists in the map, calling this method will update it to the new type.

    Declaration
    protected virtual void PutPostingsFormatType(Type postingsFormat)
    Parameters
    Type Name Description
    System.Type postingsFormat

    A type that subclasses PostingsFormat.

    | Improve this Doc View Source

    ScanForPostingsFormats(IEnumerable<Assembly>)

    Scans the given assemblies for subclasses of Codec and adds their names to the Lucene.Net.Codecs.DefaultPostingsFormatFactory.postingsFormatNameToTypeMap. Note that names will be automatically overridden if the PostingsFormat name appears multiple times - the last match wins.

    Declaration
    protected virtual void ScanForPostingsFormats(IEnumerable<Assembly> assemblies)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<System.Reflection.Assembly> assemblies

    A list of assemblies to scan. The assemblies will be scanned from first to last, and the last match for each PostingsFormat name wins.

    | Improve this Doc View Source

    ScanForPostingsFormats(Assembly)

    Scans the given assembly for subclasses of PostingsFormat and adds their names to the Lucene.Net.Codecs.DefaultPostingsFormatFactory.postingsFormatNameToTypeMap. Note that names will be automatically overridden if the PostingsFormat name appears multiple times - the last match wins.

    Declaration
    protected virtual void ScanForPostingsFormats(Assembly assembly)
    Parameters
    Type Name Description
    System.Reflection.Assembly assembly

    The assembly to scan.

    Implements

    IPostingsFormatFactory
    IServiceListable

    See Also

    IPostingsFormatFactory
    IServiceListable
    ExcludePostingsFormatFromScanAttribute
    • Improve this Doc
    • View Source
    Back to top Copyright © 2021 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.