You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2016/08/17 13:29:54 UTC
[3/7] lucene-solr:master: LUCENE-7413: move legacy numeric support to
backwards module
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/105c7eae/lucene/core/src/test/org/apache/lucene/index/TestFieldReuse.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldReuse.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldReuse.java
index b36cfef..977df3d 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestFieldReuse.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldReuse.java
@@ -24,16 +24,12 @@ import java.util.Collections;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
import org.apache.lucene.analysis.CannedTokenStream;
-import org.apache.lucene.analysis.LegacyNumericTokenStream.LegacyNumericTermAttribute;
-import org.apache.lucene.analysis.LegacyNumericTokenStream;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field;
-import org.apache.lucene.document.LegacyIntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.LegacyNumericUtils;
/** test tokenstream reuse by DefaultIndexingChain */
public class TestFieldReuse extends BaseTokenStreamTestCase {
@@ -61,7 +57,7 @@ public class TestFieldReuse extends BaseTokenStreamTestCase {
// pass a bogus stream and ensure it's still ok
stringField = new StringField("foo", "beer", Field.Store.NO);
- TokenStream bogus = new LegacyNumericTokenStream();
+ TokenStream bogus = new CannedTokenStream();
ts = stringField.tokenStream(null, bogus);
assertNotSame(ts, bogus);
assertTokenStreamContents(ts,
@@ -71,37 +67,6 @@ public class TestFieldReuse extends BaseTokenStreamTestCase {
);
}
- public void testNumericReuse() throws IOException {
- LegacyIntField legacyIntField = new LegacyIntField("foo", 5, Field.Store.NO);
-
- // passing null
- TokenStream ts = legacyIntField.tokenStream(null, null);
- assertTrue(ts instanceof LegacyNumericTokenStream);
- assertEquals(LegacyNumericUtils.PRECISION_STEP_DEFAULT_32, ((LegacyNumericTokenStream)ts).getPrecisionStep());
- assertNumericContents(5, ts);
-
- // now reuse previous stream
- legacyIntField = new LegacyIntField("foo", 20, Field.Store.NO);
- TokenStream ts2 = legacyIntField.tokenStream(null, ts);
- assertSame(ts, ts2);
- assertNumericContents(20, ts);
-
- // pass a bogus stream and ensure it's still ok
- legacyIntField = new LegacyIntField("foo", 2343, Field.Store.NO);
- TokenStream bogus = new CannedTokenStream(new Token("bogus", 0, 5));
- ts = legacyIntField.tokenStream(null, bogus);
- assertNotSame(bogus, ts);
- assertNumericContents(2343, ts);
-
- // pass another bogus stream (numeric, but different precision step!)
- legacyIntField = new LegacyIntField("foo", 42, Field.Store.NO);
- assert 3 != LegacyNumericUtils.PRECISION_STEP_DEFAULT;
- bogus = new LegacyNumericTokenStream(3);
- ts = legacyIntField.tokenStream(null, bogus);
- assertNotSame(bogus, ts);
- assertNumericContents(42, ts);
- }
-
static class MyField implements IndexableField {
TokenStream lastSeen;
TokenStream lastReturned;
@@ -163,20 +128,4 @@ public class TestFieldReuse extends BaseTokenStreamTestCase {
iw.close();
dir.close();
}
-
- private void assertNumericContents(int value, TokenStream ts) throws IOException {
- assertTrue(ts instanceof LegacyNumericTokenStream);
- LegacyNumericTermAttribute numericAtt = ts.getAttribute(LegacyNumericTermAttribute.class);
- ts.reset();
- boolean seen = false;
- while (ts.incrementToken()) {
- if (numericAtt.getShift() == 0) {
- assertEquals(value, numericAtt.getRawValue());
- seen = true;
- }
- }
- ts.end();
- ts.close();
- assertTrue(seen);
- }
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/105c7eae/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTerms.java b/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
index 0ee3447..e8871dc 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
@@ -18,17 +18,11 @@ package org.apache.lucene.index;
import org.apache.lucene.analysis.CannedBinaryTokenStream;
import org.apache.lucene.document.Document;
-import org.apache.lucene.document.LegacyDoubleField;
import org.apache.lucene.document.Field;
-import org.apache.lucene.document.LegacyFloatField;
-import org.apache.lucene.document.LegacyIntField;
-import org.apache.lucene.document.LegacyLongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.LegacyNumericUtils;
import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.TestUtil;
public class TestTerms extends LuceneTestCase {
@@ -88,132 +82,4 @@ public class TestTerms extends LuceneTestCase {
w.close();
dir.close();
}
-
- public void testEmptyIntFieldMinMax() throws Exception {
- assertNull(LegacyNumericUtils.getMinInt(EMPTY_TERMS));
- assertNull(LegacyNumericUtils.getMaxInt(EMPTY_TERMS));
- }
-
- public void testIntFieldMinMax() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir);
- int numDocs = atLeast(100);
- int minValue = Integer.MAX_VALUE;
- int maxValue = Integer.MIN_VALUE;
- for(int i=0;i<numDocs;i++ ){
- Document doc = new Document();
- int num = random().nextInt();
- minValue = Math.min(num, minValue);
- maxValue = Math.max(num, maxValue);
- doc.add(new LegacyIntField("field", num, Field.Store.NO));
- w.addDocument(doc);
- }
-
- IndexReader r = w.getReader();
- Terms terms = MultiFields.getTerms(r, "field");
- assertEquals(new Integer(minValue), LegacyNumericUtils.getMinInt(terms));
- assertEquals(new Integer(maxValue), LegacyNumericUtils.getMaxInt(terms));
-
- r.close();
- w.close();
- dir.close();
- }
-
- public void testEmptyLongFieldMinMax() throws Exception {
- assertNull(LegacyNumericUtils.getMinLong(EMPTY_TERMS));
- assertNull(LegacyNumericUtils.getMaxLong(EMPTY_TERMS));
- }
-
- public void testLongFieldMinMax() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir);
- int numDocs = atLeast(100);
- long minValue = Long.MAX_VALUE;
- long maxValue = Long.MIN_VALUE;
- for(int i=0;i<numDocs;i++ ){
- Document doc = new Document();
- long num = random().nextLong();
- minValue = Math.min(num, minValue);
- maxValue = Math.max(num, maxValue);
- doc.add(new LegacyLongField("field", num, Field.Store.NO));
- w.addDocument(doc);
- }
-
- IndexReader r = w.getReader();
-
- Terms terms = MultiFields.getTerms(r, "field");
- assertEquals(new Long(minValue), LegacyNumericUtils.getMinLong(terms));
- assertEquals(new Long(maxValue), LegacyNumericUtils.getMaxLong(terms));
-
- r.close();
- w.close();
- dir.close();
- }
-
- public void testFloatFieldMinMax() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir);
- int numDocs = atLeast(100);
- float minValue = Float.POSITIVE_INFINITY;
- float maxValue = Float.NEGATIVE_INFINITY;
- for(int i=0;i<numDocs;i++ ){
- Document doc = new Document();
- float num = random().nextFloat();
- minValue = Math.min(num, minValue);
- maxValue = Math.max(num, maxValue);
- doc.add(new LegacyFloatField("field", num, Field.Store.NO));
- w.addDocument(doc);
- }
-
- IndexReader r = w.getReader();
- Terms terms = MultiFields.getTerms(r, "field");
- assertEquals(minValue, NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMinInt(terms)), 0.0f);
- assertEquals(maxValue, NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMaxInt(terms)), 0.0f);
-
- r.close();
- w.close();
- dir.close();
- }
-
- public void testDoubleFieldMinMax() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir);
- int numDocs = atLeast(100);
- double minValue = Double.POSITIVE_INFINITY;
- double maxValue = Double.NEGATIVE_INFINITY;
- for(int i=0;i<numDocs;i++ ){
- Document doc = new Document();
- double num = random().nextDouble();
- minValue = Math.min(num, minValue);
- maxValue = Math.max(num, maxValue);
- doc.add(new LegacyDoubleField("field", num, Field.Store.NO));
- w.addDocument(doc);
- }
-
- IndexReader r = w.getReader();
-
- Terms terms = MultiFields.getTerms(r, "field");
-
- assertEquals(minValue, NumericUtils.sortableLongToDouble(LegacyNumericUtils.getMinLong(terms)), 0.0);
- assertEquals(maxValue, NumericUtils.sortableLongToDouble(LegacyNumericUtils.getMaxLong(terms)), 0.0);
-
- r.close();
- w.close();
- dir.close();
- }
-
- /**
- * A complete empty Terms instance that has no terms in it and supports no optional statistics
- */
- private static Terms EMPTY_TERMS = new Terms() {
- public TermsEnum iterator() { return TermsEnum.EMPTY; }
- public long size() { return -1; }
- public long getSumTotalTermFreq() { return -1; }
- public long getSumDocFreq() { return -1; }
- public int getDocCount() { return -1; }
- public boolean hasFreqs() { return false; }
- public boolean hasOffsets() { return false; }
- public boolean hasPositions() { return false; }
- public boolean hasPayloads() { return false; }
- };
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/105c7eae/lucene/core/src/test/org/apache/lucene/search/TestMultiValuedNumericRangeQuery.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMultiValuedNumericRangeQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestMultiValuedNumericRangeQuery.java
deleted file mode 100644
index eba37ce..0000000
--- a/lucene/core/src/test/org/apache/lucene/search/TestMultiValuedNumericRangeQuery.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.search;
-
-
-import java.util.Locale;
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-
-import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.document.LegacyIntField;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.RandomIndexWriter;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.TestUtil;
-
-public class TestMultiValuedNumericRangeQuery extends LuceneTestCase {
-
- /** Tests LegacyNumericRangeQuery on a multi-valued field (multiple numeric values per document).
- * This test ensures, that a classical TermRangeQuery returns exactly the same document numbers as
- * LegacyNumericRangeQuery (see SOLR-1322 for discussion) and the multiple precision terms per numeric value
- * do not interfere with multiple numeric values.
- */
- public void testMultiValuedNRQ() throws Exception {
- Directory directory = newDirectory();
- RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
- newIndexWriterConfig(new MockAnalyzer(random()))
- .setMaxBufferedDocs(TestUtil.nextInt(random(), 50, 1000)));
-
- DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.ROOT));
-
- int num = atLeast(500);
- for (int l = 0; l < num; l++) {
- Document doc = new Document();
- for (int m=0, c=random().nextInt(10); m<=c; m++) {
- int value = random().nextInt(Integer.MAX_VALUE);
- doc.add(newStringField("asc", format.format(value), Field.Store.NO));
- doc.add(new LegacyIntField("trie", value, Field.Store.NO));
- }
- writer.addDocument(doc);
- }
- IndexReader reader = writer.getReader();
- writer.close();
-
- IndexSearcher searcher=newSearcher(reader);
- num = atLeast(50);
- for (int i = 0; i < num; i++) {
- int lower=random().nextInt(Integer.MAX_VALUE);
- int upper=random().nextInt(Integer.MAX_VALUE);
- if (lower>upper) {
- int a=lower; lower=upper; upper=a;
- }
- TermRangeQuery cq=TermRangeQuery.newStringRange("asc", format.format(lower), format.format(upper), true, true);
- LegacyNumericRangeQuery<Integer> tq= LegacyNumericRangeQuery.newIntRange("trie", lower, upper, true, true);
- TopDocs trTopDocs = searcher.search(cq, 1);
- TopDocs nrTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", trTopDocs.totalHits, nrTopDocs.totalHits );
- }
- reader.close();
- directory.close();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/105c7eae/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java b/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java
deleted file mode 100644
index 3a82ff3..0000000
--- a/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java
+++ /dev/null
@@ -1,589 +0,0 @@
-/*
- * 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.search;
-
-
-import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.document.FieldType;
-import org.apache.lucene.document.LegacyFloatField;
-import org.apache.lucene.document.LegacyIntField;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.MultiFields;
-import org.apache.lucene.index.RandomIndexWriter;
-import org.apache.lucene.index.Terms;
-import org.apache.lucene.index.TermsEnum;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.BytesRefBuilder;
-import org.apache.lucene.util.LegacyNumericUtils;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.NumericUtils;
-import org.apache.lucene.util.TestLegacyNumericUtils; // NaN arrays
-import org.apache.lucene.util.TestUtil;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class TestNumericRangeQuery32 extends LuceneTestCase {
- // distance of entries
- private static int distance;
- // shift the starting of the values to the left, to also have negative values:
- private static final int startOffset = - 1 << 15;
- // number of docs to generate for testing
- private static int noDocs;
-
- private static Directory directory = null;
- private static IndexReader reader = null;
- private static IndexSearcher searcher = null;
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- noDocs = atLeast(4096);
- distance = (1 << 30) / noDocs;
- directory = newDirectory();
- RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
- newIndexWriterConfig(new MockAnalyzer(random()))
- .setMaxBufferedDocs(TestUtil.nextInt(random(), 100, 1000))
- .setMergePolicy(newLogMergePolicy()));
-
- final FieldType storedInt = new FieldType(LegacyIntField.TYPE_NOT_STORED);
- storedInt.setStored(true);
- storedInt.freeze();
-
- final FieldType storedInt8 = new FieldType(storedInt);
- storedInt8.setNumericPrecisionStep(8);
-
- final FieldType storedInt4 = new FieldType(storedInt);
- storedInt4.setNumericPrecisionStep(4);
-
- final FieldType storedInt2 = new FieldType(storedInt);
- storedInt2.setNumericPrecisionStep(2);
-
- final FieldType storedIntNone = new FieldType(storedInt);
- storedIntNone.setNumericPrecisionStep(Integer.MAX_VALUE);
-
- final FieldType unstoredInt = LegacyIntField.TYPE_NOT_STORED;
-
- final FieldType unstoredInt8 = new FieldType(unstoredInt);
- unstoredInt8.setNumericPrecisionStep(8);
-
- final FieldType unstoredInt4 = new FieldType(unstoredInt);
- unstoredInt4.setNumericPrecisionStep(4);
-
- final FieldType unstoredInt2 = new FieldType(unstoredInt);
- unstoredInt2.setNumericPrecisionStep(2);
-
- LegacyIntField
- field8 = new LegacyIntField("field8", 0, storedInt8),
- field4 = new LegacyIntField("field4", 0, storedInt4),
- field2 = new LegacyIntField("field2", 0, storedInt2),
- fieldNoTrie = new LegacyIntField("field"+Integer.MAX_VALUE, 0, storedIntNone),
- ascfield8 = new LegacyIntField("ascfield8", 0, unstoredInt8),
- ascfield4 = new LegacyIntField("ascfield4", 0, unstoredInt4),
- ascfield2 = new LegacyIntField("ascfield2", 0, unstoredInt2);
-
- Document doc = new Document();
- // add fields, that have a distance to test general functionality
- doc.add(field8); doc.add(field4); doc.add(field2); doc.add(fieldNoTrie);
- // add ascending fields with a distance of 1, beginning at -noDocs/2 to test the correct splitting of range and inclusive/exclusive
- doc.add(ascfield8); doc.add(ascfield4); doc.add(ascfield2);
-
- // Add a series of noDocs docs with increasing int values
- for (int l=0; l<noDocs; l++) {
- int val=distance*l+startOffset;
- field8.setIntValue(val);
- field4.setIntValue(val);
- field2.setIntValue(val);
- fieldNoTrie.setIntValue(val);
-
- val=l-(noDocs/2);
- ascfield8.setIntValue(val);
- ascfield4.setIntValue(val);
- ascfield2.setIntValue(val);
- writer.addDocument(doc);
- }
-
- reader = writer.getReader();
- searcher=newSearcher(reader);
- writer.close();
- }
-
- @AfterClass
- public static void afterClass() throws Exception {
- searcher = null;
- reader.close();
- reader = null;
- directory.close();
- directory = null;
- }
-
- @Override
- public void setUp() throws Exception {
- super.setUp();
- // set the theoretical maximum term count for 8bit (see docs for the number)
- // super.tearDown will restore the default
- BooleanQuery.setMaxClauseCount(3*255*2 + 255);
- }
-
- /** test for both constant score and boolean query, the other tests only use the constant score mode */
- private void testRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
- LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
- for (byte i=0; i<2; i++) {
- TopDocs topDocs;
- String type;
- switch (i) {
- case 0:
- type = " (constant score filter rewrite)";
- q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- break;
- case 1:
- type = " (constant score boolean rewrite)";
- q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- break;
- default:
- return;
- }
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count"+type, count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc"+type, 2*distance+startOffset, doc.getField(field).numericValue().intValue());
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc"+type, (1+count)*distance+startOffset, doc.getField(field).numericValue().intValue());
- }
- }
-
- @Test
- public void testRange_8bit() throws Exception {
- testRange(8);
- }
-
- @Test
- public void testRange_4bit() throws Exception {
- testRange(4);
- }
-
- @Test
- public void testRange_2bit() throws Exception {
- testRange(2);
- }
-
- @Test
- public void testOneMatchQuery() throws Exception {
- LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange("ascfield8", 8, 1000, 1000, true, true);
- TopDocs topDocs = searcher.search(q, noDocs);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", 1, sd.length );
- }
-
- private void testLeftOpenRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- int upper=(count-1)*distance + (distance/3) + startOffset;
- LegacyNumericRangeQuery<Integer> q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true);
- TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", startOffset, doc.getField(field).numericValue().intValue());
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
-
- q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, null, upper, false, true);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", count, sd.length );
- doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", startOffset, doc.getField(field).numericValue().intValue());
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
- }
-
- @Test
- public void testLeftOpenRange_8bit() throws Exception {
- testLeftOpenRange(8);
- }
-
- @Test
- public void testLeftOpenRange_4bit() throws Exception {
- testLeftOpenRange(4);
- }
-
- @Test
- public void testLeftOpenRange_2bit() throws Exception {
- testLeftOpenRange(2);
- }
-
- private void testRightOpenRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- int lower=(count-1)*distance + (distance/3) +startOffset;
- LegacyNumericRangeQuery<Integer> q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true);
- TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", noDocs-count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().intValue());
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
-
- q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", noDocs-count, sd.length );
- doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().intValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().intValue() );
- }
-
- @Test
- public void testRightOpenRange_8bit() throws Exception {
- testRightOpenRange(8);
- }
-
- @Test
- public void testRightOpenRange_4bit() throws Exception {
- testRightOpenRange(4);
- }
-
- @Test
- public void testRightOpenRange_2bit() throws Exception {
- testRightOpenRange(2);
- }
-
- @Test
- public void testInfiniteValues() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter writer = new RandomIndexWriter(random(), dir,
- newIndexWriterConfig(new MockAnalyzer(random())));
- Document doc = new Document();
- doc.add(new LegacyFloatField("float", Float.NEGATIVE_INFINITY, Field.Store.NO));
- doc.add(new LegacyIntField("int", Integer.MIN_VALUE, Field.Store.NO));
- writer.addDocument(doc);
-
- doc = new Document();
- doc.add(new LegacyFloatField("float", Float.POSITIVE_INFINITY, Field.Store.NO));
- doc.add(new LegacyIntField("int", Integer.MAX_VALUE, Field.Store.NO));
- writer.addDocument(doc);
-
- doc = new Document();
- doc.add(new LegacyFloatField("float", 0.0f, Field.Store.NO));
- doc.add(new LegacyIntField("int", 0, Field.Store.NO));
- writer.addDocument(doc);
-
- for (float f : TestLegacyNumericUtils.FLOAT_NANs) {
- doc = new Document();
- doc.add(new LegacyFloatField("float", f, Field.Store.NO));
- writer.addDocument(doc);
- }
-
- writer.close();
-
- IndexReader r = DirectoryReader.open(dir);
- IndexSearcher s = newSearcher(r);
-
- Query q= LegacyNumericRangeQuery.newIntRange("int", null, null, true, true);
- TopDocs topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newIntRange("int", null, null, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newIntRange("int", Integer.MIN_VALUE, Integer.MAX_VALUE, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newIntRange("int", Integer.MIN_VALUE, Integer.MAX_VALUE, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newFloatRange("float", null, null, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newFloatRange("float", null, null, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newFloatRange("float", Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newFloatRange("float", Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newFloatRange("float", Float.NaN, Float.NaN, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", TestLegacyNumericUtils.FLOAT_NANs.length, topDocs.scoreDocs.length );
-
- r.close();
- dir.close();
- }
-
- private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int totalTermCountT=0,totalTermCountC=0,termCountT,termCountC;
- int num = TestUtil.nextInt(random(), 10, 20);
- for (int i = 0; i < num; i++) {
- int lower=(int)(random().nextDouble()*noDocs*distance)+startOffset;
- int upper=(int)(random().nextDouble()*noDocs*distance)+startOffset;
- if (lower>upper) {
- int a=lower; lower=upper; upper=a;
- }
- final BytesRef lowerBytes, upperBytes;
- BytesRefBuilder b = new BytesRefBuilder();
- LegacyNumericUtils.intToPrefixCoded(lower, 0, b);
- lowerBytes = b.toBytesRef();
- LegacyNumericUtils.intToPrefixCoded(upper, 0, b);
- upperBytes = b.toBytesRef();
-
- // test inclusive range
- LegacyNumericRangeQuery<Integer> tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
- TermRangeQuery cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- TopDocs cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test left exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test right exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- }
-
- checkTermCounts(precisionStep, totalTermCountT, totalTermCountC);
- if (VERBOSE && precisionStep != Integer.MAX_VALUE) {
- System.out.println("Average number of terms during random search on '" + field + "':");
- System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4)));
- System.out.println(" Classical query: " + (((double)totalTermCountC)/(num * 4)));
- }
- }
-
- @Test
- public void testEmptyEnums() throws Exception {
- int count=3000;
- int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
- // test empty enum
- assert lower < upper;
- assertTrue(0 < countTerms(LegacyNumericRangeQuery.newIntRange("field4", 4, lower, upper, true, true)));
- assertEquals(0, countTerms(LegacyNumericRangeQuery.newIntRange("field4", 4, upper, lower, true, true)));
- // test empty enum outside of bounds
- lower = distance*noDocs+startOffset;
- upper = 2 * lower;
- assert lower < upper;
- assertEquals(0, countTerms(LegacyNumericRangeQuery.newIntRange("field4", 4, lower, upper, true, true)));
- }
-
- private int countTerms(MultiTermQuery q) throws Exception {
- final Terms terms = MultiFields.getTerms(reader, q.getField());
- if (terms == null)
- return 0;
- final TermsEnum termEnum = q.getTermsEnum(terms);
- assertNotNull(termEnum);
- int count = 0;
- BytesRef cur, last = null;
- while ((cur = termEnum.next()) != null) {
- count++;
- if (last != null) {
- assertTrue(last.compareTo(cur) < 0);
- }
- last = BytesRef.deepCopyOf(cur);
- }
- // LUCENE-3314: the results after next() already returned null are undefined,
- // assertNull(termEnum.next());
- return count;
- }
-
- private void checkTermCounts(int precisionStep, int termCountT, int termCountC) {
- if (precisionStep == Integer.MAX_VALUE) {
- assertEquals("Number of terms should be equal for unlimited precStep", termCountC, termCountT);
- } else {
- assertTrue("Number of terms for NRQ should be <= compared to classical TRQ", termCountT <= termCountC);
- }
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(8);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_4bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(4);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_2bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(2);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_NoTrie() throws Exception {
- testRandomTrieAndClassicRangeQuery(Integer.MAX_VALUE);
- }
-
- private void testRangeSplit(int precisionStep) throws Exception {
- String field="ascfield"+precisionStep;
- // 10 random tests
- int num = TestUtil.nextInt(random(), 10, 20);
- for (int i =0; i< num; i++) {
- int lower=(int)(random().nextDouble()*noDocs - noDocs/2);
- int upper=(int)(random().nextDouble()*noDocs - noDocs/2);
- if (lower>upper) {
- int a=lower; lower=upper; upper=a;
- }
- // test inclusive range
- Query tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits );
- // test exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits );
- // test left exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits );
- // test right exclusive range
- tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits );
- }
- }
-
- @Test
- public void testRangeSplit_8bit() throws Exception {
- testRangeSplit(8);
- }
-
- @Test
- public void testRangeSplit_4bit() throws Exception {
- testRangeSplit(4);
- }
-
- @Test
- public void testRangeSplit_2bit() throws Exception {
- testRangeSplit(2);
- }
-
- /** we fake a float test using int2float conversion of LegacyNumericUtils */
- private void testFloatRange(int precisionStep) throws Exception {
- final String field="ascfield"+precisionStep;
- final int lower=-1000, upper=+2000;
-
- Query tq= LegacyNumericRangeQuery.newFloatRange(field, precisionStep,
- NumericUtils.sortableIntToFloat(lower), NumericUtils.sortableIntToFloat(upper), true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits );
- }
-
- @Test
- public void testFloatRange_8bit() throws Exception {
- testFloatRange(8);
- }
-
- @Test
- public void testFloatRange_4bit() throws Exception {
- testFloatRange(4);
- }
-
- @Test
- public void testFloatRange_2bit() throws Exception {
- testFloatRange(2);
- }
-
- @Test
- public void testEqualsAndHash() throws Exception {
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test1", 4, 10, 20, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test2", 4, 10, 20, false, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test3", 4, 10, 20, true, false));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test4", 4, 10, 20, false, false));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test5", 4, 10, null, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test6", 4, null, 20, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newIntRange("test7", 4, null, null, true, true));
- QueryUtils.checkEqual(
- LegacyNumericRangeQuery.newIntRange("test8", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newIntRange("test8", 4, 10, 20, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newIntRange("test9", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newIntRange("test9", 8, 10, 20, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newIntRange("test10a", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newIntRange("test10b", 4, 10, 20, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newIntRange("test11", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newIntRange("test11", 4, 20, 10, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newIntRange("test12", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newIntRange("test12", 4, 10, 20, false, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newIntRange("test13", 4, 10, 20, true, true),
- LegacyNumericRangeQuery.newFloatRange("test13", 4, 10f, 20f, true, true)
- );
- // the following produces a hash collision, because Long and Integer have the same hashcode, so only test equality:
- Query q1 = LegacyNumericRangeQuery.newIntRange("test14", 4, 10, 20, true, true);
- Query q2 = LegacyNumericRangeQuery.newLongRange("test14", 4, 10L, 20L, true, true);
- assertFalse(q1.equals(q2));
- assertFalse(q2.equals(q1));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/105c7eae/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java b/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java
deleted file mode 100644
index 7f63fbc..0000000
--- a/lucene/core/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java
+++ /dev/null
@@ -1,623 +0,0 @@
-/*
- * 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.search;
-
-
-import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.document.FieldType;
-import org.apache.lucene.document.LegacyDoubleField;
-import org.apache.lucene.document.LegacyLongField;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.MultiFields;
-import org.apache.lucene.index.RandomIndexWriter;
-import org.apache.lucene.index.Terms;
-import org.apache.lucene.index.TermsEnum;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.BytesRefBuilder;
-import org.apache.lucene.util.LegacyNumericUtils;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.NumericUtils;
-import org.apache.lucene.util.TestLegacyNumericUtils;
-import org.apache.lucene.util.TestUtil;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class TestNumericRangeQuery64 extends LuceneTestCase {
- // distance of entries
- private static long distance;
- // shift the starting of the values to the left, to also have negative values:
- private static final long startOffset = - 1L << 31;
- // number of docs to generate for testing
- private static int noDocs;
-
- private static Directory directory = null;
- private static IndexReader reader = null;
- private static IndexSearcher searcher = null;
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- noDocs = atLeast(4096);
- distance = (1L << 60) / noDocs;
- directory = newDirectory();
- RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
- newIndexWriterConfig(new MockAnalyzer(random()))
- .setMaxBufferedDocs(TestUtil.nextInt(random(), 100, 1000))
- .setMergePolicy(newLogMergePolicy()));
-
- final FieldType storedLong = new FieldType(LegacyLongField.TYPE_NOT_STORED);
- storedLong.setStored(true);
- storedLong.freeze();
-
- final FieldType storedLong8 = new FieldType(storedLong);
- storedLong8.setNumericPrecisionStep(8);
-
- final FieldType storedLong4 = new FieldType(storedLong);
- storedLong4.setNumericPrecisionStep(4);
-
- final FieldType storedLong6 = new FieldType(storedLong);
- storedLong6.setNumericPrecisionStep(6);
-
- final FieldType storedLong2 = new FieldType(storedLong);
- storedLong2.setNumericPrecisionStep(2);
-
- final FieldType storedLongNone = new FieldType(storedLong);
- storedLongNone.setNumericPrecisionStep(Integer.MAX_VALUE);
-
- final FieldType unstoredLong = LegacyLongField.TYPE_NOT_STORED;
-
- final FieldType unstoredLong8 = new FieldType(unstoredLong);
- unstoredLong8.setNumericPrecisionStep(8);
-
- final FieldType unstoredLong6 = new FieldType(unstoredLong);
- unstoredLong6.setNumericPrecisionStep(6);
-
- final FieldType unstoredLong4 = new FieldType(unstoredLong);
- unstoredLong4.setNumericPrecisionStep(4);
-
- final FieldType unstoredLong2 = new FieldType(unstoredLong);
- unstoredLong2.setNumericPrecisionStep(2);
-
- LegacyLongField
- field8 = new LegacyLongField("field8", 0L, storedLong8),
- field6 = new LegacyLongField("field6", 0L, storedLong6),
- field4 = new LegacyLongField("field4", 0L, storedLong4),
- field2 = new LegacyLongField("field2", 0L, storedLong2),
- fieldNoTrie = new LegacyLongField("field"+Integer.MAX_VALUE, 0L, storedLongNone),
- ascfield8 = new LegacyLongField("ascfield8", 0L, unstoredLong8),
- ascfield6 = new LegacyLongField("ascfield6", 0L, unstoredLong6),
- ascfield4 = new LegacyLongField("ascfield4", 0L, unstoredLong4),
- ascfield2 = new LegacyLongField("ascfield2", 0L, unstoredLong2);
-
- Document doc = new Document();
- // add fields, that have a distance to test general functionality
- doc.add(field8); doc.add(field6); doc.add(field4); doc.add(field2); doc.add(fieldNoTrie);
- // add ascending fields with a distance of 1, beginning at -noDocs/2 to test the correct splitting of range and inclusive/exclusive
- doc.add(ascfield8); doc.add(ascfield6); doc.add(ascfield4); doc.add(ascfield2);
-
- // Add a series of noDocs docs with increasing long values, by updating the fields
- for (int l=0; l<noDocs; l++) {
- long val=distance*l+startOffset;
- field8.setLongValue(val);
- field6.setLongValue(val);
- field4.setLongValue(val);
- field2.setLongValue(val);
- fieldNoTrie.setLongValue(val);
-
- val=l-(noDocs/2);
- ascfield8.setLongValue(val);
- ascfield6.setLongValue(val);
- ascfield4.setLongValue(val);
- ascfield2.setLongValue(val);
- writer.addDocument(doc);
- }
- reader = writer.getReader();
- searcher=newSearcher(reader);
- writer.close();
- }
-
- @AfterClass
- public static void afterClass() throws Exception {
- searcher = null;
- reader.close();
- reader = null;
- directory.close();
- directory = null;
- }
-
- @Override
- public void setUp() throws Exception {
- super.setUp();
- // set the theoretical maximum term count for 8bit (see docs for the number)
- // super.tearDown will restore the default
- BooleanQuery.setMaxClauseCount(7*255*2 + 255);
- }
-
- /** test for constant score + boolean query + filter, the other tests only use the constant score mode */
- private void testRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
- LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
- for (byte i=0; i<2; i++) {
- TopDocs topDocs;
- String type;
- switch (i) {
- case 0:
- type = " (constant score filter rewrite)";
- q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- break;
- case 1:
- type = " (constant score boolean rewrite)";
- q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- break;
- default:
- return;
- }
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count"+type, count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc"+type, 2*distance+startOffset, doc.getField(field).numericValue().longValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc"+type, (1+count)*distance+startOffset, doc.getField(field).numericValue().longValue() );
- }
- }
-
- @Test
- public void testRange_8bit() throws Exception {
- testRange(8);
- }
-
- @Test
- public void testRange_6bit() throws Exception {
- testRange(6);
- }
-
- @Test
- public void testRange_4bit() throws Exception {
- testRange(4);
- }
-
- @Test
- public void testRange_2bit() throws Exception {
- testRange(2);
- }
-
- @Test
- public void testOneMatchQuery() throws Exception {
- LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange("ascfield8", 8, 1000L, 1000L, true, true);
- TopDocs topDocs = searcher.search(q, noDocs);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", 1, sd.length );
- }
-
- private void testLeftOpenRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- long upper=(count-1)*distance + (distance/3) + startOffset;
- LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true);
- TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", startOffset, doc.getField(field).numericValue().longValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
-
- q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, false, true);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", count, sd.length );
- doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", startOffset, doc.getField(field).numericValue().longValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (count-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
- }
-
- @Test
- public void testLeftOpenRange_8bit() throws Exception {
- testLeftOpenRange(8);
- }
-
- @Test
- public void testLeftOpenRange_6bit() throws Exception {
- testLeftOpenRange(6);
- }
-
- @Test
- public void testLeftOpenRange_4bit() throws Exception {
- testLeftOpenRange(4);
- }
-
- @Test
- public void testLeftOpenRange_2bit() throws Exception {
- testLeftOpenRange(2);
- }
-
- private void testRightOpenRange(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int count=3000;
- long lower=(count-1)*distance + (distance/3) +startOffset;
- LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true);
- TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- ScoreDoc[] sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", noDocs-count, sd.length );
- Document doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().longValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
-
- q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, false);
- topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
- sd = topDocs.scoreDocs;
- assertNotNull(sd);
- assertEquals("Score doc count", noDocs-count, sd.length );
- doc=searcher.doc(sd[0].doc);
- assertEquals("First doc", count*distance+startOffset, doc.getField(field).numericValue().longValue() );
- doc=searcher.doc(sd[sd.length-1].doc);
- assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().longValue() );
- }
-
- @Test
- public void testRightOpenRange_8bit() throws Exception {
- testRightOpenRange(8);
- }
-
- @Test
- public void testRightOpenRange_6bit() throws Exception {
- testRightOpenRange(6);
- }
-
- @Test
- public void testRightOpenRange_4bit() throws Exception {
- testRightOpenRange(4);
- }
-
- @Test
- public void testRightOpenRange_2bit() throws Exception {
- testRightOpenRange(2);
- }
-
- @Test
- public void testInfiniteValues() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter writer = new RandomIndexWriter(random(), dir,
- newIndexWriterConfig(new MockAnalyzer(random())));
- Document doc = new Document();
- doc.add(new LegacyDoubleField("double", Double.NEGATIVE_INFINITY, Field.Store.NO));
- doc.add(new LegacyLongField("long", Long.MIN_VALUE, Field.Store.NO));
- writer.addDocument(doc);
-
- doc = new Document();
- doc.add(new LegacyDoubleField("double", Double.POSITIVE_INFINITY, Field.Store.NO));
- doc.add(new LegacyLongField("long", Long.MAX_VALUE, Field.Store.NO));
- writer.addDocument(doc);
-
- doc = new Document();
- doc.add(new LegacyDoubleField("double", 0.0, Field.Store.NO));
- doc.add(new LegacyLongField("long", 0L, Field.Store.NO));
- writer.addDocument(doc);
-
- for (double d : TestLegacyNumericUtils.DOUBLE_NANs) {
- doc = new Document();
- doc.add(new LegacyDoubleField("double", d, Field.Store.NO));
- writer.addDocument(doc);
- }
-
- writer.close();
-
- IndexReader r = DirectoryReader.open(dir);
- IndexSearcher s = newSearcher(r);
-
- Query q= LegacyNumericRangeQuery.newLongRange("long", null, null, true, true);
- TopDocs topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newLongRange("long", null, null, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newLongRange("long", Long.MIN_VALUE, Long.MAX_VALUE, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newLongRange("long", Long.MIN_VALUE, Long.MAX_VALUE, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newDoubleRange("double", null, null, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newDoubleRange("double", null, null, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newDoubleRange("double", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newDoubleRange("double", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, false, false);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
-
- q= LegacyNumericRangeQuery.newDoubleRange("double", Double.NaN, Double.NaN, true, true);
- topDocs = s.search(q, 10);
- assertEquals("Score doc count", TestLegacyNumericUtils.DOUBLE_NANs.length, topDocs.scoreDocs.length );
-
- r.close();
- dir.close();
- }
-
- private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
- String field="field"+precisionStep;
- int totalTermCountT=0,totalTermCountC=0,termCountT,termCountC;
- int num = TestUtil.nextInt(random(), 10, 20);
- for (int i = 0; i < num; i++) {
- long lower=(long)(random().nextDouble()*noDocs*distance)+startOffset;
- long upper=(long)(random().nextDouble()*noDocs*distance)+startOffset;
- if (lower>upper) {
- long a=lower; lower=upper; upper=a;
- }
- final BytesRef lowerBytes, upperBytes;
- BytesRefBuilder b = new BytesRefBuilder();
- LegacyNumericUtils.longToPrefixCoded(lower, 0, b);
- lowerBytes = b.toBytesRef();
- LegacyNumericUtils.longToPrefixCoded(upper, 0, b);
- upperBytes = b.toBytesRef();
-
- // test inclusive range
- LegacyNumericRangeQuery<Long> tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
- TermRangeQuery cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- TopDocs cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test left exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- // test right exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false);
- cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
- tTopDocs = searcher.search(tq, 1);
- cTopDocs = searcher.search(cq, 1);
- assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
- totalTermCountT += termCountT = countTerms(tq);
- totalTermCountC += termCountC = countTerms(cq);
- checkTermCounts(precisionStep, termCountT, termCountC);
- }
-
- checkTermCounts(precisionStep, totalTermCountT, totalTermCountC);
- if (VERBOSE && precisionStep != Integer.MAX_VALUE) {
- System.out.println("Average number of terms during random search on '" + field + "':");
- System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4)));
- System.out.println(" Classical query: " + (((double)totalTermCountC)/(num * 4)));
- }
- }
-
- @Test
- public void testEmptyEnums() throws Exception {
- int count=3000;
- long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
- // test empty enum
- assert lower < upper;
- assertTrue(0 < countTerms(LegacyNumericRangeQuery.newLongRange("field4", 4, lower, upper, true, true)));
- assertEquals(0, countTerms(LegacyNumericRangeQuery.newLongRange("field4", 4, upper, lower, true, true)));
- // test empty enum outside of bounds
- lower = distance*noDocs+startOffset;
- upper = 2L * lower;
- assert lower < upper;
- assertEquals(0, countTerms(LegacyNumericRangeQuery.newLongRange("field4", 4, lower, upper, true, true)));
- }
-
- private int countTerms(MultiTermQuery q) throws Exception {
- final Terms terms = MultiFields.getTerms(reader, q.getField());
- if (terms == null)
- return 0;
- final TermsEnum termEnum = q.getTermsEnum(terms);
- assertNotNull(termEnum);
- int count = 0;
- BytesRef cur, last = null;
- while ((cur = termEnum.next()) != null) {
- count++;
- if (last != null) {
- assertTrue(last.compareTo(cur) < 0);
- }
- last = BytesRef.deepCopyOf(cur);
- }
- // LUCENE-3314: the results after next() already returned null are undefined,
- // assertNull(termEnum.next());
- return count;
- }
-
- private void checkTermCounts(int precisionStep, int termCountT, int termCountC) {
- if (precisionStep == Integer.MAX_VALUE) {
- assertEquals("Number of terms should be equal for unlimited precStep", termCountC, termCountT);
- } else {
- assertTrue("Number of terms for NRQ should be <= compared to classical TRQ", termCountT <= termCountC);
- }
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(8);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_6bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(6);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_4bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(4);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_2bit() throws Exception {
- testRandomTrieAndClassicRangeQuery(2);
- }
-
- @Test
- public void testRandomTrieAndClassicRangeQuery_NoTrie() throws Exception {
- testRandomTrieAndClassicRangeQuery(Integer.MAX_VALUE);
- }
-
- private void testRangeSplit(int precisionStep) throws Exception {
- String field="ascfield"+precisionStep;
- // 10 random tests
- int num = TestUtil.nextInt(random(), 10, 20);
- for (int i = 0; i < num; i++) {
- long lower=(long)(random().nextDouble()*noDocs - noDocs/2);
- long upper=(long)(random().nextDouble()*noDocs - noDocs/2);
- if (lower>upper) {
- long a=lower; lower=upper; upper=a;
- }
- // test inclusive range
- Query tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits );
- // test exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits );
- // test left exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits );
- // test right exclusive range
- tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false);
- tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits );
- }
- }
-
- @Test
- public void testRangeSplit_8bit() throws Exception {
- testRangeSplit(8);
- }
-
- @Test
- public void testRangeSplit_6bit() throws Exception {
- testRangeSplit(6);
- }
-
- @Test
- public void testRangeSplit_4bit() throws Exception {
- testRangeSplit(4);
- }
-
- @Test
- public void testRangeSplit_2bit() throws Exception {
- testRangeSplit(2);
- }
-
- /** we fake a double test using long2double conversion of LegacyNumericUtils */
- private void testDoubleRange(int precisionStep) throws Exception {
- final String field="ascfield"+precisionStep;
- final long lower=-1000L, upper=+2000L;
-
- Query tq= LegacyNumericRangeQuery.newDoubleRange(field, precisionStep,
- NumericUtils.sortableLongToDouble(lower), NumericUtils.sortableLongToDouble(upper), true, true);
- TopDocs tTopDocs = searcher.search(tq, 1);
- assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits );
- }
-
- @Test
- public void testDoubleRange_8bit() throws Exception {
- testDoubleRange(8);
- }
-
- @Test
- public void testDoubleRange_6bit() throws Exception {
- testDoubleRange(6);
- }
-
- @Test
- public void testDoubleRange_4bit() throws Exception {
- testDoubleRange(4);
- }
-
- @Test
- public void testDoubleRange_2bit() throws Exception {
- testDoubleRange(2);
- }
-
- @Test
- public void testEqualsAndHash() throws Exception {
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test1", 4, 10L, 20L, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test2", 4, 10L, 20L, false, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test3", 4, 10L, 20L, true, false));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test4", 4, 10L, 20L, false, false));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test5", 4, 10L, null, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test6", 4, null, 20L, true, true));
- QueryUtils.checkHashEquals(LegacyNumericRangeQuery.newLongRange("test7", 4, null, null, true, true));
- QueryUtils.checkEqual(
- LegacyNumericRangeQuery.newLongRange("test8", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newLongRange("test8", 4, 10L, 20L, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newLongRange("test9", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newLongRange("test9", 8, 10L, 20L, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newLongRange("test10a", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newLongRange("test10b", 4, 10L, 20L, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newLongRange("test11", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newLongRange("test11", 4, 20L, 10L, true, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newLongRange("test12", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newLongRange("test12", 4, 10L, 20L, false, true)
- );
- QueryUtils.checkUnequal(
- LegacyNumericRangeQuery.newLongRange("test13", 4, 10L, 20L, true, true),
- LegacyNumericRangeQuery.newFloatRange("test13", 4, 10f, 20f, true, true)
- );
- // difference to int range is tested in TestNumericRangeQuery32
- }
-}