Lucene學習筆記(應用)
Lucene學習筆記
一lucene配置
1:下載lucene
到官方網站下載最新版的lucene,最新版本是2.1的。下載網址https://lucene.apache.org/java/docs/index.html ,在windows下應用,隻需下載lucene-2.1.0-src.zip,lucene-2.1.0.zip兩個zip文件即可。
2:配置java環境,參考我寫的
WinNT+JDK+TomCat+AXIS+MySQL+MYSQLAdministrator+WinTookit詳細配置
(https://blog.csdn.net/ugg/archive/2006/03/02/614164.aspx )
3:下載eclipse
到官方網站下載eclipse最新版本,
中文版推薦下載https://down.oyksoft.com/Download.asp?ID=1854,
中文包下載https://down.oyksoft.com/Download.asp?ID=2973 下載到本地,分別把eclipse-SDK-3.2.1-win32.zip,NLpack1-eclipse-SDK-3.2.1-win32.zip解壓,然後把NLpack1-eclipse-SDK-3.2.1-win32.zip文件夾內的內容覆蓋到eclipse-SDK-3.2.1-win32.zip解壓的文件內,此時運行eclipse,就可以應用中文版的eclipse。
4:運行eclipse,eclipse會自動加載java包,我們應用lucenc時,需要把lucene-core-2.1.0.jar加入加入eclipse項目中,我們創建一個簡單的創建索引,添加記錄,查詢。
創建索引文件
package lucene;
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
public class CreateDataBase {
public static void main(String[] args){
CreateDataBase temp= new CreateDataBase();
if(temp.createDataBase("e://lucene//holendb")==1){
System.out.println("db init succ");
}
}
public CreateDataBase(){
}
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;
}
public int createDataBase(String file){
return this.createDataBase(new File(file));
}
}
添加記錄
package lucene;
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;
public class InsertRecords{
public static void main(String[] args){
InsertRecords temp= new InsertRecords();
String dbpath="e://lucene//holendb";
//holen1.txt中包含關鍵字"holen"和"java"
if(temp.insertRecords(dbpath,"e://lucene//ugg1.txt")==1){
System.out.println("add file1 succ");
}
//holen2.txt中包含關鍵字"holen"和"chen"
if(temp.insertRecords(dbpath,"e://lucene//ugg2.txt")==1){
System.out.println("add file2 succ");
}
}
public InsertRecords(){
}
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;
}
public int insertRecords(String dbpath,String file){
return this.insertRecords(dbpath,new File(file));
}
public void addFiles(IndexWriter indexWriter,File file){
Document doc= new Document();
try{
doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.UN_TOKENIZED));
//以下兩句隻能取一句,前者是索引不存儲,後者是索引且存儲
doc.add(new Field("contents", new FileReader(file)));
indexWriter.addDocument(doc);
indexWriter.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
進行查詢
epackage lucene;
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;
public class QueryRecords{
public QueryRecords(){
}
public ArrayList queryRecords(String searchkey,String dbpath,String searchfield){
ArrayList list= null;
try{
Searcher searcher= new IndexSearcher(dbpath);
QueryParser parser = new QueryParser(searchfield, new StandardAnalyzer());
Query query = parser.parse(searchkey);
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;
}
public static void main(String[] args){
QueryRecords temp= new QueryRecords();
ArrayList list= null;
list=temp.queryRecords("acronyms","e://lucene//holendb","contents");
for(int i=0;i<list.size();i++){
System.out.println((String)list.get(i));
}
}
}
最後更新:2017-04-02 00:06:17