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