| org.apache.lucene.analysis.br | Analyzer for Brazilian. |
| org.apache.lucene.analysis.cjk | Analyzer for Chinese, Japanese and Korean. |
| org.apache.lucene.analysis.cn | Analyzer for Chinese. |
| org.apache.lucene.analysis.cz | Analyzer for Czech. |
| org.apache.lucene.analysis.de | Analyzer for German. |
| org.apache.lucene.analysis.el | Analyzer for Greek. |
| org.apache.lucene.analysis.fr | Analyzer for French. |
| org.apache.lucene.analysis.ngram | |
| org.apache.lucene.analysis.nl | Analyzer for Dutch. |
| org.apache.lucene.analysis.payloads | Provides various convenience classes for creating payloads on Tokens. |
| org.apache.lucene.analysis.ru | Analyzer for Russian. |
| org.apache.lucene.analysis.sinks | Implementations of the SinkTokenizer that might be useful. |
| org.apache.lucene.analysis.snowball | org.apache.lucene.analysis.TokenFilter and org.apache.lucene.analysis.Analyzer implementations that use Snowball stemmers. |
| org.apache.lucene.analysis.standard | A fast grammar-based tokenizer constructed with JFlex. |
| org.apache.lucene.analysis.th |
| Analyzer | An Analyzer builds TokenStreams, which analyze text. | code | html |
| CharTokenizer | An abstract base class for simple, character-oriented tokenizers. | code | html |
| TokenFilter | A TokenFilter is a TokenStream whose input is another token stream. | code | html |
| TokenStream | A TokenStream enumerates the sequence of tokens, either from fields of a document or from query text. | code | html |
| Tokenizer | A Tokenizer is a TokenStream whose input is a Reader. | code | html |
| CachingTokenFilter | This class can be used if the Tokens of a TokenStream are intended to be consumed more than once. | code | html |
| CharArraySet | A simple class that stores Strings as char[]'s in a hash table. | code | html |
| CharArraySet.CharArraySetIterator | The Iterator |
code | html |
| ISOLatin1AccentFilter | A filter that replaces accented characters in the ISO Latin 1 character set (ISO-8859-1) by their unaccented equivalent. | code | html |
| KeywordAnalyzer | "Tokenizes" the entire stream as a single token. | code | html |
| KeywordTokenizer | Emits the entire input as a single token. | code | html |
| LengthFilter | Removes words that are too long and too short from the stream. | code | html |
| LetterTokenizer | A LetterTokenizer is a tokenizer that divides text at non-letters. | code | html |
| LowerCaseFilter | Normalizes token text to lower case. | code | html |
| LowerCaseTokenizer | LowerCaseTokenizer performs the function of LetterTokenizer and LowerCaseFilter together. | code | html |
| PerFieldAnalyzerWrapper | This analyzer is used to facilitate scenarios where different fields require different analysis techniques. | code | html |
| PorterStemFilter | Transforms the token stream as per the Porter stemming algorithm. | code | html |
| PorterStemmer | Stemmer, implementing the Porter Stemming Algorithm The Stemmer class transforms a word into its root form. | code | html |
| SimpleAnalyzer | An Analyzer that filters LetterTokenizer with LowerCaseFilter. | code | html |
| SinkTokenizer | A SinkTokenizer can be used to cache Tokens for use in an Analyzer | code | html |
| StopAnalyzer | Filters LetterTokenizer with LowerCaseFilter and StopFilter. | code | html |
| StopAnalyzer.SavedStreams | Filters LowerCaseTokenizer with StopFilter. | code | html |
| StopFilter | Removes stop words from a token stream. | code | html |
| TeeTokenFilter | Works in conjunction with the SinkTokenizer to provide the ability to set aside tokens that have already been analyzed. | code | html |
| Token | A Token is an occurence of a term from the text of a field. | code | html |
| WhitespaceAnalyzer | An Analyzer that uses WhitespaceTokenizer. | code | html |
| WhitespaceTokenizer | A WhitespaceTokenizer is a tokenizer that divides text at whitespace. | code | html |
| WordlistLoader | Loader for text files that represent a list of stopwords. | code | html |
| TestAnalyzers | Copyright 2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | code | html |
| TestPerFieldAnalzyerWrapper | Copyright 2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | code | html |
| TestStopAnalyzer | Copyright 2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | code | html |
API and code to convert text into indexable/searchable tokens. Covers org.apache.lucene.analysis.Analyzer and related classes.
Lucene, indexing and search library, accepts only plain text input.
Applications that build their search capabilities upon Lucene may support documents in various formats – HTML, XML, PDF, Word – just to name a few. Lucene does not care about the Parsing of these and other document formats, and it is the responsibility of the application using Lucene to use an appropriate Parser to convert the original format into plain text before passing that plain text to Lucene.
Plain text passed to Lucene for indexing goes through a process generally called tokenization – namely breaking of the input text into small indexing elements – Tokens . The way input text is broken into tokens very much dictates further capabilities of search upon that text. For instance, sentences beginnings and endings can be identified to provide for more accurate phrase and proximity searches (though sentence identification is not provided by Lucene).
In some cases simply breaking the input text into tokens is not enough – a deeper Analysis is needed, providing for several functions, including (but not limited to):
The analysis package provides the mechanism to convert Strings and Readers into tokens that can be indexed by Lucene. There are three main classes in the package from which all analysis processes are derived. These are:
The synergy between org.apache.lucene.analysis.Analyzer and org.apache.lucene.analysis.Tokenizer is sometimes confusing. To ease on this confusion, some clarifications:
Lucene Java provides a number of analysis capabilities, the most commonly used one being the org.apache.lucene.analysis.standard.StandardAnalyzer . Many applications will have a long and industrious life with nothing more than the StandardAnalyzer. However, there are a few other classes/packages that are worth mentioning:
Analysis is one of the main causes of performance degradation during indexing. Simply put, the more you analyze the slower the indexing (in most cases). Perhaps your application would be just fine using the simple org.apache.lucene.analysis.WhitespaceTokenizer combined with a org.apache.lucene.analysis.StopFilter . The contrib/benchmark library can be useful for testing out the speed of the analysis process.
Applications usually do not invoke analysis – Lucene does it for them:
Analyzer analyzer = new StandardAnalyzer(); // or any other analyzer
TokenStream ts = analyzer.tokenStream("myfield",new StringReader("some text goes here"));
Token t = ts.next();
while (t!=null) {
System.out.println("token: "+t));
t = ts.next();
}
Selecting the "correct" analyzer is crucial for search quality, and can also affect indexing and search performance. The "correct" analyzer differs between applications. Lucene java's wiki page AnalysisParalysis provides some data on "analyzing your analyzer". Here are some rules of thumb:
Creating your own Analyzer is straightforward. It usually involves either wrapping an existing Tokenizer and set of TokenFilters to create a new Analyzer or creating both the Analyzer and a Tokenizer or TokenFilter. Before pursuing this approach, you may find it worthwhile to explore the contrib/analyzers library and/or ask on the java-user@lucene.apache.org mailing list first to see if what you need already exists. If you are still committed to creating your own Analyzer or TokenStream derivation (Tokenizer or TokenFilter) have a look at the source code of any one of the many samples located in this package.
The following sections discuss some aspects of implementing your own analyzer.
When document.add(field) is called multiple times for the same field name, we could say that each such call creates a new section for that field in that document. In fact, a separate call to tokenStream(field,reader) would take place for each of these so called "sections". However, the default Analyzer behavior is to treat all these sections as one large section. This allows phrase search and proximity search to seamlessly cross boundaries between these "sections". In other words, if a certain field "f" is added like this:
document.add(new Field("f","first ends",...);
document.add(new Field("f","starts two",...);
indexWriter.addDocument(document);
Then, a phrase search for "ends starts" would find that document.
Where desired, this behavior can be modified by introducing a "position gap" between consecutive field "sections",
simply by overriding
Analyzer.getPositionIncrementGap(fieldName) :
Analyzer myAnalyzer = new StandardAnalyzer() {
public int getPositionIncrementGap(String fieldName) {
return 10;
}
};
By default, all tokens created by Analyzers and Tokenizers have a position increment of one. This means that the position stored for that token in the index would be one more than that of the previous token. Recall that phrase and proximity searches rely on position info.
If the selected analyzer filters the stop words "is" and "the", then for a document containing the string "blue is the sky", only the tokens "blue", "sky" are indexed, with position("sky") = 1 + position("blue"). Now, a phrase query "blue is the sky" would find that document, because the same analyzer filters the same stop words from that query. But also the phrase query "blue sky" would find that document.
If this behavior does not fit the application needs, a modified analyzer can be used, that would increment further the positions of tokens following a removed stop word, using org.apache.lucene.analysis.Token#setPositionIncrement(int) . This can be done with something like:
public TokenStream tokenStream(final String fieldName, Reader reader) {
final TokenStream ts = someAnalyzer.tokenStream(fieldName, reader);
TokenStream res = new TokenStream() {
public Token next() throws IOException {
int extraIncrement = 0;
while (true) {
Token t = ts.next();
if (t!=null) {
if (stopWords.contains(t.termText())) {
extraIncrement++; // filter this word
continue;
}
if (extraIncrement>0) {
t.setPositionIncrement(t.getPositionIncrement()+extraIncrement);
}
}
return t;
}
}
};
return res;
}
Now, with this modified analyzer, the phrase query "blue sky" would find that document.
But note that this is yet not a perfect solution, because any phrase query "blue w1 w2 sky"
where both w1 and w2 are stop words would match that document.
Few more use cases for modifying position increments are: