下面列出了怎么用org.apache.lucene.search.ConstantScoreRangeQuery的API类实例代码及写法,或者点击链接到github查看源代码。
public static Query rewrite(org.apache.lucene.search.Query q, Set<String> intFields) {
if (q instanceof TermQuery) {
return rewrite((TermQuery)q, intFields);
} else if (q instanceof BooleanQuery) {
return rewrite((BooleanQuery)q, intFields);
} else if (q instanceof RangeQuery) {
return rewrite((RangeQuery)q, intFields);
} else if (q instanceof ConstantScoreRangeQuery) {
return rewrite((ConstantScoreRangeQuery)q, intFields);
} else if (q instanceof PrefixQuery) {
return rewrite((PrefixQuery)q, intFields);
} else if (q instanceof PhraseQuery) {
return rewrite((PhraseQuery)q, intFields);
}
throw new IllegalArgumentException("unsupported lucene query type: " + q.getClass().getSimpleName());
}
/**
* This will look to see if "part1" or "part2" are strings of all digits,
* if they are, then they will be converted to a lexicographically safe string
* representation, then passed into the inherited getRangeQuery(). This is needed when
* comparing something like "4" to be less than "10".
* If the strings don't fit the pattern of all digits, then they get passed through
* to the inherited getRangeQuery().
*/
protected Query getRangeQuery(String field,
String part1,
String part2,
boolean inclusive) throws ParseException {
if (isDate(part1) && isDate(part2)) {
if (log.isDebugEnabled()) {
log.debug("Detected passed in terms are dates, creating " +
"ConstantScoreRangeQuery(" + field + ", " + part1 + ", " +
part2 + ", " + inclusive + ", " + inclusive);
}
return new ConstantScoreRangeQuery(field, part1, part2, inclusive,
inclusive);
}
String newPart1 = part1;
String newPart2 = part2;
String regEx = "(\\d)*";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher1 = pattern.matcher(part1);
Matcher matcher2 = pattern.matcher(part2);
if (matcher1.matches() && matcher2.matches()) {
newPart1 = NumberTools.longToString(Long.parseLong(part1));
newPart2 = NumberTools.longToString(Long.parseLong(part2));
if (log.isDebugEnabled()) {
log.debug("NGramQueryParser.getRangeQuery() Converted " + part1 + " to " +
newPart1 + ", Converted " + part2 + " to " + newPart2);
}
}
return super.getRangeQuery(field, newPart1, newPart2, inclusive);
}
/**
* This will look to see if "part1" or "part2" are strings of all digits,
* if they are, then they will be converted to a lexicographically safe string
* representation, then passed into the inherited getRangeQuery(). This is needed when
* comparing something like "4" to be less than "10".
* If the strings don't fit the pattern of all digits, then they get passed through
* to the inherited getRangeQuery().
*/
protected Query getRangeQuery(String field,
String part1,
String part2,
boolean inclusive) throws ParseException {
if (isDate(part1) && isDate(part2)) {
if (log.isDebugEnabled()) {
log.debug("Detected passed in terms are dates, creating " +
"ConstantScoreRangeQuery(" + field + ", " + part1 + ", " +
part2 + ", " + inclusive + ", " + inclusive);
}
return new ConstantScoreRangeQuery(field, part1, part2, inclusive,
inclusive);
}
String newPart1 = part1;
String newPart2 = part2;
String regEx = "(\\d)*";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher1 = pattern.matcher(part1);
Matcher matcher2 = pattern.matcher(part2);
if (matcher1.matches() && matcher2.matches()) {
newPart1 = NumberTools.longToString(Long.parseLong(part1));
newPart2 = NumberTools.longToString(Long.parseLong(part2));
if (log.isDebugEnabled()) {
log.debug("NGramQueryParser.getRangeQuery() Converted " + part1 + " to " +
newPart1 + ", Converted " + part2 + " to " + newPart2);
}
}
return super.getRangeQuery(field, newPart1, newPart2, inclusive);
}
public static Query rewrite(ConstantScoreRangeQuery rq, Set<String> intFields) {
final Term startTerm = rewriteTerm(new org.apache.lucene.index.Term(rq.getField(), rq.getLowerVal()), intFields);
final Term endTerm = rewriteTerm(new org.apache.lucene.index.Term(rq.getField(), rq.getUpperVal()), intFields);
return Query.newRangeQuery(startTerm, endTerm, rq.includesUpper());
}