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 ...
随机推荐
- 如何查询linux下BIOS信息
一般可以使用dmidecode命令(还有biosdecode命令可参考),背景知识如下: SMBIOS (System Management BIOS)是主板或系统制造者以标准格式显示产品管理信息所需 ...
- 设计模式 策略模式2 c++11
根据需求的不同 选择不同的策略算法 之前是保存的各种策略类的指针 这里直接使用 function bind 选择对应的算法 代码 // 005.cpp: 定义控制台应用程序的入口点. // #inc ...
- Spring的概念
一.思想 IOC: DI: 二.applicationContext&BeanFactory
- 深入浅出WPF文摘
第一部分 深入浅出XMAL 第一章 XMAL概览 第二章 从零起步认识XMAL 第三章 系统学习XMAL语法 第四章 X名称空间详解 第五章 控件与布局 GUI:图形化用户界面 逻辑树: 可视树: : ...
- c语言模拟c++的继承和多态
//C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...
- 《MarkMark学习笔记学习笔记》html学习笔记
iframe里有一个srcdoc属性,很有用! window.location.href=document.referrer//可以实现返回上一级页面并刷新 HTML5权威指南©®,比较老的书了,有些 ...
- Spring-Cloud之Eureka排坑之旅
1 快速demo 1.0 环境说明 Intelli IDEA+Spring Boot 1.1 新建工程chap52(通过New Project->Spring Initializer-> ...
- 前端vue框架 父组件与子组件之间的相互调用
子组件调用父组件东西: 1.在父组件与子组件契合的标签的的template模板中绑定 v-bind:自定义一个名字=“要调用的名字” 2.在子组件的script中props:["自定义的名字 ...
- Vuejs——(9)组件——props数据传递
版权声明:出处http://blog.csdn.net/qq20004604 目录(?)[+] 本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...
- .NET Core微服务之路:利用DotNetty实现一个简单的通信过程
上一篇我们已经全面的介绍过<基于gRPC服务发现与服务治理的方案>,我们先复习一下RPC的调用过程(笔者会在这一节的几篇文章中反复的强调这个过程调用方案),看下图