📄 search.java
字号:
package com.galaxyworkstation.model;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;
/**
* 该类模拟了对某一Document对象的搜索
* @author 李奕
* @version 1.0
*/
public class Search {
public static int CD_SEARCH = 0;
public static int DISC_SEARCH = 1;
/**
* 默认构造函数
*/
public Search(){}
/**
* 按给定关键词和给定方式搜索
* @param keyword 关键字
* @param type 搜索方式,Search.CD_SEARCH代表搜索光盘索引;Search.DISC_SEARCH代表搜索硬盘索引
* @return 含所有满足条件的文档集合
*/
public ArrayList<Document> doSearch(String keyword, int type) throws MyException{
ArrayList<Document> result = new ArrayList<Document>();
IndexReader indexReader;
IndexSearcher searcher;
if(type == CD_SEARCH){
indexReader = IndexFactory.getCDIndexReader();
if(indexReader == null)
return result;
searcher = IndexFactory.getCDIndexSearcher();
try {
QueryParser queryParser = new QueryParser("name", new StandardAnalyzer());
queryParser.setAllowLeadingWildcard(true);
Query query1 = queryParser.parse(keyword);
PrefixQuery query2 = new PrefixQuery(new Term("name", keyword));
//TermQuery query3 = new TermQuery(new Term("description", keyword));
BooleanQuery query= new BooleanQuery();
query.add(query1, BooleanClause.Occur.SHOULD);
query.add(query2, BooleanClause.Occur.SHOULD);
//query.add(query3, BooleanClause.Occur.SHOULD);
TopDocCollector collector = new TopDocCollector(100);
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for (int i = 0; i < hits.length; i++) {
int docId = hits[i].doc;
result.add(searcher.doc(docId));
}
} catch (BooleanQuery.TooManyClauses e) {
int n = BooleanQuery.getMaxClauseCount();
if(n>10000){
throw(new MyException("the number is too large"));
}
else{
BooleanQuery.setMaxClauseCount(n*2);
return doSearch(keyword, type);
}
}
catch (ParseException e) {
return result;
} catch (IOException e) {
return result;
}
return result;
}
else {
indexReader = IndexFactory.getDiscIndexReader();
if(indexReader == null)
return result;
searcher = IndexFactory.getDiscIndexSearcher();
try {
PrefixQuery query2 = new PrefixQuery(new Term("name", keyword));
QueryParser queryParser = new QueryParser("name", new StandardAnalyzer());
queryParser.setAllowLeadingWildcard(true);
Query query1 = queryParser.parse(keyword);
BooleanQuery query= new BooleanQuery();
query.add(query1, BooleanClause.Occur.SHOULD);
query.add(query2, BooleanClause.Occur.SHOULD);
TopDocCollector collector = new TopDocCollector(100);
searcher.search(query, collector);
Document d = new Document();
d.add(new Field("hits", ""+collector.getTotalHits(), Field.Store.YES, Field.Index.NO));
result.add(d);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for (int i = 0; i < hits.length; i++) {
int docId = hits[i].doc;
result.add(searcher.doc(docId));
}
} catch (BooleanQuery.TooManyClauses e) {
int n = BooleanQuery.getMaxClauseCount();
if(n>10000){
throw(new MyException("the number is too large"));
}
else{
BooleanQuery.setMaxClauseCount(n*2);
return doSearch(keyword, type);
}
}catch (IOException e) {
return result;
} catch (ParseException e) {
return result;
}
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -