构造方法:索引建在内存里,掉用初始化方法index()
IndexReader是单例的
搜索:调用getSearcher()
searchByTerm(String field,String name,int num)词语查找
searchByTermRange(String field,String start,String end,int num) 范围查找
searchByNumricRange(String field,int start,int end,int num)数字的范围查找
su.searchByTerm("content","i",10);//I like soccer 精确查找,查找的好像是第一个字母
package org.itat.index;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
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.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
public class SearcherUtil {
private Directory directory=null;
private IndexReader reader=null;
private String[] ids = { "1", "2", "3", "4", "5", "6" };
private String[] emails = { "dd@@dd.org", "aa@jingtian.org",
"bb@jingtian.org", "cc@ytu.edu", "ee@ee.org", "ff@ff.org" };
private String[] contents = { "I like soccer", "I like football",
"I like soccer and I like game", "I like book", "I like soccer",
"I like soccer" };
private int[] attachs = { 1, 6, 8, 9, 5, 4, 5 };
private String[] names = { "zhangsan", "lisi", "wangwu", "haoliu", "wuba",
"chenjiu" };
private Map<String, Float> scores = new HashMap<String, Float>();
private Date[] dates = null;
public SearcherUtil() {
directory = new RAMDirectory();
setDate();
index();
}
public void setDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD"); // 时间转换,这个不会有点丢人
try {
dates = new Date[ids.length];
dates[0] = sdf.parse("2010-08-17");
dates[1] = sdf.parse("2011-02-17");
dates[2] = sdf.parse("2012-03-17");
dates[3] = sdf.parse("2011-04-17");
dates[4] = sdf.parse("2012-05-17");
dates[5] = sdf.parse("2011-07-17");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
public void index() {
IndexWriter writer = null;
Document doc = null;
try {
writer = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
for (int i = 0; i <ids.length; i++) {
doc = new Document();
doc.add(new Field("id", ids[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email", emails[i], Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("content", contents[i], Field.Store.NO,
Field.Index.ANALYZED));
doc.add(new Field("name", names[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
// 存储数字
doc.add(new NumericField("attach", Field.Store.YES, true)
.setIntValue(attachs[i]));
// 存储日期
doc.add(new NumericField("date", Field.Store.YES, true)
.setLongValue(dates[i].getTime()));
String et = emails[i].substring(emails[i].lastIndexOf("@") + 1);
if (scores.containsKey(et)) {
doc.setBoost(scores.get(et));
} else {
doc.setBoost(5f);
}
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public IndexSearcher getSearcher() {
try {
if (reader == null) {
reader = IndexReader.open(directory);
} else {
IndexReader tr = IndexReader.openIfChanged(reader);
if (tr != null) {
reader.close();
reader = tr;
}
}
return new IndexSearcher(reader);
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("到这里了");
return null;
}
public void searchByTerm(String field,String name,int num)
{
try {
IndexSearcher indexSearcher = getSearcher();
Query query = new TermQuery(new Term(field, name));
TopDocs tds = indexSearcher.search(query, num);
System.out.println("一共查询了:" + tds.totalHits);
for (ScoreDoc sd : tds.scoreDocs) {
Document doc = indexSearcher.doc(sd.doc);
System.out.println(doc.get("id") + "---->" + doc.get("name") + "["
+ doc.get("email") + "]-->" + doc.get("id") + ","
+ doc.get("attach") + "," + doc.get("date"));
}
indexSearcher.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void searchByTermRange(String field,String start,String end,int num) {
try {
IndexSearcher searcher = getSearcher();
Query query = new TermRangeQuery(field,start,end,true, true);
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach")+","+doc.get("date"));
}
searcher.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void searchByNumricRange(String field,int start,int end,int num) {
try {
IndexSearcher searcher = getSearcher();
Query query = NumericRangeQuery.newIntRange(field,start, end,true,true);
TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:"+tds.totalHits);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("id")+"---->"+
doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
doc.get("attach")+","+doc.get("date"));
}
searcher.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试类
package org.itat.test;
import org.itat.index.SearcherUtil;
import org.junit.Before;
import org.junit.Test;
public class TestSearcher {
private SearcherUtil su;
@Before
public void init()
{
su=new SearcherUtil();
}
@Test
public void searchByTerm()
{
// su.searchByTerm("content","i",10);//I like soccer 精确查找//6条结果
su.searchByTerm("name","zhangsan",10);
}
@Test
public void searchByTermRange()
{
// su.searchByTermRange("id","1","6",10);
su.searchByTermRange("name","a","z",10);
}
@Test
public void searchByNumRange() {
su.searchByNumricRange("attach",2,10, 5);
}
}