org.apache.lucene.search.ConstantScoreRangeQuery Java Examples

The following examples show how to use org.apache.lucene.search.ConstantScoreRangeQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LuceneQueryTranslator.java    From imhotep with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #2
Source File: NGramQueryParser.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: NGramQueryParser.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: LuceneQueryTranslator.java    From imhotep with Apache License 2.0 4 votes vote down vote up
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());
}