- Lucene 4 Cookbook
- Edwood Ng Vineeth Mohan
- 222字
- 2021-07-16 14:07:51
Defining custom analyzers
It's necessary to create a custom analyzer when the built-in analyzers do not provide the needed behaviors for your search application. To continue with our CourtesyTitleFilter example, we will create CourtesyTitleAnalyzer
.
The anatomy of an analyzer includes one tokenizer and one or more TokenFilters. We will build an Analyzer by extending from the Analyzer
abstract class and implement the createComponents method.
How to do it…
Here is the sample code for CourtesyTitleAnalyzer
:
public class CourtesyTitleAnalyzer extends Analyzer { @Override protected TokenStreamComponents createComponents(String fieldName, Reader reader) { Tokenizer letterTokenizer = new LetterTokenizer(reader); TokenStream filter = new CourtesyTitleFilter(letterTokenizer); return new TokenStreamComponents(letterTokenizer, filter); } }
How it works…
An Analyzer is created by extending from the Analyzer
abstract class as shown in this example. Then we override the createComponents
method, adding a LetterTokenizer
to split text by non-letter characters and CourtesyTitleFilter
as a TokenFilter. Finally, we return a new TokenStreamComponents
instance initialized by the instantiated Tokenizer
and TokenFilter
.
Note that the only method we need to override is createComponents
. We don't need to override the constructor to build our Analyzer because components are not added during construction; they are added when the createComponents
method is called. Therefore, we override the createComponents
method to customize an Analyzer. Also note that we cannot override the tokenStream
method because it's declared as final.
- C++面向對象程序設計(第三版)
- Modular Programming with Python
- AngularJS Testing Cookbook
- Apache ZooKeeper Essentials
- Learning Docker
- Java面向對象思想與程序設計
- Python計算機視覺編程
- 教孩子學編程:C++入門圖解
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- 人人都懂設計模式:從生活中領悟設計模式(Python實現)
- Mastering Predictive Analytics with Python
- Hands-On Reinforcement Learning with Python
- 深入分布式緩存:從原理到實踐
- OpenCV 3 Blueprints
- Mastering Adobe Captivate 7