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 ...
随机推荐
- JVM总括四-类加载过程、双亲委派模型、对象实例化过程
JVM总括四-类加载过程.双亲委派模型.对象实例化过程 目录:JVM总括:目录 一. 类加载过程 类加载过程就是将.class文件转化为Class对象,类实例化的过程,(User user = new ...
- Ubuntu postgres 内网 安装 卸载
# 安装pg,(使用安装包, 不能连接外网) tar包下载地址 https://www.postgresql.org/ftp/source/v11.1/放在/home/sxy 目录(随便放)cd /h ...
- 现代编译原理--第六章(中间树 IR Tree 含源码)
(转载请表明出处 http://www.cnblogs.com/BlackWalnut/p/4559717.html ) 这一章,就虎书而言,理论知识点是及其少的,就介绍了为什么要有一个中间表示树 ...
- AUTEL MaxiSys MS908S Pro MS908SP Diagnostic Platform
AUTEL MaxiSys MS908S Pro Description : One of the MaxiSys series devices, the MS908S Pro Diagnostic ...
- nagios 报警参数
host_notification_options: d = notify on DOWN host states, u = notify on UNREACHABLE host states r = ...
- SpringMVC 学习 十一 springMVC控制器向jsp或者别的控制器传递参数的四种方法
以后的开发,大部分是发送ajax,因此这四种传递参数的方法,并不太常用.作为了解吧 第一种:使用原生 Servlet 在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestSe ...
- 基于UML的公开招聘教师管理系统建模的研究和设计
一.基本信息 标题:基于UML的公开招聘教师管理系统建模的研究和设计 时间:2018 出版源:赤峰学院学报(自然科学版) 领域分类:UML:公开招聘教师系统:面向对象方法:建模. 二.研究背景 问题定 ...
- Mysql中比较常用的两种存储引擎和事务
存储引擎:引擎(类似汽车上的发动机)决定了数据库的快慢,MySql中有20多个引擎,不同的存储引擎提供不同的存储机制.索引技巧.锁定水平.MYISAM存储引擎,INNODB存储引擎最出名.数据库的核心 ...
- 我知道的nginx配置
1.nginx配置文件 2.配置访问域名 #京淘商品管理系统 server { listen 80; server_name manage.jt.com; location / { proxy_pas ...
- Mac 安装微软雅黑字体
https://www.jianshu.com/p/d8c34fff3483 1.找一台Windows电脑,打开字体文件夹C:\Windows\Fonts. 2.搜索"Calibri.微软雅 ...