多域搜索MultiFieldQueryParser

package query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query;


public class QueryOne {
//在不同的Field上进行不同的查找
public static Query parse(String[] queries, String[] fields, Analyzer analyzer) throws ParseException
{
//判断数组的长度是否相同
//如果不同,则抛出异常
if(queries.length != fields.length)
throw new IllegalArgumentException("querise.length != fields.length");

//构建一个布尔型查询
BooleanQuery bQuery = new BooleanQuery();

//遍历所有Field
for(int i = 0; i < fields.length; i++)
{
//对每个Field,都构造一个QueryParser对象
QueryParser qp = new QueryParser(fields[i], analyzer);

//按顺序将查询字符串转为Query对象
Query q = qp.parse(queries[i]);

//添加到Boolean查询中,各种查询子句间的关系为Occur.SHOULD
bQuery.add(q, BooleanCluase.Occur.SHOULD);
}
return bQuery;
}

//在不同的Field上进行同一个查找,指定他们之间的布尔关系
public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException
{

if(fields.length != flags.length)
throw new IllegalArgumentException("fields.length != flags.length");

//构建一个布尔型查询
BooleanQuery bQuery = new BooleanQuery();

//遍历所有Field
for(int i = 0; i < fields.length; i++)
{
//对每个Field,都构造一个QueryParser对象
QueryParser qp = new QueryParser(fields[i], analyzer);

//按顺序将查询字符串转为Query对象
Query q = qp.parse(query);

//添加到Boolean查询中,各种查询子句间的关系为Occur.SHOULD
bQuery.add(q, flags[i]);
}
return bQuery;
}

//在不同的Field上进行不同的查找,指定它们之间的布尔关系
public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException
{

if(fields.length != flags.length)
throw new IllegalArgumentException("fields.length != flags.length");

//构建一个布尔型查询
BooleanQuery bQuery = new BooleanQuery();

//遍历所有Field
for(int i = 0; i < fields.length; i++)
{
//对每个Field,都构造一个QueryParser对象
QueryParser qp = new QueryParser(fields[i], analyzer);

//按顺序将查询字符串转为Query对象
Query q = qp.parse(query);

//添加到Boolean查询中,各种查询子句间的关系为Occur.SHOULD
bQuery.add(q, flags[i]);
}
return bQuery;
}


public static void main(String[] args) {
// TODO 自动生成的方法存根



}


}

你可能感兴趣的:(多域搜索MultiFieldQueryParser)