分类:
2008-09-09 13:30:12
使用MultiFieldQueryParser类即可。
示例代码:
view plaincopy to clipboardprint?
package com.lucene.search;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("C:\\target\\index\\book");
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new IOException();
}
search(indexDir);
}
public static void search(File indexDir) throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(fsDir);
String[] queries = { "中文版", "8*" };
String[] fields = { "name", "isbn" };
BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
Hits hits = searcher.search(query);
System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
for (int i = 0; i < hits.length(); i++) {
int DocId = hits.id(i);
String DocName = hits.doc(i).get("name");
String DocIsbn = hits.doc(i).get("isbn");
String DocPblDt = hits.doc(i).get("pbl_dt");
System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
}
}
}
package com.lucene.search;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("C:\\target\\index\\book");
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new IOException();
}
search(indexDir);
}
public static void search(File indexDir) throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(fsDir);
String[] queries = { "中文版", "8*" };
String[] fields = { "name", "isbn" };
BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
Hits hits = searcher.search(query);
System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
for (int i = 0; i < hits.length(); i++) {
int DocId = hits.id(i);
String DocName = hits.doc(i).get("name");
String DocIsbn = hits.doc(i).get("isbn");
String DocPblDt = hits.doc(i).get("pbl_dt");
System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
}
}
}注意:BooleanClause.Occur[]数组,它表示多个条件之间的关系:
BooleanClause.Occur.MUST表示and,
BooleanClause.Occur.MUST_NOT表示not,
BooleanClause.Occur.SHOULD表示or.