Lucene 7.2.1 自定义Analyzer和TokenFilter
1.自定义Analyzer:
@Test
public void t01() throws Exception {
ArrayList<String> strings = new ArrayList<String>() {
{
this.add("小鬼子");
this.add("美国佬");
}
};
Analyzer analyzer = new CustomStandardAnalyzer(strings);
String content = "小鬼子 and 美国佬 are playing together!";
TokenStream tokenStream = analyzer.tokenStream("myfield", content);
tokenStream.reset();
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
// 已经过滤掉自定义停用词
// 输出:playing together
System.out.println(charTermAttribute.toString());
}
tokenStream.end();
tokenStream.close();
analyzer.close();
}
@Test
public void t02() throws Exception {
Analyzer analyzer = new SameWordAnalyzer();
String content = "这花美丽";
TokenStream tokenStream = analyzer.tokenStream("myfield", content);
tokenStream.reset();
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
System.out.println(charTermAttribute.toString());
}
tokenStream.end();
tokenStream.close();
analyzer.close();
}
2.自定义TokenFilter
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class SameWordTokenFilter extends TokenFilter {
private CharTermAttribute charTermAttribute;
private PositionIncrementAttribute positionIncrementAttribute;
private State state;
private Stack<String> stack;
public SameWordTokenFilter(TokenStream input) {
super(input);
this.stack = new Stack<>();
this.charTermAttribute = this.addAttribute(CharTermAttribute.class);
this.positionIncrementAttribute = this.addAttribute(PositionIncrementAttribute.class);
this.stack = new Stack<>();
}
@Override
public final boolean incrementToken() throws IOException {
while (this.stack.size() > 0) {
this.restoreState(this.state);
this.charTermAttribute.setEmpty();
this.charTermAttribute.append(this.stack.pop());
this.positionIncrementAttribute.setPositionIncrement(0);
return true;
}
if (!this.input.incrementToken()) {
return false;
}
String term = this.charTermAttribute.toString();
if (this.getSameWords(term)) {
this.state = this.captureState();
}
return true;
}
private boolean getSameWords(String name) {
Map<String, String[]> map = new HashMap<>();
map.put("美", new String[]{"美丽", "好看"});
map.put("花", new String[]{"鲜花", "花朵"});
String[] words = map.get(name);
if (words != null) {
for (String word : words) {
this.stack.push(word);
}
return true;
}
return false;
}
}
3.使用自定义Analyzer和自定义TokenFilter
ArrayList<String> strings = new ArrayList<String>() {{
this.add("小鬼子");
this.add("美国佬");
}};
Analyzer analyzer = new CustomStandardAnalyzer(strings);
String content = "小鬼子 and 美国佬 are playing together!";
TokenStream tokenStream = analyzer.tokenStream("myfield", content);
tokenStream.reset();
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
// 已经过滤掉自定义停用词
// 输出:playing together
System.out.println(charTermAttribute.toString());
}
tokenStream.end();
tokenStream.close();
analyzer.close();
4.代码解释,具体Analyzer和 TokenFilter之间的关联,用Eclipse的DEBUG功能,跟踪理解。
Lucene 7.2.1 自定义Analyzer和TokenFilter的更多相关文章
- Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer
原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...
- Lucene 7.2.1 自定义TokenFilter
1.自定义TokenFilter import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.To ...
- lucene分词器中的Analyzer,TokenStream, Tokenizer, TokenFilter
分词器的核心类: Analyzer:分词器 TokenStream: 分词器做优点理之后得到的一个流.这个流中存储了分词的各种信息,能够通过TokenStream有效的获取到分词单元. 下面是把文件流 ...
- lucene源码分析(7)Analyzer分析
1.Analyzer的使用 Analyzer使用在IndexWriter的构造方法 /** * Constructs a new IndexWriter per the settings given ...
- Lucene根据字段进行自定义搜索扩展
最近需要对公司的产品搜索功能做一步改动,搜索到的结果首先按照是否有库存进行排序,然后再按照销量.由于库存量也是一个整数,如果直接按照库存量进行倒序排序的话,是不符合要求的,Lucene也没有支持我们这 ...
- 多字段特性及配置自定义Analyzer
PUT logs/_doc/1 {"level":"DEBUG"} GET /logs/_mapping POST _analyze { "token ...
- Lucene 中自定义排序的实现
使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...
- ElasticSearch 启动时加载 Analyzer 源码分析
ElasticSearch 启动时加载 Analyzer 源码分析 本文介绍 ElasticSearch启动时如何创建.加载Analyzer,主要的参考资料是Lucene中关于Analyzer官方文档 ...
- lucene学习教程
1Lucene的介绍 ①Lucene是什么: 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎 ②Lu ...
随机推荐
- BZOJ2212或洛谷3521 [POI2011]ROT-Tree Rotations
BZOJ原题链接 洛谷原题链接 线段树合并裸题. 因为交换子树只会对子树内部的逆序对产生影响,所以我们计算交换前的逆序对个数和交换后的个数,取\(\min\)即可. 对每个叶子节点建一棵动态开点线段树 ...
- BZOJ4381 : [POI2015]Odwiedziny / Luogu3591[POI2015]ODW - 分块+树剖
Solution 在步伐$pace$比较小的时候, 我们发现用前缀和直接维护会很快 而在$pace$比较大的时候, 则暴力往上跳会最优 设$blo= \sqrt{N}$ 若$pace<=blo$ ...
- Spring Boot 启动(二) Environment 加载
Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...
- leveldb 学习记录(三) MemTable 与 Immutable Memtable
前文: leveldb 学习记录(一) skiplist leveldb 学习记录(二) Slice 存储格式: leveldb数据在内存中以 Memtable存储(核心结构是skiplist 已介绍 ...
- Python 多进程编程之multiprocessing--Process
Python 多进程编程之multiprocessing 1,Process 跨平台的进程创建模块(multiprocessing), 支持跨平台:windowx/linux 创建和启动 创 ...
- Firefox录制时浏览器提示代理服务器拒绝连接
解决方法:检查火狐浏览器的代理设置是否正确,在 菜单栏 工具->选项->高级->网络->连接->设置里.将“配置访问因特网的代理”选项改为“无代理”.
- 从服务器角度分析RPG游戏——NPC的AI
最近主程有些忙,甩给我一些服务器的代码,零零散散总结了一些要素. java程序架构也是层层分析,先罗列出需要做的工作,然后从主干到细节依次实现.就这点而言,程序和绘画有很多类似的地方. 关于怪物AI类 ...
- ActiveMQ_4SpringBoot整合
SpringBoot实现 引入jar包 <dependency> <groupId>org.springframework.boot</groupId> ...
- QT中报错collect2:ld returned 1 exit status的可能原因。
参考:https://blog.csdn.net/u014546553/article/details/78781547 1.编译成功的例子在后台执行,有时一闪而过,如果再次build ,则会提示上述 ...
- overlay fs挂载及操作测试
overlayfs是目前使用比较广泛的层次文件系统,实现简单,性能较好,可以充分利用不同或则相同overlay文件系统的page cache,具有 上下合并 同名遮盖 写时拷贝 等特点. 一个 ove ...