From ba0f63c0c25ca520b8816ac1ae803d7c7451dcca Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 9 Mar 2016 10:06:11 -0500 Subject: [PATCH] LUCENE-7088, LUCENE-7075: Add PointRangeQueryBuilder to xml-queryparser to replace LegacyNumericRangeQueryBuilder --- .../lucene/queryparser/xml/CoreParser.java | 1 + .../LegacyNumericRangeQueryBuilder.java | 2 + .../xml/builders/PointRangeQueryBuilder.java | 95 +++++++++++++++++++ .../queryparser/xml/PointRangeQuery.xml | 31 ++++++ .../queryparser/xml/TestCoreParser.java | 7 ++ 5 files changed, 136 insertions(+) create mode 100644 lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java create mode 100644 lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java index 6bbb626a750..1416f255bdd 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java @@ -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) { diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java index 2aba681347f..e19596430e2 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java @@ -83,7 +83,9 @@ import org.w3c.dom.Element; * A {@link ParserException} will be thrown if an error occurs parsing the * supplied lowerTerm or upperTerm into the numeric type * specified by type. + * @deprecated Index with points and use {@link PointRangeQueryBuilder} instead */ +@Deprecated public class LegacyNumericRangeQueryBuilder implements QueryBuilder { @Override diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java new file mode 100644 index 00000000000..45483168bac --- /dev/null +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java @@ -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: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Attribute nameValuesRequiredDefault
fieldNameStringYesN/A
lowerTermSpecified by typeYesN/A
upperTermSpecified by typeYesN/A
typeint, long, float, doubleNoint
+ *

+ * 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"); + 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); + } + } +} diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml new file mode 100644 index 00000000000..45af138b8da --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java index e675723e320..5cadec21f3f 100644 --- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java @@ -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 ===================================