在网上找了些关于lucene全文检索的资料,发现对于初学者那些资料还是难于理解,自己总结了些代码现在贴出来,首先在c:\\source文件夹下创建两个txt文件里面的信息可加入“测试”二字,执行TestFileIndexer.java创建索引文件,然后运行TestQuery.java根据关键字读取索引信息,具体的使用还是要看个人需求而定
我用的jar包是3.6.0,没有jar包的话可以到官网下载
创建索引文件:
TestFileIndexer.java
package text; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; 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.document.FieldSelectorResult; import org.apache.lucene.document.NumericField; 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 TestFileIndexer { public static void main(String[] args) throws Exception { /* 指明要索引文件夹的位置,这里是C盘的source文件夹下 */ File fileDir = new File( "c:\\source " ); /* 这里放索引文件的位置 */ File indexDir = new File( "c:\\index" ); Directory dir=FSDirectory.open(indexDir);//将索引存放在磁盘上 Analyzer lucenAnalyzer=new StandardAnalyzer(Version.LUCENE_36);//分析器 IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_36,lucenAnalyzer); iwc.setOpenMode(OpenMode.CREATE);//创建新的索引文件create 表示创建或追加到已有索引库 IndexWriter indexWriter=new IndexWriter(dir,iwc);//把文档写入到索引库 File[] textFiles=fileDir.listFiles();//得到索引文件夹下所有文件 long startTime=new Date().getTime(); //增加document到检索去 for (int i = 0; i < textFiles.length; i++) { // if (textFiles[i].isFile()&& textFiles[i].getName().endsWith(".txt")) { System.out.println(":;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"); System.out.println("File"+textFiles[i].getCanonicalPath()+"正在被索引..."); String temp=FileReaderAll(textFiles[i].getCanonicalPath(),"GBK"); System.out.println(temp); 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); NumericField modifiField=new NumericField("modified");//所以key为modified modifiField.setLongValue(fileDir.lastModified()); document.add(FieldPath); document.add(FieldBody); document.add(modifiField); indexWriter.addDocument(document); // } } indexWriter.close(); //计算一下索引的时间 long endTime=new Date().getTime(); System.out.println("花了"+(endTime-startTime)+"毫秒把文档添加到索引里面去"+fileDir.getPath()); } public static String FileReaderAll(String FileName,String charset)throws IOException{ BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(FileName),charset)); String line=new String(); String temp=new String(); while ((line=reader.readLine())!=null) { temp+=line; } reader.close(); return temp; } }
根据关键字查询索引文件里面的信息:
TestQuery.java
package text; 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.document.Document; 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 ParseException, IOException { String index="c:\\index";//搜索的索引路径 IndexReader reader=IndexReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher=new IndexSearcher(reader);//检索工具 ScoreDoc[] hits=null; String queryString="测试"; //搜索的索引名称 Query query=null; Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_36); try { QueryParser qp=new QueryParser(Version.LUCENE_36,"body",analyzer);//用于解析用户输入的工具 query=qp.parse(queryString); } catch (Exception e) { // TODO: handle exception } if (searcher!=null) { TopDocs results=searcher.search(query, 10);//只取排名前十的搜索结果 hits=results.scoreDocs; Document document=null; for (int i = 0; i < hits.length; i++) { document=searcher.doc(hits[i].doc); String body=document.get("body"); String path=document.get("path"); String modifiedtime=document.get("modifiField"); System.out.println(body+" "); System.out.println(path); } if (hits.length>0) { System.out.println("找到"+hits.length+"条结果"); } searcher.close(); reader.close(); } } }
相关推荐
NULL 博文链接:https://370371029-qq-com.iteye.com/blog/1832474
Lucene是apache软件基金会 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎...
基于lucene 2.4简单的一个索引和搜索实例
lucene 全文检索 包结构 lucene开发实例
快速有效地索引企业累积的大量的信息资源,是提供高质量检索服务的基础。Lucene 是一个用Java 写的全文索 引引擎工具包,访问索引时间快,支持多用户...Lucene 的索引文件格式,实现了一个基于Lucene 文档检索的应用实例。
lucene实例是一个比较详细的例子,包括lucene的入门到高级实例,代码里有比较详细的实例,所有的实例都是通过junit来测试的。实例包括各种搜索:如通配符查询、模糊查询、查询结果的分页、中文分词器、自定义分词器...
Lucene3.0和Lucene2.0API有多处改动,该实例用Luence3.0实现
利用paoding进行中文分词,可进行自定义词典。利用lucene对文本内容进行索引和搜索并进行高亮处理。
Eclipse工程文件,全文检索 lucene-5.2.1 入门Eclipse工程实例,福利放送,与lucene3结果比对
Lucene 是为文本类型的数据建立索引的,所以只要能把需要索引的数据格式转化的文本的,Lucene 就能对文档进行索引和搜索。比如HTML、PDF,都可以转换文本再交给Lucene进行索引。
文档中包含Lucene4.0.0版本jar包,中文分词器jar包,Lucene实例代码 1:建立索引 2:各种搜索方式方法 3:删除索引 4:检查索引文件 5:恢复删除的索引 6:强制删除 7:更新索引 8:合并索引 9:高亮回显 供大家参考...
lucene 创建中文索引,运用创建的索引进行全文检索实例
包含最新lucene5.2.1的入门经典案例,以及程序中所需的lucene-core-5.2.1.jar、lucene-analyzers-common-5.2.1.jar等几个jar包。代码可运行。
lucene小实例文件检索,虽然精简 但是实现啦检索文件的功能,lucene初学者必须啊
Lucene是一个强大的全文索引引擎工具包, 它的...介绍了一个高性能的全文检索引擎——Lucene开源系统,详细分析了Lucene的系统结构、全文索引机制,然后将其引入具体应用,给出了一个基于Lucene全文检索技术的具体实例。
Lucene数据库操作实例
这是一个实现Lucene全文搜索多种文件格式的一个小项目。包括对搜索到得内容中的搜索字进行高亮显示,就是实现像百度那样对搜索的关键字标红。
Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎...
完整的Eclipse 工程,举一些小例子,Lucene的建立索引、删除索引、以及检索,希望对喜欢或者用Lucene开发程序的朋友们有所帮助!