java.lang.Object
   ↳ org.apache.lucene.util.AttributeImpl
     ↳ org.apache.lucene.analysis.Token

Class Overview

A Token is an occurrence of a term from the text of a field. It consists of a term's text, the start and end offset of the term in the text of the field, and a type string.

The start and end offsets permit applications to re-associate a token with its source text, e.g., to display highlighted query terms in a document browser, or to show matching text fragments in a KWIC display, etc.

The type is a string, assigned by a lexical analyzer (a.k.a. tokenizer), naming the lexical or syntactic class that the token belongs to. For example an end of sentence marker token might be implemented with type "eos". The default token type is "word".

A Token can optionally have metadata (a.k.a. Payload) in the form of a variable length byte array. Use getPayloadLength() and getPayload(byte[], int) to retrieve the payloads from the index.

NOTE: As of 2.9, Token implements all Attribute interfaces that are part of core Lucene and can be found in the tokenattributes subpackage. Even though it is not necessary to use Token anymore, with the new TokenStream API it can be used as convenience class that implements all Attributes, which is especially useful to easily switch from the old to the new TokenStream API.

Tokenizers and TokenFilters should try to re-use a Token instance when possible for best performance, by implementing the incrementToken() API. Failing that, to create a new Token you should first use one of the constructors that starts with null text. To load the token from a char[] use setTermBuffer(char[], int, int). To load from a String use setTermBuffer(String) or setTermBuffer(String, int, int). Alternatively you can get the Token's termBuffer by calling either termBuffer(), if you know that your text is shorter than the capacity of the termBuffer or resizeTermBuffer(int), if there is any possibility that you may need to grow the buffer. Fill in the characters of your term into this buffer, with getChars(int, int, char[], int) if loading from a string, or with arraycopy(Object, int, Object, int, int), and finally call setTermLength(int) to set the length of the term text. See LUCENE-969 for details.

Typical Token reuse patterns:

  • Copying text from a string (type is reset to DEFAULT_TYPE if not specified):
        return reusableToken.reinit(string, startOffset, endOffset[, type]);
      
  • Copying some text from a string (type is reset to DEFAULT_TYPE if not specified):
        return reusableToken.reinit(string, 0, string.length(), startOffset, endOffset[, type]);
      
  • Copying text from char[] buffer (type is reset to DEFAULT_TYPE if not specified):
        return reusableToken.reinit(buffer, 0, buffer.length, startOffset, endOffset[, type]);
      
  • Copying some text from a char[] buffer (type is reset to DEFAULT_TYPE if not specified):
        return reusableToken.reinit(buffer, start, end - start, startOffset, endOffset[, type]);
      
  • Copying from one one Token to another (type is reset to DEFAULT_TYPE if not specified):
        return reusableToken.reinit(source.termBuffer(), 0, source.termLength(), source.startOffset(), source.endOffset()[, source.type()]);
      
A few things to note:
  • clear() initializes all of the fields to default values. This was changed in contrast to Lucene 2.4, but should affect no one.
  • Because TokenStreams can be chained, one cannot assume that the Token's current type is correct.
  • The startOffset and endOffset represent the start and offset in the source text, so be careful in adjusting them.
  • When caching a reusable token, clone it. When injecting a cached token into a stream that can be reset, clone it again.

See Also

Summary

Nested Classes
class Token.TokenAttributeFactory Expert: Creates a TokenAttributeFactory returning Token as instance for the basic attributes and for all other attributes calls the given delegate factory. 
Constants
String DEFAULT_TYPE
Fields
public static final AttributeSource.AttributeFactory TOKEN_ATTRIBUTE_FACTORY Convenience factory that returns Token as implementation for the basic attributes and return the default impl (with "Impl" appended) for all other attributes.
Public Constructors
Token()
Constructs a Token will null text.
Token(int start, int end)
Constructs a Token with null text and start & end offsets.
Token(int start, int end, String typ)
Constructs a Token with null text and start & end offsets plus the Token type.
Token(int start, int end, int flags)
Constructs a Token with null text and start & end offsets plus flags.
Token(String text, int start, int end)
Constructs a Token with the given term text, and start & end offsets.
Token(String text, int start, int end, String typ)
Constructs a Token with the given text, start and end offsets, & type.
Token(String text, int start, int end, int flags)
Constructs a Token with the given text, start and end offsets, & type.
Token(char[] startTermBuffer, int termBufferOffset, int termBufferLength, int start, int end)
Constructs a Token with the given term buffer (offset & length), start and end offsets
Public Methods
void clear()
Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default.
Token clone(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
Makes a clone, but replaces the term buffer & start/end offset in the process.
Object clone()
Shallow clone.
void copyTo(AttributeImpl target)
Copies the values from this Attribute into the passed-in target attribute.
final int endOffset()
Returns this Token's ending offset, one greater than the position of the last character corresponding to this token in the source text.
boolean equals(Object obj)
All values used for computation of hashCode() should be checked here for equality.
int getFlags()
EXPERIMENTAL: While we think this is here to stay, we may want to change it to be a long.
Payload getPayload()
Returns this Token's payload.
int getPositionIncrement()
Returns the position increment of this Token.
int hashCode()
Subclasses must implement this method and should compute a hashCode similar to this:
   public int hashCode() {
     int code = startOffset;
     code = code * 31 + endOffset;
     return code;
   }
 
see also equals(Object)
Token reinit(String newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
Token reinit(String newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, String newType)
Token reinit(String newTerm, int newStartOffset, int newEndOffset, String newType)
void reinit(Token prototype, char[] newTermBuffer, int offset, int length)
Copy the prototype token's fields into this one, with a different term.
Token reinit(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, String newType)
Token reinit(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
void reinit(Token prototype)
Copy the prototype token's fields into this one.
void reinit(Token prototype, String newTerm)
Copy the prototype token's fields into this one, with a different term.
Token reinit(String newTerm, int newStartOffset, int newEndOffset)
char[] resizeTermBuffer(int newSize)
Grows the termBuffer to at least size newSize, preserving the existing content.
void setEndOffset(int offset)
Set the ending offset.
void setFlags(int flags)
void setOffset(int startOffset, int endOffset)
Set the starting and ending offset.
void setPayload(Payload payload)
Sets this Token's payload.
void setPositionIncrement(int positionIncrement)
Set the position increment.
void setStartOffset(int offset)
Set the starting offset.
final void setTermBuffer(String buffer)
Copies the contents of buffer into the termBuffer array.
final void setTermBuffer(char[] buffer, int offset, int length)
Copies the contents of buffer, starting at offset for length characters, into the termBuffer array.
final void setTermBuffer(String buffer, int offset, int length)
Copies the contents of buffer, starting at offset and continuing for length characters, into the termBuffer array.
final void setTermLength(int length)
Set number of valid characters (length of the term) in the termBuffer array.
final void setType(String type)
Set the lexical type.
final int startOffset()
Returns this Token's starting offset, the position of the first character corresponding to this token in the source text.
final String term()
Returns the Token's term text.
final char[] termBuffer()
Returns the internal termBuffer character array which you can then directly alter.
final int termLength()
Return number of valid characters (length of the term) in the termBuffer array.
String toString()
The default implementation of this method accesses all declared fields of this object and prints the values in the following syntax:
   public String toString() {
     return "start=" + startOffset + ",end=" + endOffset;
   }
 
This method may be overridden by subclasses.
final String type()
Returns this Token's lexical type.
[Expand]
Inherited Methods
From class org.apache.lucene.util.AttributeImpl
From class java.lang.Object
From interface org.apache.lucene.analysis.tokenattributes.FlagsAttribute
From interface org.apache.lucene.analysis.tokenattributes.OffsetAttribute
From interface org.apache.lucene.analysis.tokenattributes.PayloadAttribute
From interface org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute
From interface org.apache.lucene.analysis.tokenattributes.TermAttribute
From interface org.apache.lucene.analysis.tokenattributes.TypeAttribute

Constants

public static final String DEFAULT_TYPE

Constant Value: "word"

Fields

public static final AttributeSource.AttributeFactory TOKEN_ATTRIBUTE_FACTORY

Convenience factory that returns Token as implementation for the basic attributes and return the default impl (with "Impl" appended) for all other attributes.

Public Constructors

public Token ()

Constructs a Token will null text.

public Token (int start, int end)

Constructs a Token with null text and start & end offsets.

Parameters
start start offset in the source text
end end offset in the source text

public Token (int start, int end, String typ)

Constructs a Token with null text and start & end offsets plus the Token type.

Parameters
start start offset in the source text
end end offset in the source text
typ the lexical type of this Token

public Token (int start, int end, int flags)

Constructs a Token with null text and start & end offsets plus flags. NOTE: flags is EXPERIMENTAL.

Parameters
start start offset in the source text
end end offset in the source text
flags The bits to set for this token

public Token (String text, int start, int end)

Constructs a Token with the given term text, and start & end offsets. The type defaults to "word." NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.

Parameters
text term text
start start offset
end end offset

public Token (String text, int start, int end, String typ)

Constructs a Token with the given text, start and end offsets, & type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.

Parameters
text term text
start start offset
end end offset
typ token type

public Token (String text, int start, int end, int flags)

Constructs a Token with the given text, start and end offsets, & type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.

Parameters
flags token type bits

public Token (char[] startTermBuffer, int termBufferOffset, int termBufferLength, int start, int end)

Constructs a Token with the given term buffer (offset & length), start and end offsets

Public Methods

public void clear ()

Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default.

public Token clone (char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)

Makes a clone, but replaces the term buffer & start/end offset in the process. This is more efficient than doing a full clone (and then calling setTermBuffer) because it saves a wasted copy of the old termBuffer.

public Object clone ()

Shallow clone. Subclasses must override this if they need to clone any members deeply,

public void copyTo (AttributeImpl target)

Copies the values from this Attribute into the passed-in target attribute. The target implementation must support all the Attributes this implementation supports.

public final int endOffset ()

Returns this Token's ending offset, one greater than the position of the last character corresponding to this token in the source text. The length of the token in the source text is (endOffset - startOffset).

public boolean equals (Object obj)

All values used for computation of hashCode() should be checked here for equality. see also equals(Object)

public int getFlags ()

EXPERIMENTAL: While we think this is here to stay, we may want to change it to be a long.

Get the bitset for any bits that have been set. This is completely distinct from type(), although they do share similar purposes. The flags can be used to encode information about the token for use by other TokenFilters.

Returns
  • The bits

public Payload getPayload ()

Returns this Token's payload.

public int getPositionIncrement ()

Returns the position increment of this Token.

public int hashCode ()

Subclasses must implement this method and should compute a hashCode similar to this:

   public int hashCode() {
     int code = startOffset;
     code = code * 31 + endOffset;
     return code;
   }
 
see also equals(Object)

public Token reinit (String newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)

Returns
  • this Token instance

public Token reinit (String newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, String newType)

public Token reinit (String newTerm, int newStartOffset, int newEndOffset, String newType)

Returns
  • this Token instance

public void reinit (Token prototype, char[] newTermBuffer, int offset, int length)

Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.

public Token reinit (char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, String newType)

Returns
  • this Token instance

public Token reinit (char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)

Returns
  • this Token instance

public void reinit (Token prototype)

Copy the prototype token's fields into this one. Note: Payloads are shared.

public void reinit (Token prototype, String newTerm)

Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.

public Token reinit (String newTerm, int newStartOffset, int newEndOffset)

Returns
  • this Token instance

public char[] resizeTermBuffer (int newSize)

Grows the termBuffer to at least size newSize, preserving the existing content. Note: If the next operation is to change the contents of the term buffer use setTermBuffer(char[], int, int), setTermBuffer(String), or setTermBuffer(String, int, int) to optimally combine the resize with the setting of the termBuffer.

Parameters
newSize minimum size of the new termBuffer
Returns
  • newly created termBuffer with length >= newSize

public void setEndOffset (int offset)

Set the ending offset.

See Also

public void setFlags (int flags)

See Also

public void setOffset (int startOffset, int endOffset)

Set the starting and ending offset.

See Also

public void setPayload (Payload payload)

Sets this Token's payload.

public void setPositionIncrement (int positionIncrement)

Set the position increment. This determines the position of this token relative to the previous Token in a TokenStream, used in phrase searching.

The default value is one.

Some common uses for this are:

  • Set it to zero to put multiple terms in the same position. This is useful if, e.g., a word has multiple stems. Searches for phrases including either stem will match. In this case, all but the first stem's increment should be set to zero: the increment of the first instance should be one. Repeating a token with an increment of zero can also be used to boost the scores of matches on that token.
  • Set it to values greater than one to inhibit exact phrase matches. If, for example, one does not want phrases to match across removed stop words, then one could build a stop word filter that removes stop words and also sets the increment to the number of stop words removed before each non-stop word. Then exact phrase queries will only match when the terms occur with no intervening stop words.

Parameters
positionIncrement the distance from the prior term
See Also

public void setStartOffset (int offset)

Set the starting offset.

See Also

public final void setTermBuffer (String buffer)

Copies the contents of buffer into the termBuffer array.

Parameters
buffer the buffer to copy

public final void setTermBuffer (char[] buffer, int offset, int length)

Copies the contents of buffer, starting at offset for length characters, into the termBuffer array.

Parameters
buffer the buffer to copy
offset the index in the buffer of the first character to copy
length the number of characters to copy

public final void setTermBuffer (String buffer, int offset, int length)

Copies the contents of buffer, starting at offset and continuing for length characters, into the termBuffer array.

Parameters
buffer the buffer to copy
offset the index in the buffer of the first character to copy
length the number of characters to copy

public final void setTermLength (int length)

Set number of valid characters (length of the term) in the termBuffer array. Use this to truncate the termBuffer or to synchronize with external manipulation of the termBuffer. Note: to grow the size of the array, use resizeTermBuffer(int) first.

Parameters
length the truncated length

public final void setType (String type)

Set the lexical type.

See Also

public final int startOffset ()

Returns this Token's starting offset, the position of the first character corresponding to this token in the source text. Note that the difference between endOffset() and startOffset() may not be equal to termLength(), as the term text may have been altered by a stemmer or some other filter.

public final String term ()

Returns the Token's term text. This method has a performance penalty because the text is stored internally in a char[]. If possible, use termBuffer() and termLength() directly instead. If you really need a String, use this method, which is nothing more than a convenience call to new String(token.termBuffer(), 0, token.termLength())

public final char[] termBuffer ()

Returns the internal termBuffer character array which you can then directly alter. If the array is too small for your token, use resizeTermBuffer(int) to increase it. After altering the buffer be sure to call setTermLength(int) to record the number of valid characters that were placed into the termBuffer.

public final int termLength ()

Return number of valid characters (length of the term) in the termBuffer array.

public String toString ()

The default implementation of this method accesses all declared fields of this object and prints the values in the following syntax:

   public String toString() {
     return "start=" + startOffset + ",end=" + endOffset;
   }
 
This method may be overridden by subclasses.

public final String type ()

Returns this Token's lexical type. Defaults to "word".