lunece多个字段检索

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;
}
Term term = new Term("state","2 OR 3");
Query query3 = new TermQuery(term);
query.add(query3,BooleanClause.Occur.MUST);
state字段在建索引时要为可索引类型
创建索引的代码
public static Document Document(Info obj) throws java.io.IOException {
Document doc = new Document();
// 为article表的主健创建索引,关于Field的几个参数下面有详细解释
Field fieldId = new Field("id", obj.getId().toString(),
Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES);
Field fieldTitle = new Field("title", obj.getTitle(),Field.Store.YES,Field.Index.TOKENIZED);
Field fieldState = new Field("state", obj.getState(),Field.Store.YES,Field.Index.TOKENIZED);
// 为detail字段创建索引,detail在DB中是clob字段,内容为html文本
String contentHtml = obj.getContents();
Reader read = new StringReader(contentHtml);
// 用HTMLParser把detail字段中的HTML分析成文本再索引
// HTMLParser这个类可以在lucene的demo中找到
HTMLParser htmlParser = new HTMLParser(read);
BufferedReader breader = new BufferedReader(htmlParser.getReader());
String tempContent = breader.readLine();
String htmlContent = "";
while (tempContent != null && tempContent.length() > 0) {
htmlContent = htmlContent + tempContent;
tempContent = breader.readLine();
}
Field fieldContents = new Field("content", htmlContent,
Field.Store.YES, Field.Index.TOKENIZED,
Field.TermVector.YES);
// db中的每条纪录对应一个doc,每个字段对应一个field
doc.add(fieldId);
doc.add(fieldTitle);
doc.add(fieldState);
doc.add(fieldContents);
return doc;
}

public void indexFiles(Info obj) {
// 创建索引文件存放路径
File indexDir = new File(util.getPropertyValue("IndexFileDir"));
try {
Date start = new Date();
// 创建分析器,主要用于从文本中抽取那些需要建立索引的内容,把不需要参与建索引的文本内容去掉.
// 比如去掉一些a the之类的常用词,还有决定是否大小写敏感.
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
// 参数true用于确定是否覆盖原有索引的
if(!indexDir.exists()){
log.info("索引路径不存在!");
return;
}
IndexWriter indexWriter = null;
if(indexDir.listFiles().length>0){
indexWriter = new IndexWriter(indexDir,standardAnalyzer, false);
}else{
indexWriter = new IndexWriter(indexDir,standardAnalyzer, true);
}
indexWriter.setMergeFactor(100);
indexWriter.setMaxBufferedDocs(100);
// 只索引这个Field的前5000个字,默认为10000
indexWriter.setMaxFieldLength(5000);
// 在Document方法是创建索引的具体代码
Document doc = Document(obj);
indexWriter.addDocument(doc);
// Optimize的过程就是要减少剩下的Segment的数量,尽量让它们处于一个文件中.
indexWriter.optimize();
indexWriter.close();
Date end = new Date();
log.info("创建索引: "
+ (end.getTime() - start.getTime())
+ " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass()
+ "\n with message: " + e.getMessage());
}
}
query.add(query3,BooleanClause.Occur.MUST);

这个query是谁的对象?怎么生成的,能说的具体点吗,谢谢!
Query query = parser.parse(keyword);
你这里的对象啊
大哥,这个对象也没有add方法啊
看错了,是bquerymain ,query3就是替换你原来的那个query2