Class NGramTokenFilter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class NGramTokenFilter
    extends TokenFilter
    Tokenizes the input into n-grams of the given size(s).

    You must specify the required Version compatibility when creating a NGramTokenFilter. As of Lucene 4.4, this token filters:

    • handles supplementary characters correctly,
    • emits all n-grams for the same token at the same position,
    • does not modify offsets,
    • sorts n-grams by their offset in the original token first, then increasing length (meaning that "abc" will give "a", "ab", "abc", "b", "bc", "c").

    You can make this filter use the old behavior by providing a version < Version.LUCENE_44 in the constructor but this is not recommended as it will lead to broken TokenStreams that will cause highlighting bugs.

    If you were using this TokenFilter to perform partial highlighting, this won't work anymore since this filter doesn't update offsets. You should modify your analysis chain to use NGramTokenizer, and potentially override NGramTokenizer.isTokenChar(int) to perform pre-tokenization.

    • Constructor Detail

      • NGramTokenFilter

        public NGramTokenFilter​(Version version,
                                TokenStream input,
                                int minGram,
                                int maxGram)
        Creates NGramTokenFilter with given min and max n-grams.
        Parameters:
        version - Lucene version to enable correct position increments. See above for details.
        input - TokenStream holding the input to be tokenized
        minGram - the smallest n-gram to generate
        maxGram - the largest n-gram to generate
      • NGramTokenFilter

        public NGramTokenFilter​(Version version,
                                TokenStream input)
        Creates NGramTokenFilter with default min and max n-grams.
        Parameters:
        version - Lucene version to enable correct position increments. See above for details.
        input - TokenStream holding the input to be tokenized
    • Method Detail

      • incrementToken

        public final boolean incrementToken()
                                     throws java.io.IOException
        Returns the next token in the stream, or null at EOS.
        Specified by:
        incrementToken in class TokenStream
        Returns:
        false for end of stream; true otherwise
        Throws:
        java.io.IOException
      • reset

        public void reset()
                   throws java.io.IOException
        Description copied from class: TokenFilter
        This method is called by a consumer before it begins consumption using TokenStream.incrementToken().

        Resets this stream to a clean state. Stateful implementations must implement this method so that they can be reused, just as if they had been created fresh.

        If you override this method, always call super.reset(), otherwise some internal state will not be correctly reset (e.g., Tokenizer will throw IllegalStateException on further usage).

        NOTE: The default implementation chains the call to the input TokenStream, so be sure to call super.reset() when overriding this method.

        Overrides:
        reset in class TokenFilter
        Throws:
        java.io.IOException