You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2016/03/12 01:26:51 UTC

[10/50] [abbrv] lucene-solr git commit: LUCENE-7088, LUCENE-7075: Add PointRangeQueryBuilder to xml-queryparser to replace LegacyNumericRangeQueryBuilder

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


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/116ece2f
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/116ece2f
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/116ece2f

Branch: refs/heads/jira/SOLR-445
Commit: 116ece2fe4386496dd2131bd6b4f11ed01cce7fc
Parents: 770e508
Author: Robert Muir <rm...@apache.org>
Authored: Wed Mar 9 10:06:11 2016 -0500
Committer: Robert Muir <rm...@apache.org>
Committed: Wed Mar 9 10:07:33 2016 -0500

----------------------------------------------------------------------
 .../lucene/queryparser/xml/CoreParser.java      |  1 +
 .../LegacyNumericRangeQueryBuilder.java         |  2 +
 .../xml/builders/PointRangeQueryBuilder.java    | 95 ++++++++++++++++++++
 .../lucene/queryparser/xml/PointRangeQuery.xml  | 31 +++++++
 .../lucene/queryparser/xml/TestCoreParser.java  |  7 ++
 5 files changed, 136 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/116ece2f/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/CoreParser.java
----------------------------------------------------------------------
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 6bbb626..1416f25 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) {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/116ece2f/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java
----------------------------------------------------------------------
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 2aba681..e195964 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 <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

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/116ece2f/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java
----------------------------------------------------------------------
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 0000000..4548316
--- /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:
+ * <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);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/116ece2f/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml
----------------------------------------------------------------------
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 0000000..45af138
--- /dev/null
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQuery.xml
@@ -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>

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/116ece2f/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java
----------------------------------------------------------------------
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 e675723..5cadec2 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 ===================================