Lucene的一个简单的标准测试(Lucene包基于3.5版本的)

2024-06-23 18:38

本文主要是介绍Lucene的一个简单的标准测试(Lucene包基于3.5版本的),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Lucene编程一般分为:索引、分词、搜索

索引源代码:

package lucene的一个标准测试;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;public class TextFileIndexer {public static void main(String[] args) throws Exception {// 需要索引的源文件位置File fileDir = new File("E:\\Lucene\\source");// 存放索引的文件位置File indexDir = new File("E:\\Lucene\\index");// 把索引建立在硬盘中Directory dir = FSDirectory.open(indexDir);// 建立一个标准分词器Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_35);//IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,luceneAnalyzer);// 每次执行都是创建新的索引而不是追加索引iwc.setOpenMode(OpenMode.CREATE);// 创建一个索引器IndexWriter indexWriter = new IndexWriter(dir, iwc);// 保存源文件的多个文件到数组File[]中File[] textFiles = fileDir.listFiles();// 索引的开始时间long startTime = new Date().getTime();// for循环遍历源目录下的文件for (int i = 0; i < textFiles.length; i++) {if (textFiles[i].isFile()&& textFiles[i].getName().endsWith(".txt")) { // 获取相对路径???System.out.println("文件" + textFiles[i].getCanonicalPath()+ "正在被索引...");// 调用自定义读取文件内容的方法FileReaderAll()String temp = FileReaderAll(textFiles[i].getCanonicalPath(),"GBK");// 打印读取到的内容System.out.println(temp);// 为每一个文件的索引信息建立一个document对象Document document = new Document();// 文件路径索引:只是存储 不建立路径的索引,因为我们不需要对路径进行查询Field fieldPath = new Field("path", textFiles[i].getPath(),Field.Store.YES, Field.Index.NO);// 文件内容索引:Field fieldBody = new Field("body", temp, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);// 添加各域到document文档对象中document.add(fieldPath);document.add(fieldBody);// 将文档写入索引indexWriter.addDocument(document);}}// 关闭indexWriter.close();// 测试一下索引时间long endTime = new Date().getTime();//getPath,getAbsolutePath,getCanonicalPath的区别参考转载的文章System.out.println("用时:" + (endTime - startTime) + "毫秒来把文档增加到索引里面去,"+fileDir.getPath());System.out.println("用时:" + (endTime - startTime) + "毫秒来把文档增加到索引里面去,"+fileDir.getAbsolutePath());System.out.println("用时:" + (endTime - startTime) + "毫秒来把文档增加到索引里面去,"+fileDir.getCanonicalPath());}// 自定义读取文件内容的方法 FileReaderAll()public static String FileReaderAll(String FileName, String charset)throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), charset));//知制定读取文件方式charset:GBKString line = new String();String temp = new String();while ((line = reader.readLine()) != null) {temp += line;}reader.close();return temp;}}

搜索源代码:

package lucene的一个标准测试;import java.io.File;
import java.io.IOException;import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;public class TestQuery {public static void main(String[] args) throws IOException,ParseException{//搜索的索引路径String index = "E:\\index";IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));//定义在索引库中进行查询的searcherIndexSearcher searcher = new IndexSearcher(reader);ScoreDoc[] hits = null;//检索词String queryString="绝对秋香";//声明query对象Query query = null;//分词器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);try {QueryParser qp = new QueryParser(Version.LUCENE_35,"body",analyzer);query = qp.parse(queryString);} catch (ParseException e) {e.printStackTrace();}if(searcher!=null){TopDocs results = searcher.search(query, 10);hits = results.scoreDocs;if(hits.length>0){System.out.println("找到hits.length="+hits.length+"个结果\n"+"找到results.totalHits="+results.totalHits);}searcher.close();}}}


这篇关于Lucene的一个简单的标准测试(Lucene包基于3.5版本的)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087963

相关文章

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

Redis指南及6.2.x版本安装过程

《Redis指南及6.2.x版本安装过程》Redis是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSIC语言编写、支持网络、... 目录概述Redis特点Redis应用场景缓存缓存分布式会话分布式锁社交网络最新列表Redis各版本介绍旧

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

python多线程并发测试过程

《python多线程并发测试过程》:本文主要介绍python多线程并发测试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、并发与并行?二、同步与异步的概念?三、线程与进程的区别?需求1:多线程执行不同任务需求2:多线程执行相同任务总结一、并发与并行?1、