Lucene的Suggest怎么使用
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章主要讲解了"Lucene的Suggest怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Lucene的Suggest怎么使用"吧!luc
千家信息网最后更新 2025年12月03日Lucene的Suggest怎么使用
这篇文章主要讲解了"Lucene的Suggest怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Lucene的Suggest怎么使用"吧!
lucene的联想词是在org.apache.lucene.search.suggest包下边,提供了自动补全或者联想提示功能的支持。
Suggest介绍
org.apache.lucene lucene-suggest 7.2.1
Suggest深入
Suggest用例
1. Controller层
@RestController@RequestMapping(value = "/suggest")public class SuggestController { @Resource private SuggestService suggestService; /** * 推荐词搜索 * @param keyword * @return */ @GetMapping(value = "/searchSuggest") public List searchSuggest(String keyword) { return suggestService.searchSuggest(keyword); }}访问地址:localhost:2000/spring-master/suggest/searchSuggest?keyword=胃造 2. Service层
@Slf4j@Servicepublic class SuggestServiceImpl implements SuggestService { private AnalyzingInfixSuggester suggester; /** * 内存存储:优点速度快,缺点程序退出数据就没了 */ protected RAMDirectory directory; /** * 索引分词 */ protected StandardAnalyzer indexAnalyzer; /** * 查询分词 */ protected StandardAnalyzer queryAnalyzer; @Override public List searchSuggest(String keyword) { List dictionaryList = new ArrayList(); HashSet contexts = new HashSet(); // 先根据region域进行suggest,再根据name域进行suggest// contexts.add(new BytesRef(region.getBytes("UTF8"))); // num决定了返回几条数据,参数四表明是否所有TermQuery是否都需要满足,参数五表明是否需要高亮显示 int num = 10; try { List results = suggester.lookup(keyword, num, true, false); for (Lookup.LookupResult result : results) { // result.key中存储的是根据用户输入内部算法进行匹配后返回的suggest内容 log.info("result_key: " + result.key); // 从载荷(payload)中反序列化出Product对象(实际生产中出于降低内存占用考虑一般不会在载荷中存储这么多内容) BytesRef bytesRef = result.payload; ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytesRef.bytes)); try { DictionaryVO dictionaryVO = (DictionaryVO) objectInputStream.readObject(); dictionaryList.add(dictionaryVO); } catch (ClassNotFoundException cnfe) { log.error(cnfe.getMessage()); } } } catch (Exception e) { log.error(e.getMessage()); } return dictionaryList; } /** * 初始化词典 * @return */ @PostConstruct protected void initSuggest() { directory = new RAMDirectory(); indexAnalyzer = new StandardAnalyzer(); queryAnalyzer = new StandardAnalyzer(); try { suggester = new AnalyzingInfixSuggester(directory, indexAnalyzer, queryAnalyzer, AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS, false); long start = System.currentTimeMillis(); // 读DictionaryVO数据 List diseases = FileUtils.readCsv(SuggestConstants.disease); List facultys = FileUtils.readCsv(SuggestConstants.faculty); List hospitals = FileUtils.readCsv(SuggestConstants.hospital); List drugcatalogues = FileUtils.readCsv(SuggestConstants.drugcatalogue); List doctors = FileUtils.readCsv(SuggestConstants.doctor); List allTerms = new ArrayList(); allTerms.addAll(facultys); allTerms.addAll(hospitals); allTerms.addAll(diseases); allTerms.addAll(drugcatalogues); allTerms.addAll(doctors); // 创建索引,根据InputIterator的具体实现决定数据源以及创建索引的规则 suggester.build(new DictionaryIterator(allTerms.iterator())); suggester.commit(); long end = System.currentTimeMillis(); log.info("It takes time to initialize the dictionary:" + (end - start)); this.initAfter(); } catch (IOException io) { log.error(io.getMessage()); } } protected void initAfter() { } /** * 销毁词典 */ @PreDestroy protected void destroy(){ try { if(suggester != null) { suggester.close(); } if(directory != null) { directory.close(); } } catch (IOException e) { log.error(e.getMessage(), e); } if(indexAnalyzer != null) { indexAnalyzer.close(); } if(queryAnalyzer != null) { queryAnalyzer.close(); } this.destroyAfter(); } protected void destroyAfter(){ }} 3. Util
@Slf4jpublic class FileUtils { /** * 读取词典csv文件 * @param fileNamePath * @return */ public static List readCsv(String fileNamePath) { List dictionarys = new ArrayList<>(); try { // 换成你的文件名 BufferedReader reader = new BufferedReader(new FileReader(fileNamePath)); String line; while ((line = reader.readLine()) != null) { // CSV格式文件为逗号分隔符文件,这里根据逗号切分 String[] item = line.split(","); dictionarys.add(new DictionaryVO(item[0], item[1], Long.parseLong(item[2]), Long.parseLong(item[3]))); } } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); } return dictionarys; }} 4. Core
package com.spring.master.lucene.suggest.core;import com.spring.master.lucene.suggest.vo.DictionaryVO;import org.apache.lucene.search.suggest.InputIterator;import org.apache.lucene.util.BytesRef;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.Set;/** * @author Huan Lee * @version 1.0 * @date 2020-09-10 14:55 * @describtion 核心类:决定了你的索引是如何创建的,决定了最终返回的提示关键词列表数据及其排序 * */public class DictionaryIterator implements InputIterator { private Iterator dictionaryIterator; private DictionaryVO currentDictionary; public DictionaryIterator(Iterator dictionaryIterator) { this.dictionaryIterator = dictionaryIterator; } @Override public long weight() { // TODO 这里可以设置权重 return currentDictionary.getWeight(); return 1; } /** * 将DictionaryVO对象序列化存入payload * @return */ @Override public BytesRef payload() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(currentDictionary); out.close(); return new BytesRef(bos.toByteArray()); } catch (IOException e) { throw new RuntimeException("Well that's unfortunate."); } } @Override public boolean hasPayloads() { return true; } /** * 设置是否启用Contexts域 * @return */ @Override public boolean hasContexts() { return false; } /** * 获取某个term的contexts,用来过滤suggest的内容,如果suggest的列表为空,返回null * @return */ @Override public Set contexts() {// try {// Set regions = new HashSet<>();// regions.add(new BytesRef(currentDictionary.getSourceType().getBytes("UTF8")));// return regions;// } catch (UnsupportedEncodingException e) {// throw new RuntimeException("Couldn't convert to UTF-8");// } return null; } @Override public BytesRef next() throws IOException { if (dictionaryIterator.hasNext()) { currentDictionary = dictionaryIterator.next(); try { //返回当前Project的name值,把product类的name属性值作为key return new BytesRef(currentDictionary.getWord().getBytes("UTF8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Couldn't convert to UTF-8",e); } } else { return null; } }} 5. Vo
@Datapublic class DictionaryVO implements Serializable { public DictionaryVO() { } public DictionaryVO(String word, String sourceType, Long sourceId, Long weight) { this.word = word; this.sourceId = sourceId; this.sourceType = sourceType; this.weight = weight; } /** * 词典 */ private String word; /** * 来源id */ private Long sourceId; /** * 来源:Doctor、Disease、Hospital、Faculty、Drugcatalogue */ private String sourceType; /** * 权重 */ private Long weight;}6. Constant
public class SuggestConstants { public static final String faculty = "/Users/lihuan/Documents/projects/git/me/faculty.csv"; public static final String hospital = "/Users/lihuan/Documents/projects/git/me/hospital.csv"; public static final String disease = "/Users/lihuan/Documents/projects/git/me/disease.csv"; public static final String drugcatalogue = "/Users/lihuan/Documents/projects/git/me/drugcatalogue.csv"; public static final String doctor = "/Users/lihuan/Documents/projects/git/me/doctor.csv";}感谢各位的阅读,以上就是"Lucene的Suggest怎么使用"的内容了,经过本文的学习后,相信大家对Lucene的Suggest怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
内容
数据
文件
索引
词典
存储
学习
内存
参数
对象
序列
权重
来源
载荷
逗号
UTF-8
提示
优点
关键
关键词
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网网络技术概况
数据服务器屏
洛雪音乐助手同步服务器
服务器架构工程师
三星s21谷歌服务器出现问题
福州网络生鲜软件开发
派出所如何实现下数据库
软件开发规范国家标准
第一篇mysql数据库详解重点
互联网信息科技公司发展
汽车软件开发工具分类
网络安全防御特色
马来西亚服务器设置
c井数据库sq乚
红包扫雷系统app软件开发
Srio软件开发需求 ddr3
知网数据库价格表
数据库搭建使用方法
软件开发的行业发展情况
启动服务器
南京市软件开发区
公司电话及网络技术方案
asp写入sql数据库
网站服务器推荐
易用web文件服务器
吉中多互联网科技有限公司
陕西连锁软件开发
国内网络安全市场分析
长春管理软件开发公司
r语言抓取亚马逊数据库