Lucene是Apache公司的开源全文搜索工具包,一下是基本用法的入门代码:



第一个java文件

package com.citi.service;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import javax.print.Doc;
import javax.print.attribute.HashAttributeSet;


import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;


public class LuceneUtil {


//Defined the Directory
Directory directory = null;

//Constructor
public LuceneUtil() {
try {
directory = FSDirectory.open(new File("C:/Users/sx32717/workspace/Search/index"));
} catch (IOException e) {
e.printStackTrace();
}
}

//CreateTheIndex
public void CreateIndex(){
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter writer = null;
try {
writer = new IndexWriter(directory, config);
writer.deleteAll();
Document doc = null;
File files = (new File("C:/Users/sx32717/workspace/Search/src"));
for(File f:files.listFiles()){
doc = new Document();
doc.add(new Field("content",new FileReader(f)));
doc.add(new Field("name", f.getName(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("path", f.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
System.out.println("Index was created successfully");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null)writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//Search
public ArrayList Search(String str){
ArrayList a_List = new ArrayList();
Map<String, String> map = null;
IndexReader reader = null;
try {
reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse(str);
TopDocs tDocs = searcher.search(query, 20);
ScoreDoc[] scores = tDocs.scoreDocs;
Document doc = null;
for(ScoreDoc score : scores){
doc = searcher.doc(score.doc);
map = new HashMap<String, String>();
map.put("name", doc.get("name"));
map.put("path", doc.get("path"));
a_List.add(map);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
try {
if(reader!=null)reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return a_List;
}

//numDoc
public int numDocument(){
IndexReader reader = null;
try {
reader = IndexReader.open(directory);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(reader!=null)reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return reader.numDocs();
}

//delDoc
public int delDocument(){
IndexReader reader = null;
try {
reader = IndexReader.open(directory);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(reader!=null)reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return reader.maxDoc()-reader.numDocs();
}

//delete All Indexs
public void deleteAllIndexs(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory, 
new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
writer.deleteAll();
System.out.println("All was be deleted !");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null)writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//Remove the Index
public void removeIndex(String name){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory, 
new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
writer.deleteDocuments(new Term("name",name));
System.out.println("Remove the Index successful.");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null)writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//Unremove the Index

}





第二个java测试程序


package com.citi.test;


import java.util.ArrayList;
import java.util.HashMap;


import org.junit.Test;


import com.citi.service.LuceneUtil;


public class LuceneUtilTest {


@Test
public void testCreateIndex() {
LuceneUtil lucene = new LuceneUtil();
lucene.CreateIndex();
}

@Test
public void testSearch(){
LuceneUtil lucene = new LuceneUtil();
ArrayList list = lucene.Search("java");
if(!list.isEmpty()){
for(int i=0;i<list.size();i++){
System.out.println("********************");
System.out.println("name:" + ((HashMap)list.get(i)).get("name"));
System.out.println("path:" + ((HashMap)list.get(i)).get("path"));
}
} else {
System.out.println("Not find the content that you want.");
}
}

/*
* The all numbers
*/
@Test
public void testNumDoc(){
LuceneUtil lucene = new LuceneUtil();
int num = lucene.numDocument();
System.out.println("The number is :" + num);
}

/*
* The remove numbers
*/
@Test
public void testRemoveDoc(){
LuceneUtil lucene = new LuceneUtil();
int num = lucene.delDocument();
System.out.println("The number is :" + num);
}

/*
* Delete all Indexs
*/
@Test
public void testDelAllIndex(){
LuceneUtil lucene = new LuceneUtil();
lucene.deleteAllIndexs();
System.out.println("Deleted the all Index!");
}

/*
* Remove the Index
*/
@Test
public void testDelIndex(){
LuceneUtil lucene = new LuceneUtil();
lucene.removeIndex("java");
}

}


Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐