LUCENE-7088, LUCENE-7075: Add PointRangeQueryBuilder to xml-queryparser to replace LegacyNumericRangeQueryBuilder

This commit is contained in:
Robert Muir 2016-03-09 10:06:11 -05:00
parent 9f8fe1239a
commit ba0f63c0c2
5 changed files with 136 additions and 0 deletions

View File

@ -67,6 +67,7 @@ public class CoreParser implements QueryBuilder {
queryFactory.addBuilder("MatchAllDocsQuery", new MatchAllDocsQueryBuilder());
queryFactory.addBuilder("BooleanQuery", new BooleanQueryBuilder(queryFactory));
queryFactory.addBuilder("LegacyNumericRangeQuery", new LegacyNumericRangeQueryBuilder());
queryFactory.addBuilder("PointRangeQuery", new PointRangeQueryBuilder());
queryFactory.addBuilder("RangeQuery", new RangeQueryBuilder());
queryFactory.addBuilder("DisjunctionMaxQuery", new DisjunctionMaxQueryBuilder(queryFactory));
if (parser != null) {

View File

@ -83,7 +83,9 @@ import org.w3c.dom.Element;
* A {@link ParserException} will be thrown if an error occurs parsing the
* supplied <tt>lowerTerm</tt> or <tt>upperTerm</tt> into the numeric type
* specified by <tt>type</tt>.
* @deprecated Index with points and use {@link PointRangeQueryBuilder} instead
*/
@Deprecated
public class LegacyNumericRangeQueryBuilder implements QueryBuilder {
@Override

View File

@ -0,0 +1,95 @@
/*
* 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.search.Query;
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.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:
* <table summary="supported attributes">
* <tr>
* <th>Attribute name</th>
* <th>Values</th>
* <th>Required</th>
* <th>Default</th>
* </tr>
* <tr>
* <td>fieldName</td>
* <td>String</td>
* <td>Yes</td>
* <td>N/A</td>
* </tr>
* <tr>
* <td>lowerTerm</td>
* <td>Specified by <tt>type</tt></td>
* <td>Yes</td>
* <td>N/A</td>
* </tr>
* <tr>
* <td>upperTerm</td>
* <td>Specified by <tt>type</tt></td>
* <td>Yes</td>
* <td>N/A</td>
* </tr>
* <tr>
* <td>type</td>
* <td>int, long, float, double</td>
* <td>No</td>
* <td>int</td>
* </tr>
* </table>
* <p>
* A {@link ParserException} will be thrown if an error occurs parsing the
* supplied <tt>lowerTerm</tt> or <tt>upperTerm</tt> into the numeric type
* specified by <tt>type</tt>.
*/
public class PointRangeQueryBuilder implements QueryBuilder {
@Override
public Query getQuery(Element e) throws ParserException {
String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm");
String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm");
String type = DOMUtils.getAttribute(e, "type", "int");
try {
if (type.equalsIgnoreCase("int")) {
return IntPoint.newRangeQuery(field, Integer.valueOf(lowerTerm), Integer.valueOf(upperTerm));
} else if (type.equalsIgnoreCase("long")) {
return LongPoint.newRangeQuery(field, Long.valueOf(lowerTerm), Long.valueOf(upperTerm));
} else if (type.equalsIgnoreCase("double")) {
return DoublePoint.newRangeQuery(field, Double.valueOf(lowerTerm), Double.valueOf(upperTerm));
} else if (type.equalsIgnoreCase("float")) {
return FloatPoint.newRangeQuery(field, Float.valueOf(lowerTerm), Float.valueOf(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);
}
}
}

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<BooleanQuery fieldName="contents">
<Clause occurs="should">
<TermQuery>merger</TermQuery>
</Clause>
<Clause occurs="mustnot">
<TermQuery >sumitomo</TermQuery>
</Clause>
<Clause occurs="must">
<TermQuery>bank</TermQuery>
</Clause>
<Clause occurs="must">
<PointRangeQuery fieldName="date3" lowerTerm="19870409" upperTerm="19870412"/>
</Clause>
</BooleanQuery>

View File

@ -22,6 +22,7 @@ import org.apache.lucene.analysis.MockTokenFilter;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LegacyIntField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
@ -72,6 +73,7 @@ public class TestCoreParser extends LuceneTestCase {
doc.add(newTextField("date", date, Field.Store.YES));
doc.add(newTextField("contents", content, Field.Store.YES));
doc.add(new LegacyIntField("date2", Integer.valueOf(date), Field.Store.NO));
doc.add(new IntPoint("date3", Integer.valueOf(date)));
writer.addDocument(doc);
line = d.readLine();
}
@ -164,6 +166,11 @@ public class TestCoreParser extends LuceneTestCase {
Query q = parse("LegacyNumericRangeQuery.xml");
dumpResults("LegacyNumericRangeQuery", q, 5);
}
public void testPointRangeQuery() throws ParserException, IOException {
Query q = parse("PointRangeQuery.xml");
dumpResults("PointRangeQuery", q, 5);
}
//================= Helper methods ===================================