lucene全文检索

1.Lucene 是Apache开源的全文检索的工具包 创建索引 查询索引
2.遇到问题? 文件名 及文件内容 顺序扫描法 全文检索
3.什么是全文检索? 这种先创建索引 再对索引进行搜索的过程叫全文检索
4.索引是什么? 非结构数据中提取一个数据、并重新组合的过程叫索引

在这里插入图片描述入门程序
磁盘文件为原始文件
创建索引
第一步:获取文件
第二步:创建文档对象
第三步:创建分析器
第四步:保存索引及文档到索引库

搜索索引
第一步:用户接口(百度)
第二步:创建Query查询对象(KV)域名:值
第三步:执行查询
第四步:渲染

public class FirstLucene {
	//创建索引
	@Test
	public void testIndex() throws Exception {
		// 第一步:创建一个java工程,并导入jar包。
		// 第二步:创建一个indexwriter对象。
		Directory directory=  FSDirectory.open(new File("F:\\temp\\index"));
		Analyzer analyzer=new StandardAnalyzer();  //官方推荐
		IndexWriterConfig config=new IndexWriterConfig(Version.LATEST, analyzer); 
		IndexWriter indexwriter=new IndexWriter(directory, config);
		
			// 1)指定索引库的存放位置Directory对象
				// 2)指定一个分析器,对文档内容进行分析。
		// 第三步:创建field对象,将field添加到document对象中。
		File f=new File("F:\\Lucene$Solr\\Searchsource");
		File[] listFiles = f.listFiles();
		for (File file : listFiles) {
		// 第三步:创建document对象。
			Document document=new Document();
			//文件名称
			String file_name = file.getName();
			Field filenameField=new TextField("fileName", file_name, Store.YES);
			//文件大小
			long file_sizeOf = FileUtils.sizeOf(file);
			Field fileSizeField=new LongField("fileSize", file_sizeOf, Store.YES);
			//文件路径
			String file_path = file.getPath();
			Field filePathField=new StoredField("filePath", file_path);
			//文件内容
			String file_content = FileUtils.readFileToString(file);
			Field fileContentField=new TextField("fileContent", file_content, Store.YES);
			
			document.add(filenameField);
			document.add(fileSizeField);
			document.add(filePathField);
			document.add(fileContentField);
			// 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
			indexwriter.addDocument(document);
		}
		// 第五步:关闭IndexWriter对象。
		indexwriter.close();
	}
	
	// 搜索索引
		@Test
		public void testSearch() throws Exception {
			// 第一步:创建一个Directory对象,也就是索引库存放的位置。
			Directory directory=FSDirectory.open(new File("F:\\temp\\index"));   //磁盘硬盘内存保存索引
			// 第二步:创建一个indexReader对象,需要指定Directory对象
			IndexReader indexReader=DirectoryReader.open(directory);
			// 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
			IndexSearcher indexsearcher=new IndexSearcher(indexReader);
			// 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词
			Query query =new TermQuery(new Term("fileName", "spring"));
			// 第五步:执行查询
			TopDocs topDocs = indexsearcher.search(query, 2);
			// 第六步:返回查询结果。遍历查询结果并输出
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for (ScoreDoc scoreDoc : scoreDocs) {
				int doc=scoreDoc.doc;
				Document document = indexsearcher.doc(doc);
				//文件名称
				String fileName = document.get("fileName");
				System.out.println(fileName);
				//文件内容
				String fileContent = document.get("fileContent");
				System.out.println(fileContent);
				//文件路径
				String filePath = document.get("filePath");
				System.out.println(filePath);
				//文件大小
				String fileSize = document.get("fileSize");
				System.out.println(fileSize);
			}
			// 第七步:关闭IndexReader对象
			indexReader.close();
		}
		
		// 查看标准分析器的分词效果
		@Test
		public void testTokenStream() throws Exception {
			// 创建一个标准分析器对象
//			Analyzer analyzer = new StandardAnalyzer();
//			Analyzer analyzer = new CJKAnalyzer();
//			Analyzer analyzer = new SmartChineseAnalyzer();
			Analyzer analyzer = new IKAnalyzer();
			// 获得tokenStream对象
			// 第一个参数:域名,可以随便给一个
			// 第二个参数:要分析的文本内容
//			TokenStream tokenStream = analyzer.tokenStream("test",
//					"The Spring Framework provides a comprehensive programming and configuration model.");
			TokenStream tokenStream = analyzer.tokenStream("test",
					"高富帅可以用二维表结构来逻辑表达实现的数据");
			// 添加一个引用,可以获得每个关键词
			CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
			// 添加一个偏移量的引用,记录了关键词的开始位置以及结束位置
			OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
			// 将指针调整到列表的头部
			tokenStream.reset();
			// 遍历关键词列表,通过incrementToken方法判断列表是否结束
			while (tokenStream.incrementToken()) {
				// 关键词的起始位置
				System.out.println("start->" + offsetAttribute.startOffset());
				// 取关键词
				System.out.println(charTermAttribute);
				// 结束位置
				System.out.println("end->" + offsetAttribute.endOffset());
			}
			tokenStream.close();
		}
}

分析器(中文分析器)
标准
中日韩
SmartChineseAnalyzer
IKAnalyzer(扩展、停止)

使用IK分析器
第一步:导入IK.jar
第二步:复制IKAnalyzer.cfg.xml
第三步:复制stopword.dic(停止:不需要分析的去掉)
复制ext.dic(扩展:里面的字分析成一组词)
全放在classpath(src)下

在这里插入图片描述在这里插入图片描述
IKAnalyzer.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">ext.dic;</entry> 
	
	<!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">stopword.dic;</entry> 
	
</properties>

Lucene的维护
添加
删除
修改
查询

高级查询
第一个:全查询 MatchAllDocsQuery

第二个:TermQuery 精准查询
第三个:区间查询(根据数值)L
第四个:组合查询BooleadQuery 多个 AND OR NOT Occur.MUST MUST_NOT SHOULD

解析进行查询

//条件解析的对象查询
	@Test
	public void testQueryParser() throws Exception {
		IndexSearcher indexSearcher = getIndexSearcher();
		//参数1:默认查询的域
		//参数2:采用的分析器
		QueryParser querParser=new QueryParser("fileName", new IKAnalyzer());
		//  *:*   域:值
		Query query =querParser.parse("fileName:apache");
		
		
		printResult(indexSearcher, query);
		//关闭资源
		indexSearcher.getIndexReader().close();
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Lucene实现对MySQL数据表的全文检索是一种可行的方案。Lucene是一套开源的全文检索和搜寻的程式库,它提供了一个简单但强大的应用程式接口,能够实现全文索引和搜寻的功能。在Java开发环境中,Lucene是一个成熟的免费开源工具,被广泛应用于信息检索领域。 全文检索是一种针对非结构化数据的检索方法,对于像磁盘上的文件、网站资源等非结构化数据,无法使用SQL语句进行查询,因此需要使用全文检索法。全文检索法将非结构化数据中的一部分信息提取出来进行组织,形成索引,然后根据索引快速定位到要查找的信息。Lucene可以实现全文检索的功能,它是Apache软件基金会支持和提供的工具包。 使用Lucene实现全文检索的流程如下: 1. 创建索引:首先获取要进行检索的文档,可以是磁盘文件或网站资源等,然后构建文档对象,包括多个域,如文件名称、文件路径、文件大小、文件内容等。接下来对文档进行分词,将分词结果创建为索引并添加到索引库中。 2. 索引搜索:创建查询对象,执行查询并渲染结果。在倒排索引词典表中查找对应搜索词的索引,然后找到索引所链接的文档。例如,搜索语法为"fileName:lucene"表示搜索文件名中包含Lucene的文档。 要使用Lucene实现全文检索,首先需要下载和配置Lucene。你可以从官方网站或其他可信的资源下载Lucene的安装包,并按照相应的指南进行配置。然后,你可以使用Lucene提供的API来实现全文检索功能,根据具体需求进行代码编写和调用。 总之,Lucene是一种强大的全文检索工具,可以帮助你在MySQL数据表中实现全文检索功能。你可以通过学习和使用Lucene的API来了解更多关于Lucene的功能和用法,并根据具体需求进行相应的实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【springboot微服务】Lucence实现Mysql全文检索](https://blog.csdn.net/zhangcongyi420/article/details/129940816)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【Lucene&Solr】Lucene实现全文检索](https://blog.csdn.net/qq_43705275/article/details/107229299)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值