lunece搜索


我有一个对象Info 里面有id,title,state,content几个属性,现在我要查询content包含keyword关键字,而且state='2' or state='3'的Info 对象,例如这个语句"from Info where content like '%keyword%' and (state='2' or state='3')" 查询,请问该如何写,下面是我写的代码,不太对。


public List searchFiles(boolean flag,String keyword) {
// hitsList用来保存db的纪录,这些纪录可以通过查询结果取到
List hitsList = new ArrayList();
try {
IndexReader reader = IndexReader.open("E:/lunece/");
Searcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
//查询分析器
String querys[]={"2","3"};
String fields[]={"state","state"};
BooleanClause.Occur[] flags = {
BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD};
MultiFieldQueryParser squery = new MultiFieldQueryParser(fields, analyzer);
Query query2 = squery.parse(querys, fields, flags, analyzer);
QueryParser parser = null;
if(flag){
parser = new QueryParser("title", analyzer);
}else{
parser = new QueryParser("content", analyzer);
}
// 解析查询关键字,比如输入的是以空格等分开的多个查询关键字,这里解析后,可以多条件查询
Query query = parser.parse(keyword);
BooleanQuery bquerymain = new BooleanQuery();
bquerymain.add(query, BooleanClause.Occur.MUST);
bquerymain.add(query2, BooleanClause.Occur.MUST);
// hits用来保存查询结果,这里的hits相当于sql中的result
Hits hits = searcher.search(bquerymain);
InfoDAO dao = new InfoDAO();
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
// 获得article表的主健
String id = doc.get("id");
// 根据主健去db中取纪录,返回到hitsList中
Info obj = dao.get(id);
hitsList.add(obj);
}
searcher.close();
reader.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

catch (ParseException e) {
e.printStackTrace();

catch (NotExistedException e) {
e.printStackTrace();
}
for(Iterator itor=hitsList.iterator();itor.hasNext();){
Info obj = (Info)itor.next();
log.info("检索结果:"+obj.getId().longValue()+":"+obj.getTitle());
}
return hitsList;
}

你可能感兴趣的:(DAO,sql)