Lucene.java:
建索引:
Doc.java
建索引:
public class Lucene {
private Lucene() {}
public static final String INDEX_DIR = "E:\\index";
public static void index(ArrayList list, boolean create) {
final File indexDir = new File(INDEX_DIR);
Date start = new Date();
try {
IndexWriter writer = new IndexWriter(indexDir, new MMAnalyzer(), create);
writer.setMergeFactor(100);
for (int i=0; i<list.size(); i++){
System.out.println("---i="+i);
writer.addDocument(Doc.Document((BasicElement)list.get(i)));
}
writer.optimize();
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
} catch (IOException e) {
System.out.println(e);
}
}
public static ArrayList search(String strContent, String strDistrict,String StrBegin,String strEnd){
ArrayList list = new ArrayList();
try {
BooleanQuery query = new BooleanQuery();
StringTokenizer st=null;
if(strContent!=null&& !"".equals(strContent)){
st = parseString("content", strContent);
while (st.hasMoreTokens()) {
String str = "*"+st.nextToken()+"*";
Query queryContent = new WildcardQuery(new Term("content",str));
query.add(queryContent,BooleanClause.Occur.MUST);
}
}
if(strDistrict!=null&& !"".equals(strDistrict)){
st = parseString("district", strDistrict);
while (st.hasMoreTokens()) {
String str = "*"+st.nextToken()+"*";
Query queryDistrict = new WildcardQuery(new Term("district",str));
query.add(queryDistrict,BooleanClause.Occur.MUST);
}
}
if(StrBegin!=null&& !"".equals(StrBegin) && strEnd!=null&& !"".equals(strEnd)){
RangeQuery queryTime = new RangeQuery(new Term("time",StrBegin), new Term("time",strEnd), true);
query.add(queryTime,BooleanClause.Occur.MUST);
}
System.out.println("query=" + query);
Searcher searcher = new IndexSearcher(INDEX_DIR);
Hits hits = searcher.search(query);
System.out.println("hits.length()=" + hits.length());
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
HashMap map = new HashMap();
map.put("table",doc.get("table"));
map.put("pid",doc.get("pid"));
//map.put("district",doc.get("district"));
//map.put("time",doc.get("time"));
System.out.println("doc.get table="+doc.get("table"));
list.add(map);
}
} catch (Exception e) {
System.out.println(e);
}
return list;
}
public static StringTokenizer parseString(String strField, String strQuery){
StringTokenizer st = null;
try {
MMAnalyzer analyzer = new MMAnalyzer();
QueryParser parser = new QueryParser(strField, analyzer);
Query query = parser.parse(strQuery);
st = new StringTokenizer(query.toString(strField).replace('\"', ' '));
}catch (Exception e) {
System.out.println(e);
}
return st;
}
/*当添加一条新记录时触发该方法,同步增加索引记录*/
public static void insertIndex(BasicElement entity) {
try {
IndexWriter writer = new IndexWriter(INDEX_DIR, new MMAnalyzer(), false);
writer.addDocument(entity);
writer.optimize();/*如果速度很慢,删除此行代码*/
writer.close();
System.out.println("insert Index OK!");
} catch (IOException e) {
System.out.println(e);
}
}
/*当删除一条记录时触发该方法,同步更新索引*/
public static void deleteIndex(BasicElement entity) {
try{
Analyzer analyzer = new StandardAnalyzer();
BooleanQuery query = new BooleanQuery();
QueryParser parserPid = new QueryParser("pid", analyzer);
Query queryPid = parserPid.parse(entity.getPid());
query.add(queryPid,BooleanClause.Occur.MUST);
QueryParser parserTable = new QueryParser("table", analyzer);
Query queryTable = parserTable.parse(entity.getType());
query.add(queryTable,BooleanClause.Occur.MUST);
Searcher searcher = new IndexSearcher(INDEX_DIR);
Hits hits = searcher.search(query);
if (hit.length()>0){
IndexReader reader = IndexReader.open(INDEX_DIR);
reader.deleteDocument(hits.id(0));
reader.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
/*当修改一条记录时触发该方法,同步更新索引*/
public static void modifyIndex(BasicElement entity) {
deleteIndex(entity);
insertIndex(entity);
}
/*定时调用该方法,根据记录的修改时间,更新和增加索引记录
ArrayList list是在上次更新索引到现在这段时间内所有修改过的记录*/
public static void updateIndex(ArrayList list) {
try{
for(int i=0; i<list.size(); i++){
BasicElement entity = (BasicElement)list.get(i);
deleteIndex(entity);
}
IndexWriter writer = new IndexWriter(INDEX_DIR, new MMAnalyzer(), false);
writer.setMergeFactor(100);
for (int i=0; i<list.size(); i++){
writer.addDocument(Doc.Document((BasicElement)list.get(i)));
}
writer.optimize();
writer.close();
} catch (IOException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
}
}
Doc.java
public class Doc {
private Doc() {}
public static Document Document(BasicElement entity){
String strTable = "";
String strPid = "";
String strDistrict = "";
String strTime = "";
String strContent = "";
if (strTable.equals("type")){
strDistrict = "";
}
Document doc = new Document();
doc.add(new Field("table",strTable, Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("pid", strPid, Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("district", strDistrict, Field.Store.NO, Field.Index.TOKENIZED));
doc.add(new Field("time", strTime, Field.Store.NO, Field.Index.TOKENIZED));
doc.add(new Field("content",strContent, Field.Store.NO, Field.Index.TOKENIZED));
//System.out.println("Document.table:"+doc.get("table"));
return doc;
}