Lucene简单DEMO

Share

1、CreateDataBase.java  

<br />/**<br /> *@description 创建库文件<br /> *@package com.mysearch<br /> *@author &nbsp;李国庆<br /> *@company &nbsp;LEEMENZ<br /> *@version &nbsp;1.0.0<br /> *@discription <br /> * <br /> */<br />package com.mysearch;<br /><br />import java.io.File;<br /><br />import org.apache.lucene.analysis.standard.StandardAnalyzer;<br />import org.apache.lucene.index.IndexWriter;<br /><br />/**<br /> * @author Administrator<br /> *<br /> */<br />public class CreateDataBase {<br /> public CreateDataBase() {<br /><br /> }<br /><br /> /**<br /> &nbsp;* <br /> &nbsp;* @param file<br /> &nbsp;* @return<br /> &nbsp;*/<br /> public int createDataBase(File file) {<br /> &nbsp;int returnValue = 0;<br /> &nbsp;if (!file.isDirectory()) {<br /> &nbsp; file.mkdirs();<br /> &nbsp;}<br /> &nbsp;try {<br /> &nbsp; IndexWriter indexWriter = new IndexWriter(file,<br /> &nbsp; &nbsp; new StandardAnalyzer(), true);<br /> &nbsp; indexWriter.close();<br /> &nbsp; returnValue = 1;<br /> &nbsp;} catch (Exception ex) {<br /> &nbsp; ex.printStackTrace();<br /> &nbsp;}<br /> &nbsp;return returnValue;<br /> }<br /><br /> /**<br /> &nbsp;*传入检索库路径,初始化库<br /> &nbsp;* @paramfile<br /> &nbsp;* @return<br /> &nbsp;*/<br /> public int createDataBase(String file) {<br /> &nbsp;return this.createDataBase(new File(file));<br /> }<br /><br /> /*<br /> &nbsp;* <br /> &nbsp;* <br /> &nbsp;*/<br /> public static void main(String[] args) {<br /> &nbsp;CreateDataBase temp = new CreateDataBase();<br /> &nbsp;if (temp.createDataBase("d:&#92;&#92;lucene&#92;&#92;holendb") == 1) {<br /> &nbsp; System.out.println("db init succ");<br /> &nbsp;}<br /> }<br />}<br /><br /><span id="more-14"></span><br />2、InsertRecords 添加索引<br />/**<br /> *@description 添加索引<br /> *@package com.mysearch<br /> *@author &nbsp;李国庆<br /> *@company &nbsp;LEEMENZ<br /> *@version &nbsp;1.0.0<br /> *@discription <br /> * <br /> */<br />package com.mysearch;<br /><br />import java.io.File;<br />import java.io.FileReader;<br />import java.io.Reader;<br /><br />import org.apache.lucene.analysis.standard.StandardAnalyzer;<br />import org.apache.lucene.document.Document;<br />import org.apache.lucene.document.Field;<br />import org.apache.lucene.index.IndexWriter;<br /><br />/**<br /> * @author Administrator<br /> * <br /> */<br />public class InsertRecords {<br /> public InsertRecords() {<br /><br /> }<br /><br /> /**<br /> &nbsp;* <br /> &nbsp;* @param dbpath 数据文件(INDEX)所在的路径<br /> &nbsp;* @param file &nbsp; 文件名<br /> &nbsp;* @return<br /> &nbsp;*/<br /> public int insertRecords(String dbpath, File file) {<br /><br /> &nbsp;int returnValue = 0;<br /> &nbsp;try {<br /> &nbsp; IndexWriter indexWriter = new IndexWriter(dbpath,<br /> &nbsp; &nbsp; new StandardAnalyzer(), false);<br /> &nbsp; this.addFiles(indexWriter, file);<br /> &nbsp; returnValue = 1;<br /> &nbsp;} catch (Exception ex) {<br /> &nbsp; ex.printStackTrace();<br /> &nbsp;}<br /><br /> &nbsp;return returnValue;<br /> }<br /><br /> /**<br /> &nbsp;* 传入需加载的文件名<br /> &nbsp;* <br /> &nbsp;* @paramfile &nbsp;dbpath &nbsp; 需加载的文件路径<br /> &nbsp;* @return &nbsp; file &nbsp; 需加载的文件名<br /> &nbsp;*/<br /> public int insertRecords(String dbpath, String file) {<br /><br /> &nbsp;return this.insertRecords(dbpath, new File(file));<br /><br /> }<br /><br /> /**<br /> &nbsp;* 建立索引<br /> &nbsp;* @param indexWriter &nbsp;lucene内部对象,负责建立索引<br /> &nbsp;* @param file &nbsp; &nbsp; &nbsp; &nbsp; 文件名<br /> &nbsp;*/<br /> public void addFiles(IndexWriter indexWriter, File file) {<br /> &nbsp;Document doc = new Document();<br /> &nbsp;try {<br /> &nbsp; doc.add(Field.Keyword("filename", file.getName()));<br /> &nbsp; // 以下两句只能取一句,前者是索引不存储,后者是索引且存储<br /> &nbsp; // doc.add(Field.Text("content",new FileReader(file)));<br /> &nbsp; doc.add(Field.Text("content", this.chgFileToString(file)));<br /> &nbsp; indexWriter.addDocument(doc);<br /> &nbsp; indexWriter.close();<br /> &nbsp;} catch (Exception ex) {<br /> &nbsp; ex.printStackTrace();<br /> &nbsp;}<br /> }<br /><br /> /**<br /> &nbsp;* 从文本文件中读取内容<br /> &nbsp;* <br /> &nbsp;* @param file<br /> &nbsp;* @return string<br /> &nbsp;*/<br /> public String chgFileToString(File file) {<br /><br /> &nbsp;String returnValue = null;<br /> &nbsp;StringBuffer sb = new StringBuffer();<br /> &nbsp;char[] c = new char[4096];<br /><br /> &nbsp;try {<br /> &nbsp; Reader reader = new FileReader(file); &nbsp; //以reader的形式读取文件<br /> &nbsp; int n = 0;<br /> &nbsp; while (true) {<br /> &nbsp; &nbsp;n = reader.read(c);<br /> &nbsp; &nbsp;if (n > 0) {<br /> &nbsp; &nbsp; sb.append(c, 0, n);<br /> &nbsp; &nbsp;} else {<br /> &nbsp; &nbsp; break;<br /> &nbsp; &nbsp;}<br /> &nbsp; }<br /> &nbsp; reader.close(); &nbsp; &nbsp;//关闭流<br /> &nbsp;} catch (Exception ex) {<br /> &nbsp; ex.printStackTrace();<br /> &nbsp;}<br /> &nbsp;returnValue = sb.toString();<br /><br /> &nbsp;return returnValue;<br /> }<br /><br /> /**<br /> &nbsp;* &nbsp;执行插入数据操作<br /> &nbsp;* @param args<br /> &nbsp;*/<br /> public static void main(String[] args) {<br /><br /> &nbsp;InsertRecords temp = new InsertRecords();<br /> &nbsp;String dbpath = "d:&#92;&#92;lucene&#92;&#92;holendb";<br /><br /> &nbsp;// holen1.txt中包含关键字"nationally"和"ggbm"<br /> &nbsp;if (temp.insertRecords(dbpath, "d:&#92;&#92;lucene&#92;&#92;nationally1.txt") == 1) {<br /> &nbsp; System.out.println("add file1 succ");<br /> &nbsp;}<br /><br /> &nbsp;// holen2.txt中包含关键字"nationally"和"leo"<br /> &nbsp;if (temp.insertRecords(dbpath, "d:&#92;&#92;lucene&#92;&#92;nationally2.txt") == 1) {<br /> &nbsp; System.out.println("add file2 succ");<br /> &nbsp;}<br /> &nbsp;// holenalsdjflasdlfj.txt 中不包括关键字"nationally"<br /> &nbsp;if (temp.insertRecords(dbpath,<br /> &nbsp; &nbsp;"d:&#92;&#92;lucene&#92;&#92;nationallynalsdjflasdlfj.txt") == 1) {<br /> &nbsp; System.out.println("add file3 succ");<br /> &nbsp;}<br /> }<br />}<br /><br /><br />3、QueryRecords &nbsp;测试查询<br />/**<br /> *@description 测试查询<br /> *@package com.mysearch<br /> *@author &nbsp;李国庆<br /> *@company &nbsp;LEEMENZ<br /> *@version &nbsp;1.0.0<br /> *@discription <br /> * <br /> */<br />package com.mysearch;<br /><br />import java.util.ArrayList;<br /><br />import org.apache.lucene.analysis.standard.StandardAnalyzer;<br />import org.apache.lucene.document.Document;<br />import org.apache.lucene.queryParser.QueryParser;<br />import org.apache.lucene.search.Hits;<br />import org.apache.lucene.search.IndexSearcher;<br />import org.apache.lucene.search.Query;<br />import org.apache.lucene.search.Searcher;<br /><br />/**<br /> * @author Administrator<br /> *<br /> */<br />public class QueryRecords {<br /><br /> public QueryRecords(){<br /> &nbsp; &nbsp;}<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;/**<br /> &nbsp; &nbsp; *检索查询,将结果集返回<br /> &nbsp; &nbsp; * @param searchkey<br /> &nbsp; &nbsp; * @param dbpath<br /> &nbsp; &nbsp; * @param searchfield<br /> &nbsp; &nbsp; * @return ArrayList<br /> &nbsp; &nbsp; */<br /> &nbsp; &nbsp;public ArrayList queryRecords(String searchkey,String dbpath,String searchfield){<br /> &nbsp; &nbsp; &nbsp; ArrayList list= null;<br /> &nbsp; &nbsp; &nbsp; try{<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Searcher searcher= new IndexSearcher(dbpath);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query = QueryParser.parse(searchkey,searchfield,new StandardAnalyzer());<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hits hits = searcher.search(query);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(hits!= null){<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list= new ArrayList();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int temp_hitslength=hits.length();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Document doc= null;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0;i<temp_hitslength;i++){<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;doc=hits.doc(i);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.add(doc.get("filename"));<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; }catch(Exception ex){<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();<br /> &nbsp; &nbsp; &nbsp; }<br /><br /> &nbsp; &nbsp; &nbsp; return list;<br /> &nbsp; &nbsp;} <br /><br /> &nbsp; &nbsp;/**<br /> &nbsp; &nbsp; * <br /> &nbsp; &nbsp; * @param args<br /> &nbsp; &nbsp; */<br /> &nbsp; &nbsp;public static void main(String[]args) {<br /><br /> &nbsp; &nbsp; &nbsp; QueryRecords temp= new QueryRecords(); &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; ArrayList list= null;<br /> &nbsp; &nbsp; &nbsp; list=temp.queryRecords("nationally","d:&#92;&#92;lucene&#92;&#92;holendb","content");<br /> &nbsp; &nbsp; &nbsp; for(int i=0;i<list.size();i++){<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println((String)list.get(i));<br /> &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp;}<br />}<br />

然后在d:\lucene下建立3个文本文件,我的是holenalsdjflasdlfj.txt、holen1.txt、holen2.txt,在里面输入一些内容,有一个包含有”nationally”就可以了,这个就是我们要测试查询的字符串。
首先执行CreateDataBase.java(初始化库),然后InsertRecords.java(添加索引),最后QueryRecords.java(测试文件)
然后就可以看到正确的输出了。

:),就到这里,欢迎指正!!!!!!