Tokenpublic class Token extends Object implements CloneableA Token is an occurence 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 (KeyWord In Context)
display, etc.
The type is an interned 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 {@link TermPositions#getPayloadLength()} and
{@link TermPositions#getPayload(byte[], int)} to retrieve the payloads from the index.
WARNING: The status of the Payloads feature is experimental.
The APIs introduced here might change in the future and will not be
supported anymore in such a case. |
Fields Summary |
---|
String | termText | int | startOffset | int | endOffset | String | type | Payload | payload | private int | positionIncrement |
Constructors Summary |
---|
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."
termText = text;
startOffset = start;
endOffset = end;
| public Token(String text, int start, int end, String typ)Constructs a Token with the given text, start and end offsets, & type.
termText = text;
startOffset = start;
endOffset = end;
type = typ;
|
Methods Summary |
---|
public java.lang.Object | clone()
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e); // shouldn't happen since we implement Cloneable
}
| 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. return endOffset;
| public org.apache.lucene.index.Payload | getPayload()Returns this Token's payload.
WARNING: The status of the Payloads feature is experimental.
The APIs introduced here might change in the future and will not be
supported anymore in such a case.
return this.payload;
| public int | getPositionIncrement()Returns the position increment of this Token. return positionIncrement;
| public void | setPayload(org.apache.lucene.index.Payload payload)Sets this Token's payload.
WARNING: The status of the Payloads feature is experimental.
The APIs introduced here might change in the future and will not be
supported anymore in such a case.
this.payload = payload;
| public void | setPositionIncrement(int positionIncrement)Set the position increment. This determines the position of this token
relative to the previous Token in a {@link 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.
if (positionIncrement < 0)
throw new IllegalArgumentException
("Increment must be zero or greater: " + positionIncrement);
this.positionIncrement = positionIncrement;
| public void | setTermText(java.lang.String text)Sets the Token's term text.
termText = text;
| 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 termText.length(), as the term text may have been altered by a
stemmer or some other filter. return startOffset;
| public final java.lang.String | termText()Returns the Token's term text. return termText;
| public java.lang.String | toString()
StringBuffer sb = new StringBuffer();
sb.append("(" + termText + "," + startOffset + "," + endOffset);
if (!type.equals("word"))
sb.append(",type="+type);
if (positionIncrement != 1)
sb.append(",posIncr="+positionIncrement);
sb.append(")");
return sb.toString();
| public final java.lang.String | type()Returns this Token's lexical type. Defaults to "word". return type;
|
|