RangeQuery的rewrite方法

package query;


import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;


public class Range {


public static void main(String[] args) {
// TODO 自动生成的方法存根
public Query rewrite(IndexReader reader) throws IOException{
//布尔型Query, 用来保存查询重写后的结果
BooleanQuery query = new BooleanQuery(true);
//查找出所有索引中按字典序法在lowerTerm之后的Term
TermEnum enumerator = reader.terms(lowerTerm);

try{
//是 “开区间”还是“闭区间”
boolean checkLower = flase;
if(!inclusive)
checkLower = true;
String testField = getField();

//循环遍历所有的词条
do{
Term term = enumerator.term();
if(term != null && term.field() == testField)
{
//进行比较
if(!checkLower || term.text().compareTo(lowerTerm.text()) > 0)
{
checkLower = false;
if(upperTerm != null)
{
//当小于上界时才考虑,否则就结束循环
int compare = upperTerm.test().compareTo(term.text());
if((compare < 0)||(!inclusive && compare == 0))
break;
}
//把所有匹配的词条组成TermQuery
TermQuery tq = new TermQuery(term);
tq.setBoost(getBoost());

//加入布尔查询
query.add(tq, BooleanClause.Occur.SHOULD); //add to query 
}
}
else
{
break;
}
}
while(enumerator.next());

}
finally{
enumerator.close();
}
return query;
}


}


}

你可能感兴趣的:(RangeQuery的rewrite方法)