由于工作的需要,做以个搜索的功能,所以,准备用大名鼎鼎的LUCENE,在网上找了一些的资料,基本上看懂了点点,现将学习的代码贴出来,大家提提意见。。。
我用的工具。
eclipse3.1 http://www.eclipse.org
lucene1.4 http://mirror.vmmatrix.net/apache/lucene/java/archive/lucene-1.4.3.jar
把jar文件放到classpath 里面就ok了。
下面开始编码:
1、CreateDataBase.java
/**
*@description 创建库文件
*@package com.mysearch
*@author 李国庆
*@company LEEMENZ
*@version 1.0.0
*@discription
*
*/
package com.mysearch;
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
/**
* @author Administrator
*
*/
public class CreateDataBase {
public CreateDataBase() {
}
/**
*
* @param file
* @return
*/
public int createDataBase(File file) {
int returnValue = 0;
if (!file.isDirectory()) {
file.mkdirs();
}
try {
IndexWriter indexWriter = new IndexWriter(file,
new StandardAnalyzer(), true);
indexWriter.close();
returnValue = 1;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
/**
*传入检索库路径,初始化库
* @paramfile
* @return
*/
public int createDataBase(String file) {
return this.createDataBase(new File(file));
}
/*
*
*
*/
public static void main(String[] args) {
CreateDataBase temp = new CreateDataBase();
if (temp.createDataBase("d:\\lucene\\holendb") == 1) {
System.out.println("db init succ");
}
}
}
2、InsertRecords 添加索引
/**
*@description 添加索引
*@package com.mysearch
*@author 李国庆
*@company LEEMENZ
*@version 1.0.0
*@discription
*
*/
package com.mysearch;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
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;
/**
* @author Administrator
*
*/
public class InsertRecords {
public InsertRecords() {
}
/**
*
* @param dbpath 数据文件(INDEX)所在的路径
* @param file 文件名
* @return
*/
public int insertRecords(String dbpath, File file) {
int returnValue = 0;
try {
IndexWriter indexWriter = new IndexWriter(dbpath,
new StandardAnalyzer(), false);
this.addFiles(indexWriter, file);
returnValue = 1;
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
/**
* 传入需加载的文件名
*
* @paramfile dbpath 需加载的文件路径
* @return file 需加载的文件名
*/
public int insertRecords(String dbpath, String file) {
return this.insertRecords(dbpath, new File(file));
}
/**
* 建立索引
* @param indexWriter lucene内部对象,负责建立索引
* @param file 文件名
*/
public void addFiles(IndexWriter indexWriter, File file) {
Document doc = new Document();
try {
doc.add(Field.Keyword("filename", file.getName()));
// 以下两句只能取一句,前者是索引不存储,后者是索引且存储
// doc.add(Field.Text("content",new FileReader(file)));
doc.add(Field.Text("content", this.chgFileToString(file)));
indexWriter.addDocument(doc);
indexWriter.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 从文本文件中读取内容
*
* @param file
* @return string
*/
public String chgFileToString(File file) {
String returnValue = null;
StringBuffer sb = new StringBuffer();
char[] c = new char[4096];
try {
Reader reader = new FileReader(file); //以reader的形式读取文件
int n = 0;
while (true) {
n = reader.read(c);
if (n > 0) {
sb.append(c, 0, n);
} else {
break;
}
}
reader.close(); //关闭流
} catch (Exception ex) {
ex.printStackTrace();
}
returnValue = sb.toString();
return returnValue;
}
/**
* 执行插入数据操作
* @param args
*/
public static void main(String[] args) {
InsertRecords temp = new InsertRecords();
String dbpath = "d:\\lucene\\holendb";
// holen1.txt中包含关键字"nationally"和"ggbm"
if (temp.insertRecords(dbpath, "d:\\lucene\\nationally1.txt") == 1) {
System.out.println("add file1 succ");
}
// holen2.txt中包含关键字"nationally"和"leo"
if (temp.insertRecords(dbpath, "d:\\lucene\\nationally2.txt") == 1) {
System.out.println("add file2 succ");
}
// holenalsdjflasdlfj.txt 中不包括关键字"nationally"
if (temp.insertRecords(dbpath,
"d:\\lucene\\nationallynalsdjflasdlfj.txt") == 1) {
System.out.println("add file3 succ");
}
}
}
3、QueryRecords 测试查询
/**
*@description 测试查询
*@package com.mysearch
*@author 李国庆
*@company LEEMENZ
*@version 1.0.0
*@discription
*
*/
package com.mysearch;
import java.util.ArrayList;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
/**
* @author Administrator
*
*/
public class QueryRecords {
public QueryRecords(){
}
/**
*检索查询,将结果集返回
* @param searchkey
* @param dbpath
* @param searchfield
* @return ArrayList
*/
public ArrayList queryRecords(String searchkey,String dbpath,String searchfield){
ArrayList list= null;
try{
Searcher searcher= new IndexSearcher(dbpath);
Query query = QueryParser.parse(searchkey,searchfield,new StandardAnalyzer());
Hits hits = searcher.search(query);
if(hits!= null){
list= new ArrayList();
int temp_hitslength=hits.length();
Document doc= null;
for(int i=0;i<temp_hitslength;i++){
doc=hits.doc(i);
list.add(doc.get("filename"));
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
/**
*
* @param args
*/
public static void main(String[]args) {
QueryRecords temp= new QueryRecords();
ArrayList list= null;
list=temp.queryRecords("nationally","d:\\lucene\\holendb","content");
for(int i=0;i<list.size();i++){
System.out.println((String)list.get(i));
}
}
}
然后在d:\lucene下建立3个文本文件,我的是holenalsdjflasdlfj.txt、holen1.txt、holen2.txt,在里面输入一些内容,有一个包含有"nationally"就可以了,这个就是我们要测试查询的字符串。
首先执行CreateDataBase.java(初始化库),然后InsertRecords.java(添加索引),最后QueryRecords.java(测试文件)
然后就可以看到正确的输出了。
:),就到这里,欢迎指正!!!!!!
评论