Apache Solr open-source search software
SOLR-4538: Date Math expressions were being truncated to 32 characters when used in field:value queries in the lucene QParser
Chris M. Hostettera5c4101d9bac
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 684a192edc4..0aa3497d0dc 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -75,6 +75,10 @@ Bug Fixeshandler-level parameters were specified containing periods in the name(James Dyer)+* SOLR-4538: Date Math expressions were being truncated to 32 characters+ when used in field:value queries in the lucene QParser. (hossman, yonik)++Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java b/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.javaindex 7d49f9b45a2..196fecee197 100644--- a/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java+++ b/solr/core/src/java/org/apache/solr/analysis/TrieTokenizerFactory.java@@ -17,6 +17,8 @@package org.apache.solr.analysis;import org.apache.lucene.analysis.NumericTokenStream;+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;+import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;import org.apache.lucene.analysis.Tokenizer;import org.apache.lucene.analysis.util.TokenizerFactory;@@ -59,10 +61,11 @@ final class TrieTokenizer extends Tokenizer {protected final TrieTypes type;protected final NumericTokenStream ts;+ // NumericTokenStream does not support CharTermAttribute so keep it local+ private final CharTermAttribute termAtt = new CharTermAttributeImpl();protected final OffsetAttribute ofsAtt = addAttribute(OffsetAttribute.class);protected int startOfs, endOfs;protected boolean hasValue;- protected final char[] buf = new char[32];static NumericTokenStream getNumericTokenStream(int precisionStep) {return new NumericTokenStream(precisionStep);@@ -73,22 +76,23 @@ final class TrieTokenizer extends Tokenizer {super(ts, input);this.type = type;this.ts = ts;+ // dates tend to be longer, especially when math is involved+ termAtt.resizeBuffer( type == TrieTypes.DATE ? 128 : 32 );}@Overridepublic void reset() {try {int upto = 0;- while (upto < buf.length) {- final int length = input.read(buf, upto, buf.length - upto);+ char[] buf = termAtt.buffer();+ while (true) {+ final int length = input.read(buf, upto, buf.length-upto);if (length == -1) break;upto += length;+ if (upto == buf.length)+ buf = termAtt.resizeBuffer(1+buf.length);}- // skip remaining data if buffer was too short:- if (upto == buf.length) {- input.skip(Long.MAX_VALUE);- }-+ termAtt.setLength(upto);this.startOfs = correctOffset(0);this.endOfs = correctOffset(upto);diff --git a/solr/core/src/test/org/apache/solr/BasicFunctionalityTest.java b/solr/core/src/test/org/apache/solr/BasicFunctionalityTest.javaindex b4636437c6f..5cfe0733fdb 100644--- a/solr/core/src/test/org/apache/solr/BasicFunctionalityTest.java+++ b/solr/core/src/test/org/apache/solr/BasicFunctionalityTest.java@@ -781,7 +781,35 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {assertU(adoc("id", "5", "bday", "NOW+30MINUTES"));assertU(adoc("id", "6", "bday", "NOW+2YEARS"));assertU(commit());++ // a ridiculoulsy long date math expression that's still equivilent to july4+ final StringBuilder july4Long = new StringBuilder(july4);+ final int iters = atLeast(10);+ for (int i = 0; i < iters; i++) {+ final String val = String.valueOf(atLeast(1));+ july4Long.append("+" + val + "SECONDS-" + val + "SECONDS");+ }++ // term queries using date math (all of these should match doc#1)+ for (String q :+ new String[] {+ "bday:1976-07-04T12\\:08\\:56.45Z/SECOND+235MILLIS",+ "bday:1976-07-04T12\\:08\\:56.123Z/MINUTE+56SECONDS+235MILLIS",+ "bday:\"1976-07-04T12:08:56.45Z/SECOND+235MILLIS\"",+ "bday:\"1976-07-04T12:08:56.123Z/MINUTE+56SECONDS+235MILLIS\"",+ "{!term f=bday}1976-07-04T12:08:56.45Z/SECOND+235MILLIS",+ "{!term f=bday}1976-07-04T12:08:56.123Z/MINUTE+56SECONDS+235MILLIS",+ "{!term f=bday}"+july4,+ "{!term f=bday}"+july4Long,+ "bday:\"" + july4Long + "\""+ }) {+ assertQ("check math on field query: " + q,+ req("q", q),+ "*[count(//doc)=1]",+ "//int[@name='id'][.='1']");+ }+ // range queries using date mathassertQ("check math on absolute date#1",req("q", "bday:[* TO "+july4+"/SECOND]"),"*[count(//doc)=0]");diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.javaindex b2370ab6a0b..cb1b666a17b 100644--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java@@ -75,7 +75,20 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {private static final Set<String> valParsersTested = new HashSet<String>();+ public void testDateMathParsingEquality() throws Exception {+ // regardless of parser, these should all be equivilent queries+ assertQueryEquals+ (null+ ,"{!lucene}f_tdt:2013-09-11T00\\:00\\:00Z"+ ,"{!lucene}f_tdt:2013-03-08T00\\:46\\:15Z/DAY+6MONTHS+3DAYS"+ ,"{!lucene}f_tdt:\"2013-03-08T00:46:15Z/DAY+6MONTHS+3DAYS\""+ ,"{!field f=f_tdt}2013-03-08T00:46:15Z/DAY+6MONTHS+3DAYS"+ ,"{!field f=f_tdt}2013-09-11T00:00:00Z"+ ,"{!term f=f_tdt}2013-03-08T00:46:15Z/DAY+6MONTHS+3DAYS"+ ,"{!term f=f_tdt}2013-09-11T00:00:00Z"+ );+ }public void testQueryLucene() throws Exception {assertQueryEquals("lucene", "{!lucene}apache solr","apache solr", "apache solr ");@@ -720,8 +733,8 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {}/**- * NOTE: defType is not only used to pick the parser, but also to record- * the parser being tested for coverage sanity checking+ * NOTE: defType is not only used to pick the parser, but, if non-null it is+ * also to record the parser being tested for coverage sanity checking** @see QueryUtils#check* @see QueryUtils#checkEqual@@ -730,7 +743,8 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {protected void assertQueryEquals(final String defType,final SolrQueryRequest req,final String... inputs) throws Exception {- qParsersTested.add(defType);++ if (null != defType) qParsersTested.add(defType);final Query[] queries = new Query[inputs.length];diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.javaindex 2b7c89659b4..a8864d3a515 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java@@ -104,6 +104,11 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {);assertJQ(req("q","doesnotexist_s:( * * * )"),"/response/numFound==0" // nothing should be found+ );++ // length of date math caused issues...+ assertJQ(req("q","foo_dt:\"2013-03-08T00:46:15Z/DAY+000MILLISECONDS+00SECONDS+00MINUTES+00HOURS+0000000000YEARS+6MONTHS+3DAYS\"", "debug","query")+ ,"/debug/parsedquery=='foo_dt:2013-09-11T00:00:00Z'");}