分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

Lucene_Hello(示例)

发布时间:2023-09-06 02:18责任编辑:白小东关键词:暂无标签

(1)创建project

(2)导入Lucene的核心包

(3)编写代码建立索引

/lucene01/src/cn/hk/lucene/TestIndex.java:

 1 package cn.hk.lucene; 2 ?3 import java.io.File; 4 import java.io.FileReader; 5 import java.io.IOException; 6 import org.apache.lucene.analysis.standard.StandardAnalyzer; 7 import org.apache.lucene.document.Document; 8 import org.apache.lucene.document.Field; 9 import org.apache.lucene.index.CorruptIndexException;10 import org.apache.lucene.index.IndexWriter;11 import org.apache.lucene.index.IndexWriterConfig;12 import org.apache.lucene.store.Directory;13 import org.apache.lucene.store.FSDirectory;14 import org.apache.lucene.util.Version;15 16 /**17 ?* 生成索引18 ?*19 ?*/20 public class TestIndex {21 ????public static void main(String[] args) {22 ????????IndexWriter writer = null;23 ????????24 ????????try {25 ????????????//1、创建Directory26 ????????????//在内存中创建27 ????????????//Directory directory = new RAMDirectory();28 ????????????//在磁盘中创建29 ????????????Directory directory = FSDirectory.open(new File("e:\\lucene")); ???????????30 ????????????31 ????????????//2、创建IndexWriter32 ????????????IndexWriterConfig config = new IndexWriterConfig(33 ????????????????????Version.LUCENE_35,34 ????????????????????new StandardAnalyzer(Version.LUCENE_35));35 ????????????36 ????????????writer = new IndexWriter(directory,config);37 ????????????38 ????????????//3、创建Document39 ????????????Document doc = null;40 ????????????41 ????????????//获取files下的所有文件42 ????????????File list = new File("e:\\lucene\\files");43 ????????????44 ????????????//4、添加Field属性45 ????????????//遍历获取到的文件的集合46 ????????????for(File file : list.listFiles()){47 ????????????????//实例化文档对象48 ????????????????doc = new Document();49 ????????????????//存储文件的名称信息(对名称进行存储但不分析)50 ????????????????doc.add(new Field("filename",file.getName(),Field.Store.YES, Field.Index.NOT_ANALYZED));51 ????????????????doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));52 ????????????????53 ????????????????doc.add(new Field("content",new FileReader(file)));54 ????????????????55 ????????????????//将document添加到writer中56 ????????????????writer.addDocument(doc);57 ????????????}58 ????????????59 ????????} catch (IOException e) {60 ????????????e.printStackTrace();61 ????????}62 ????????finally{63 ????????????if(writer != null){64 ????????????????try {65 ????????????????????writer.close();66 ????????????????} catch (CorruptIndexException e) {67 ????????????????????// TODO Auto-generated catch block68 ????????????????????e.printStackTrace();69 ????????????????} catch (IOException e) {70 ????????????????????// TODO Auto-generated catch block71 ????????????????????e.printStackTrace();72 ????????????????}73 ????????????}74 ????????}75 ????}76 }

/lucene01/src/cn/hk/lucene/TestSearch.java:

 1 package cn.hk.lucene; 2 ?3 import java.io.File; 4 import java.io.IOException; 5 import org.apache.lucene.analysis.standard.StandardAnalyzer; 6 import org.apache.lucene.document.Document; 7 import org.apache.lucene.index.IndexReader; 8 import org.apache.lucene.queryParser.ParseException; 9 import org.apache.lucene.queryParser.QueryParser;10 import org.apache.lucene.search.IndexSearcher;11 import org.apache.lucene.search.Query;12 import org.apache.lucene.search.ScoreDoc;13 import org.apache.lucene.search.TopDocs;14 import org.apache.lucene.store.Directory;15 import org.apache.lucene.store.FSDirectory;16 import org.apache.lucene.util.Version;17 18 /**19 ?* 检索20 ?*21 ?*/22 public class TestSearch {23 ????public static void main(String[] args) {24 ????????IndexReader reader =null;25 ????????try {26 ????????????//1、创建Dierctory27 ????????????Directory directory = FSDirectory.open(new File("e:\\lucene"));28 29 ????????????//2、创建IndexReader30 ????????????reader = IndexReader.open(directory);31 ????????????32 ????????????//3、创建Searcher33 ????????????IndexSearcher searcher = new IndexSearcher(reader);34 ????????????35 ????????????//4、创建Query36 ????????????//参数:匹配器的版本、查询的属性、分析器37 ????????????QueryParser parser = new QueryParser(Version.LUCENE_35,38 ????????????????????"content",39 ????????????????????new StandardAnalyzer(Version.LUCENE_35));40 ????????????41 ????????????//查询的条件(相当于数据库中的where部分)42 ????????????Query query = parser.parse("users");43 ????????????44 ????????????//5、创建TopDocs45 ????????????//使用查询器进行查询(条件,查询次数)46 ????????????TopDocs docs = searcher.search(query, 20);47 ????????????48 ????????????//6、根据TopDocs获取ScoreDoc49 ????????????ScoreDoc[] sds = ?docs.scoreDocs;50 ????????????51 ????????????//7、根据searcher和ScoreDoc获取具体的document(分词所出现的文档)对象52 ????????????for(ScoreDoc sd : sds){53 ????????????????//表示分词所在的文档对象54 ????????????????Document doc = searcher.doc(sd.doc);55 ????????????????56 ????????????????//输出文档的信息57 ????????????????System.out.println(doc.get("filename") + " | " + doc.get("path"));58 ????????????}59 ????????????60 ????????} catch (IOException | ParseException e) {61 ????????????// TODO Auto-generated catch block62 ????????????e.printStackTrace();63 ????????} ???????????64 ????????finally{65 ????????????if(reader != null){66 ????????????????try {67 ????????????????????reader.close();68 ????????????????} catch (IOException e) {69 ????????????????????// TODO Auto-generated catch block70 ????????????????????e.printStackTrace();71 ????????????????}72 ????????????}73 ????????}74 ????????75 ????}76 }

Lucene_Hello(示例)

原文地址:https://www.cnblogs.com/zhzcode/p/9783424.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved