/**
* 根据信息分类和关键词进行查询
* @param type,资源的类型,其值为news或product
* @param searchKey,搜索的关键字
* @return Hits
*/
public Hits executeSearch(String type,String keyword)
{
Hits result = null;
if(type != null && !type.equals("") && keyword != null && !keyword.equals(""))
{
try
{
//根据关键字构造一个数组
String[] key = new String[]{keyword,type};
//同时声明一个与之对应的字段数组
String[] fields = {"title","type"};
//声明BooleanClause.Occur[]数组,它表示多个条件之间的关系
BooleanClause.Occur[] flags=new BooleanClause.Occur[]{BooleanClause.Occur.MUST,BooleanClause.Occur.MUST};
ChineseAnalyzer analyzer = new ChineseAnalyzer();
//用MultiFieldQueryParser得到query对象
Query query = MultiFieldQueryParser.parse(key, fields, flags, analyzer);
//c:/index表示我们的索引文件所在的目录
IndexSearcher searcher = new IndexSearcher("c:/index");
//查询结果
result = searcher.search(query);
} catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}