org.apache.lucene.queryparser.xml.builders.PointRangeQueryBuilder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.queryparser.xml.builders;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.queryparser.xml.DOMUtils;
import org.apache.lucene.queryparser.xml.ParserException;
import org.apache.lucene.queryparser.xml.QueryBuilder;
import org.apache.lucene.search.Query;
import org.w3c.dom.Element;
/**
* Creates a range query across 1D {@link PointValues}. The table below specifies the required
* attributes and the defaults if optional attributes are omitted:
*
*
* supported attributes
*
* Attribute name
* Values
* Required
* Default
*
*
* fieldName
* String
* Yes
* N/A
*
*
* lowerTerm
* Specified by type
* No
* Integer.MIN_VALUE Long.MIN_VALUE Float.NEGATIVE_INFINITY Double.NEGATIVE_INFINITY
*
*
* upperTerm
* Specified by type
* No
* Integer.MAX_VALUE Long.MAX_VALUE Float.POSITIVE_INFINITY Double.POSITIVE_INFINITY
*
*
* type
* int, long, float, double
* No
* int
*
*
*
* A {@link ParserException} will be thrown if an error occurs parsing the supplied
* lowerTerm
or upperTerm
into the numeric type specified by type
.
*/
public class PointRangeQueryBuilder implements QueryBuilder {
@Override
public Query getQuery(Element e) throws ParserException {
String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null);
final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null);
String type = DOMUtils.getAttribute(e, "type", "int");
try {
if (type.equalsIgnoreCase("int")) {
return IntPoint.newRangeQuery(
field,
(lowerTerm == null ? Integer.MIN_VALUE : Integer.parseInt(lowerTerm)),
(upperTerm == null ? Integer.MAX_VALUE : Integer.parseInt(upperTerm)));
} else if (type.equalsIgnoreCase("long")) {
return LongPoint.newRangeQuery(
field,
(lowerTerm == null ? Long.MIN_VALUE : Long.parseLong(lowerTerm)),
(upperTerm == null ? Long.MAX_VALUE : Long.parseLong(upperTerm)));
} else if (type.equalsIgnoreCase("double")) {
return DoublePoint.newRangeQuery(
field,
(lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.parseDouble(lowerTerm)),
(upperTerm == null ? Double.POSITIVE_INFINITY : Double.parseDouble(upperTerm)));
} else if (type.equalsIgnoreCase("float")) {
return FloatPoint.newRangeQuery(
field,
(lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.parseFloat(lowerTerm)),
(upperTerm == null ? Float.POSITIVE_INFINITY : Float.parseFloat(upperTerm)));
} else {
throw new ParserException("type attribute must be one of: [long, int, double, float]");
}
} catch (NumberFormatException nfe) {
throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
}
}
}