You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by "Steve Rowe (Confluence)" <co...@apache.org> on 2013/09/26 22:49:00 UTC

[CONF] Apache Solr Reference Guide > What Is A Tokenizer?

Space: Apache Solr Reference Guide (https://cwiki.apache.org/confluence/display/solr)
Page: What Is A Tokenizer? (https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=32604230)

Change Comment:
---------------------------------------------------------------------
add quotes around keyword tokenizer example input and output; removed link from "StandardTokenizer" to non-existent solr wiki page

Edited by Steve Rowe:
---------------------------------------------------------------------
The job of a [tokenizer|Tokenizers] is to break up a stream of text into tokens, where each token is (usually) a sub-sequence of the characters in the text. An analyzer is aware of the field it is configured for, but a tokenizer is not. Tokenizers read from a character stream (a Reader) and produce a sequence of Token objects (a TokenStream).

Characters in the input stream may be discarded, such as whitespace or other delimiters. They may also be added to or replaced, such as mapping aliases or abbreviations to normalized forms. A token contains various metadata in addition to its text value, such as the location at which the token occurs in the field. Because a tokenizer may produce tokens that diverge from the input text, you should not assume that the text of the token is the same text that occurs in the field, or that its length is the same as the original text.  It's also possible for more than one token to have the same position or refer to the same offset in the original text. Keep this in mind if you use token metadata for things like highlighting search results in the field text.

{code:xml|borderStyle=solid|borderColor=#666666}
<fieldType name="text" class="solr.TextField">
    <analyzer>
        <tokenizer class="solr.StandardTokenizerFactory"/>
    </analyzer>
</fieldType>
{code}

The class named in the tokenizer element is not the actual tokenizer, but rather a class that implements the {{org.apache.solr.analysis.TokenizerFactory}} interface. This factory class will be called upon to create new tokenizer instances as needed.  Objects created by the factory must derive from {{org.apache.lucene.analysis.TokenStream}}, which indicates that they produce sequences of tokens. If the tokenizer produces tokens that are usable as is, it may be the only component of the analyzer. Otherwise, the tokenizer's output tokens will serve as input to the first filter stage in the pipeline.

A {{TypeTokenFilterFactory}} is available that creates a {{TypeTokenFilter}} that filters tokens based on their TypeAttribute, which is set in {{factory.getStopTypes}}.

h2. When To use a CharFilter vs. a TokenFilter

There are  several pairs of CharFilters and TokenFilters that have related (ie: {{MappingCharFilter}} and {{ASCIIFoldingFilter}}) or nearly identical (ie: {{PatternReplaceCharFilterFactory}} and {{PatternReplaceFilterFactory}}) functionality and it may not always be obvious which is the best choice.

The decision about which to use depends largely on which Tokenizer you are using, and whether you need to preprocess the stream of characters.

For example, suppose you have a tokenizer such as {{StandardTokenizer}} and although you are pretty happy with how it works overall, you want to customize how some specific characters behave. You could modify the rules and re-build your own tokenizer with {{javacc}}, but it might be easier to simply map some of the characters before tokenization with a {{CharFilter}}.

h2. TokenizerFactories

Solr provides the following {{TokenizerFactories}} (Tokenizers and TokenFilters):

h3. {{solr.KeywordTokenizerFactory}}

Creates {{org.apache.lucene.analysis.core.KeywordTokenizer}}.

Treats the entire field as a single token, regardless of its content. For example:

{code:language=none|borderStyle=solid|borderColor=#666666}
"http://example.com/I-am+example?Text=-Hello" ==> "http://example.com/I-am+example?Text=-Hello"
{code}

h3. {{solr.LetterTokenizerFactory}}

Creates {{org.apache.lucene.analysis.LetterTokenizer}}.

Creates tokens consisting of strings of contiguous letters. Any non-letter characters will be discarded.  For example:

{code:language=none|borderStyle=solid|borderColor=#666666}
"I can't"==>"I","can","t"
{code}

h3. {{solr.WhitespaceTokenizerFactory}}

Creates {{org.apache.lucene.analysis.WhitespaceTokenizer}}.

Creates tokens of characters separated by splitting on white space.

h3. {{solr.LowerCaseTokenizerFactory}}

Creates {{org.apache.lucene.analysis.LowerCaseTokenizer}}.

Creates tokens by lowercasing all letters and dropping non-letters. For example:

{code:language=none|borderStyle=solid|borderColor=#666666}
"I can't"==>"i","can","t"
{code}

h3. {{solr.StandardTokenizerFactory}}

Creates {{org.apache.lucene.analysis.standard.StandardTokenizer}}.

A good general purpose tokenizer that strips many extraneous characters and sets token types to meaningful values.  Token types are only useful for subsequent token filters that are type-aware of the same token types.  There aren't any filters that use StandardTokenizer's types.

{scrollbar}


Stop watching space: https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=solr
Change email notification preferences: https://cwiki.apache.org/confluence/users/editmyemailsettings.action