You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2015/01/02 13:02:33 UTC
svn commit: r1649007 [7/10] - in /lucene/dev/branches/lucene6005/lucene:
analysis/common/src/test/org/apache/lucene/analysis/core/
analysis/common/src/test/org/apache/lucene/collation/
benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/ benchm...
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestFloatFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestFloatFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestFloatFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestFloatFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,425 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.FieldDoc;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortedNumericSelector;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.HalfFloat;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.TestUtil;
+
+public class TestFloatFields extends LuceneTestCase {
+
+ public void testBasicRange() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 2f);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ // Make sure range query hits the right number of hits
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatRangeFilter("num", 0f, true, 3f, true), 1).totalHits);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatDocValuesRangeFilter("num", 0f, true, 3f, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatRangeFilter("num", 0f, true, 10f, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatDocValuesRangeFilter("num", 0f, true, 10f, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatRangeFilter("num", 1f, true,2.5f, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatDocValuesRangeFilter("num", 1f, true,2.5f, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandom() throws Exception {
+ int iters = atLeast(10000);
+ for(int iter=0;iter<iters;iter++) {
+ float v = random().nextFloat();
+ int x = NumericUtils.floatToInt(v);
+ float v2 = NumericUtils.intToFloat(x);
+ assertEquals(v, v2, 0.0f);
+ }
+ }
+
+ public void testNaN() throws Exception {
+ assertEquals(Float.NaN, NumericUtils.intToFloat(NumericUtils.floatToInt(Float.NaN)), 0.0f);
+ }
+
+ public void testPositiveInfinity() throws Exception {
+ assertEquals(Float.POSITIVE_INFINITY, NumericUtils.intToFloat(NumericUtils.floatToInt(Float.POSITIVE_INFINITY)), 0.0f);
+ }
+
+ public void testNegativeInfinity() throws Exception {
+ assertEquals(Float.NEGATIVE_INFINITY, NumericUtils.intToFloat(NumericUtils.floatToInt(Float.NEGATIVE_INFINITY)), 0.0f);
+ }
+
+ public void testBasicSort() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 2f);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingFirst() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingFirst("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingLast() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingLast("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("one", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("two", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandomRangeAndSort() throws Exception {
+ Directory dir = newDirectory();
+ int numDocs = atLeast(100);
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ List<Float> values = new ArrayList<>();
+ for(int i=0;i<numDocs;i++) {
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", i);
+ Float num = random().nextFloat();
+ values.add(num);
+ doc.addFloat("num", num);
+ w.addDocument(doc);
+ if (VERBOSE) {
+ System.out.println("TEST: id=" + i + " num=" + num);
+ }
+ }
+
+ IndexReader r = w.getReader();
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ int iters = atLeast(1000);
+ for(int iter=0;iter<iters;iter++) {
+ float x = random().nextFloat();
+ float y = random().nextFloat();
+
+ float min, max;
+ if (x < y) {
+ min = x;
+ max = y;
+ } else {
+ min = y;
+ max = x;
+ }
+ Set<Integer> expected = new HashSet<>();
+ for(int i=0;i<values.size();i++) {
+ float value = values.get(i).floatValue();
+ if (value >= min && value <= max) {
+ expected.add(i);
+ }
+ }
+ if (VERBOSE) {
+ System.out.println("TEST: iter " + iter + " count=" + expected.size() + " min=" + min + " max=" + max);
+ for(int value : expected) {
+ System.out.println(" " + value);
+ }
+ }
+
+ Set<Integer> actual = new HashSet<>();
+ Filter filter;
+ if (random().nextBoolean()) {
+ filter = fieldTypes.newFloatRangeFilter("num", min, true, max, true);
+ } else {
+ filter = fieldTypes.newFloatDocValuesRangeFilter("num", min, true, max, true);
+ }
+
+ boolean reversed = random().nextBoolean();
+ Sort sort = fieldTypes.newSort("num", reversed);
+ if (VERBOSE) {
+ System.out.println("TEST: filter=" + filter + " reversed=" + reversed + " sort=" + sort);
+ }
+ TopDocs hits = s.search(new MatchAllDocsQuery(), filter, numDocs, sort);
+ Float last = null;
+ boolean wrongValues = false;
+ for(ScoreDoc hit : hits.scoreDocs) {
+ Document doc = s.doc(hit.doc);
+ actual.add(doc.getInt("id"));
+ Float v = doc.getFloat("num");
+ if (v.equals(((FieldDoc) hit).fields[0]) == false) {
+ System.out.println(" wrong: " + v + " vs " + ((FieldDoc) hit).fields[0]);
+ wrongValues = true;
+ }
+ if (VERBOSE) {
+ System.out.println(" hit doc=" + doc);
+ }
+ if (last != null) {
+ int cmp = last.compareTo(v);
+ assertTrue((reversed && cmp >= 0) || (reversed == false && cmp <= 0));
+ }
+ last = v;
+ }
+ assertEquals(expected, actual);
+ assertFalse(wrongValues);
+ }
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedSort() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addFloat("num", 45F);
+ doc.addFloat("num", -22F);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addFloat("num", -2F);
+ doc.addFloat("num", 14F);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+
+ // Default selector is MIN
+ assertEquals(0, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(2, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ fieldTypes.setMultiValuedNumericSortSelector("num", SortedNumericSelector.Type.MAX);
+ hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+ assertEquals(2, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(0, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedRange() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addFloat("num", 45F);
+ doc.addFloat("num", -22F);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addFloat("num", -2F);
+ doc.addFloat("num", 14F);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatRangeFilter("num", -100F, true, 100F, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newFloatRangeFilter("num", 40F, true, 45F, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testTermQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addFloat("num", 180f);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ FieldTypes fieldTypes = s.getFieldTypes();
+ assertEquals(1, s.search(fieldTypes.newFloatTermQuery("num", 180f), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testJustStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredFloat("num", 180f);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ doc = s.doc(0);
+ assertEquals(180f, doc.getFloat("num"), 0.0f);
+ r.close();
+ w.close();
+ }
+
+ public void testExcIndexedThenStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addFloat("num", 100f);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addStoredFloat("num", 200f),
+ "field \"num\": cannot addStored: field was already added non-stored");
+ w.close();
+ }
+
+ public void testExcStoredThenIndexed() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredFloat("num", 100f);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addFloat("num", 200f),
+ "field \"num\": this field is only stored; use addStoredXXX instead");
+ w.close();
+ }
+}
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestHalfFloatFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestHalfFloatFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestHalfFloatFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestHalfFloatFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,418 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.FieldDoc;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortedNumericSelector;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.HalfFloat;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.TestUtil;
+
+public class TestHalfFloatFields extends LuceneTestCase {
+
+ // We can use constant IOTA because all our random floats are 0.0 - 1.0:
+ static float IOTA = .0005F;
+
+ public void testBasicRange() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addHalfFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 2f);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ // Make sure range query hits the right number of hits
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatRangeFilter("num", 0f, true, 3f, true), 1).totalHits);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatDocValuesRangeFilter("num", 0f, true, 3f, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatRangeFilter("num", 0f, true, 10f, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatDocValuesRangeFilter("num", 0f, true, 10f, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatRangeFilter("num", 1f, true,2.5f, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatDocValuesRangeFilter("num", 1f, true,2.5f, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandom() throws Exception {
+ int iters = atLeast(10000);
+ for(int iter=0;iter<iters;iter++) {
+ float v = random().nextFloat();
+ short x = NumericUtils.halfFloatToShort(v);
+ float v2 = NumericUtils.shortToHalfFloat(x);
+ assertEquals(v, v2, IOTA);
+ }
+ }
+
+ public void testNaN() throws Exception {
+ assertEquals(Float.NaN, NumericUtils.shortToHalfFloat(NumericUtils.halfFloatToShort(Float.NaN)), 0.0f);
+ }
+
+ public void testPositiveInfinity() throws Exception {
+ assertEquals(Float.POSITIVE_INFINITY, NumericUtils.shortToHalfFloat(NumericUtils.halfFloatToShort(Float.POSITIVE_INFINITY)), 0.0f);
+ }
+
+ public void testNegativeInfinity() throws Exception {
+ assertEquals(Float.NEGATIVE_INFINITY, NumericUtils.shortToHalfFloat(NumericUtils.halfFloatToShort(Float.NEGATIVE_INFINITY)), 0.0f);
+ }
+
+ public void testBasicSort() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addHalfFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 2f);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingFirst() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addHalfFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingFirst("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingLast() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addHalfFloat("num", 3f);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addHalfFloat("num", 7f);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingLast("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("one", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("two", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandomRangeAndSort() throws Exception {
+ Directory dir = newDirectory();
+ int numDocs = atLeast(100);
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ List<Float> values = new ArrayList<>();
+ for(int i=0;i<numDocs;i++) {
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", i);
+ Float num = random().nextFloat();
+ values.add(num);
+ doc.addHalfFloat("num", num);
+ w.addDocument(doc);
+ if (VERBOSE) {
+ System.out.println("TEST: id=" + i + " num=" + num);
+ }
+ }
+
+ IndexReader r = w.getReader();
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ int iters = atLeast(1000);
+ for(int iter=0;iter<iters;iter++) {
+ float x = random().nextFloat();
+ float y = random().nextFloat();
+
+ float min, max;
+ if (x < y) {
+ min = x;
+ max = y;
+ } else {
+ min = y;
+ max = x;
+ }
+ Set<Integer> expected = new HashSet<>();
+ for(int i=0;i<values.size();i++) {
+ float value = values.get(i).floatValue();
+ if (value >= min && value <= max) {
+ expected.add(i);
+ }
+ }
+ if (VERBOSE) {
+ System.out.println("TEST: iter " + iter + " count=" + expected.size() + " min=" + min + " max=" + max);
+ for(int value : expected) {
+ System.out.println(" " + value);
+ }
+ }
+
+ Set<Integer> actual = new HashSet<>();
+ Filter filter = fieldTypes.newHalfFloatRangeFilter("num", min, true, max, true);
+
+ boolean reversed = random().nextBoolean();
+ Sort sort = fieldTypes.newSort("num", reversed);
+ if (VERBOSE) {
+ System.out.println("TEST: filter=" + filter + " reversed=" + reversed + " sort=" + sort);
+ }
+ TopDocs hits = s.search(new MatchAllDocsQuery(), filter, numDocs, sort);
+ Float last = null;
+ boolean wrongValues = false;
+ for(ScoreDoc hit : hits.scoreDocs) {
+ Document doc = s.doc(hit.doc);
+ actual.add(doc.getInt("id"));
+ Float v = doc.getHalfFloat("num");
+ if (isClose(v, (Float) ((FieldDoc) hit).fields[0]) == false) {
+ System.out.println(" wrong: " + v + " vs " + ((FieldDoc) hit).fields[0]);
+ wrongValues = true;
+ }
+ if (VERBOSE) {
+ System.out.println(" hit doc=" + doc);
+ }
+ if (last != null) {
+ int cmp;
+ if (isClose(last, v)) {
+ cmp = 0;
+ } else {
+ cmp = last.compareTo(v);
+ }
+ assertTrue((reversed && cmp >= 0) || (reversed == false && cmp <= 0));
+ }
+ last = v;
+ }
+
+ for (Integer id : expected) {
+ if (actual.contains(id) == false) {
+ float value = values.get(id);
+ assertTrue("extra expected: value=" + value + " vs min=" + min + " max=" + max, isClose(value, min) || isClose(value, max));
+ }
+ }
+
+ for (Integer id : actual) {
+ if (expected.contains(id) == false) {
+ float value = values.get(id);
+ assertTrue("extra actual: value=" + value + " vs min=" + min + " max=" + max, isClose(value, min) || isClose(value, max));
+ }
+ }
+
+ assertFalse(wrongValues);
+ }
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ static boolean isClose(float v1, float v2) {
+ return Math.abs(v1-v2) < IOTA;
+ }
+
+ public void testMultiValuedSort() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addHalfFloat("num", 45F);
+ doc.addHalfFloat("num", -22F);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addHalfFloat("num", -2F);
+ doc.addHalfFloat("num", 14F);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+
+ // Default selector is MIN:
+ assertEquals(0, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(2, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ fieldTypes.setMultiValuedNumericSortSelector("num", SortedNumericSelector.Type.MAX);
+ hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+ assertEquals(2, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(0, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedRange() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addHalfFloat("num", 45F);
+ doc.addHalfFloat("num", -22F);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addHalfFloat("num", -2F);
+ doc.addHalfFloat("num", 14F);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatRangeFilter("num", -100F, true, 100F, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newHalfFloatRangeFilter("num", 40F, true, 45F, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testExcIndexedThenStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addHalfFloat("num", 100f);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addStoredHalfFloat("num", 200f),
+ "field \"num\": cannot addStored: field was already added non-stored");
+ w.close();
+ }
+
+ public void testExcStoredThenIndexed() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredHalfFloat("num", 100f);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addHalfFloat("num", 200f),
+ "field \"num\": this field is only stored; use addStoredXXX instead");
+ w.close();
+ }
+}
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestInetAddressFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestInetAddressFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestInetAddressFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestInetAddressFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,173 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.net.InetAddress;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestInetAddressFields extends LuceneTestCase {
+
+ public void testInetAddressSort() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+
+ Document doc = w.newDocument();
+ InetAddress inet0 = InetAddress.getByName("10.17.4.10");
+ doc.addInetAddress("inet", inet0);
+ doc.addAtom("id", "0");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ InetAddress inet1 = InetAddress.getByName("10.17.4.22");
+ doc.addInetAddress("inet", inet1);
+ doc.addAtom("id", "1");
+ w.addDocument(doc);
+
+ DirectoryReader r = DirectoryReader.open(w, true);
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("inet"));
+ assertEquals(2, hits.totalHits);
+ Document hit = s.doc(hits.scoreDocs[0].doc);
+ assertEquals("0", hit.getString("id"));
+ assertEquals(inet0, hit.getInetAddress("inet"));
+ hit = s.doc(hits.scoreDocs[1].doc);
+ assertEquals("1", hit.getString("id"));
+ assertEquals(inet1, hit.getInetAddress("inet"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testInetAddressRangeFilter() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+
+ Document doc = w.newDocument();
+ InetAddress inet0 = InetAddress.getByName("10.17.4.10");
+ doc.addInetAddress("inet", inet0);
+ doc.addAtom("id", "0");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ InetAddress inet1 = InetAddress.getByName("10.17.4.22");
+ doc.addInetAddress("inet", inet1);
+ doc.addAtom("id", "1");
+ w.addDocument(doc);
+
+ DirectoryReader r = DirectoryReader.open(w, true);
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, false), 1).totalHits);
+ assertEquals(0, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, false, inet1, false), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", InetAddress.getByName("10.17.0.0"), true, InetAddress.getByName("10.17.4.20"), false), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testInetV6AddressRangeFilter() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+
+ Document doc = w.newDocument();
+ InetAddress inet0 = InetAddress.getByName("1080:0:0:0:8:700:200C:417A");
+ doc.addInetAddress("inet", inet0);
+ doc.addAtom("id", "0");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ InetAddress inet1 = InetAddress.getByName("1080:0:0:0:8:800:200C:417A");
+ doc.addInetAddress("inet", inet1);
+ doc.addAtom("id", "1");
+ w.addDocument(doc);
+
+ DirectoryReader r = DirectoryReader.open(w, true);
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, false), 1).totalHits);
+ assertEquals(0, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, false, inet1, false), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", InetAddress.getByName("1080:0:0:0:0:0:0:0"), true, InetAddress.getByName("1080:0:0:0:8:750:0:0"), false), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMixed() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+
+ Document doc = w.newDocument();
+ InetAddress inet0 = InetAddress.getByName("10.17.5.22");
+ doc.addInetAddress("inet", inet0);
+ doc.addAtom("id", "0");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ InetAddress inet1 = InetAddress.getByName("1080:0:0:0:8:800:200C:417A");
+ doc.addInetAddress("inet", inet1);
+ doc.addAtom("id", "1");
+ w.addDocument(doc);
+
+ DirectoryReader r = DirectoryReader.open(w, true);
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, true, inet1, false), 1).totalHits);
+ assertEquals(0, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", inet0, false, inet1, false), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newRangeFilter("inet", InetAddress.getByName("10.17.5.0"), true, InetAddress.getByName("1080:0:0:0:8:750:0:0"), false), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testExcIndexedThenStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addInetAddress("num", InetAddress.getByName("10.17.5.22"));
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addStoredInetAddress("num", InetAddress.getByName("10.17.5.22")),
+ "field \"num\": cannot addStored: field was already added non-stored");
+ w.close();
+ }
+
+ public void testExcStoredThenIndexed() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredInetAddress("num", InetAddress.getByName("10.17.5.22"));
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addInetAddress("num", InetAddress.getByName("10.17.5.22")),
+ "field \"num\": this field is only stored; use addStoredXXX instead");
+ w.close();
+ }
+}
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestIntFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestIntFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestIntFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestIntFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,407 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.FieldDoc;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortedNumericSelector;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.HalfFloat;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.TestUtil;
+
+public class TestIntFields extends LuceneTestCase {
+
+ public void testBasicRange() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addInt("num", 3);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 2);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 7);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ // Make sure range query hits the right number of hits
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newIntRangeFilter("num", 0, true, 3, true), 1).totalHits);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newIntDocValuesRangeFilter("num", 0, true, 3, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newIntRangeFilter("num", 0, true, 10, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newIntDocValuesRangeFilter("num", 0, true, 10, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testBasicSort() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addInt("num", 3);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 2);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 7);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingFirst() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addInt("num", 3);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 7);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingFirst("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingLast() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addInt("num", 3);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addInt("num", 7);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingLast("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("one", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("two", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandomRangeAndSort() throws Exception {
+ Directory dir = newDirectory();
+ int numDocs = atLeast(100);
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ List<Integer> values = new ArrayList<>();
+ for(int i=0;i<numDocs;i++) {
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", i);
+ Integer num = random().nextInt();
+ values.add(num);
+ doc.addInt("num", num);
+ w.addDocument(doc);
+ if (VERBOSE) {
+ System.out.println("TEST: id=" + i + " num=" + num);
+ }
+ }
+
+ IndexReader r = w.getReader();
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ int iters = atLeast(1000);
+ for(int iter=0;iter<iters;iter++) {
+ int x = random().nextInt();
+ int y = random().nextInt();
+
+ int min, max;
+ if (x < y) {
+ min = x;
+ max = y;
+ } else {
+ min = y;
+ max = x;
+ }
+ Set<Integer> expected = new HashSet<>();
+ for(int i=0;i<values.size();i++) {
+ float value = values.get(i).floatValue();
+ if (value >= min && value <= max) {
+ expected.add(i);
+ }
+ }
+ if (VERBOSE) {
+ System.out.println("TEST: iter " + iter + " count=" + expected.size() + " min=" + min + " max=" + max);
+ for(int value : expected) {
+ System.out.println(" " + value);
+ }
+ }
+
+ Set<Integer> actual = new HashSet<>();
+ Filter filter;
+ if (random().nextBoolean()) {
+ filter = fieldTypes.newIntRangeFilter("num", min, true, max, true);
+ } else {
+ filter = fieldTypes.newIntDocValuesRangeFilter("num", min, true, max, true);
+ }
+
+ boolean reversed = random().nextBoolean();
+ Sort sort = fieldTypes.newSort("num", reversed);
+ if (VERBOSE) {
+ System.out.println("TEST: filter=" + filter + " reversed=" + reversed + " sort=" + sort);
+ }
+ TopDocs hits = s.search(new MatchAllDocsQuery(), filter, numDocs, sort);
+ Integer last = null;
+ boolean wrongValues = false;
+ for(ScoreDoc hit : hits.scoreDocs) {
+ Document doc = s.doc(hit.doc);
+ actual.add(doc.getInt("id"));
+ Integer v = doc.getInt("num");
+ if (v.intValue() != ((Integer) ((FieldDoc) hit).fields[0]).intValue()) {
+ System.out.println(" wrong: " + v + " vs " + ((FieldDoc) hit).fields[0]);
+ wrongValues = true;
+ }
+ if (VERBOSE) {
+ System.out.println(" hit doc=" + doc);
+ }
+ if (last != null) {
+ int cmp;
+ if (v.equals(last) == false) {
+ cmp = 0;
+ } else {
+ cmp = last.compareTo(v);
+ }
+ assertTrue((reversed && cmp >= 0) || (reversed == false && cmp <= 0));
+ }
+ last = v;
+ }
+
+ assertEquals(expected, actual);
+ assertFalse(wrongValues);
+ }
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedSort() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addInt("num", 45);
+ doc.addInt("num", -22);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addInt("num", -2);
+ doc.addInt("num", 14);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+
+ // Default selector is MIN:
+ assertEquals(0, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(2, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ fieldTypes.setMultiValuedNumericSortSelector("num", SortedNumericSelector.Type.MAX);
+ hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+ assertEquals(2, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(0, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedRange() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addInt("num", 45);
+ doc.addInt("num", -22);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addInt("num", -2);
+ doc.addInt("num", 14);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newIntRangeFilter("num", -100, true, 100, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newIntRangeFilter("num", 40, true, 45, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testTermQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addInt("num", 180);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ FieldTypes fieldTypes = s.getFieldTypes();
+ assertEquals(1, s.search(fieldTypes.newIntTermQuery("num", 180), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testJustStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredInt("num", 180);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ doc = s.doc(0);
+ assertEquals(180, doc.getInt("num").intValue());
+ r.close();
+ w.close();
+ }
+
+ public void testExcIndexedThenStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addInt("num", 100);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addStoredInt("num", 200),
+ "field \"num\": cannot addStored: field was already added non-stored");
+ w.close();
+ }
+
+ public void testExcStoredThenIndexed() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredInt("num", 100);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addInt("num", 200),
+ "field \"num\": this field is only stored; use addStoredXXX instead");
+ w.close();
+ }
+}
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestLongFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestLongFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestLongFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestLongFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,407 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.FieldDoc;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortedNumericSelector;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.HalfFloat;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.TestUtil;
+
+public class TestLongFields extends LuceneTestCase {
+
+ public void testBasicRange() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addLong("num", 3l);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 2l);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 7l);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ // Make sure range query hits the right number of hits
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newLongRangeFilter("num", 0l, true, 3l, true), 1).totalHits);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newLongDocValuesRangeFilter("num", 0l, true, 3l, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newLongRangeFilter("num", 0l, true, 10l, true), 1).totalHits);
+ assertEquals(3, s.search(new MatchAllDocsQuery(), fieldTypes.newLongDocValuesRangeFilter("num", 0l, true, 10l, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testBasicSort() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ //System.out.println("id type: " + fieldTypes.getFieldType("id"));
+
+ Document doc = w.newDocument();
+ doc.addLong("num", 3l);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 2l);
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 7l);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingFirst() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addLong("num", 3l);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 7l);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingFirst("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("two", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("one", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testSortMissingLast() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+
+ Document doc = w.newDocument();
+ doc.addLong("num", 3l);
+ doc.addAtom("id", "one");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addAtom("id", "two");
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addLong("num", 7l);
+ doc.addAtom("id", "three");
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ fieldTypes = s.getFieldTypes();
+ fieldTypes.setSortMissingLast("num");
+
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 3, fieldTypes.newSort("num"));
+ assertEquals(3, hits.totalHits);
+ assertEquals("one", r.document(hits.scoreDocs[0].doc).getString("id"));
+ assertEquals("three", r.document(hits.scoreDocs[1].doc).getString("id"));
+ assertEquals("two", r.document(hits.scoreDocs[2].doc).getString("id"));
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testRandomRangeAndSort() throws Exception {
+ Directory dir = newDirectory();
+ int numDocs = atLeast(100);
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ List<Long> values = new ArrayList<>();
+ for(int i=0;i<numDocs;i++) {
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", i);
+ Long num = random().nextLong();
+ values.add(num);
+ doc.addLong("num", num);
+ w.addDocument(doc);
+ if (VERBOSE) {
+ System.out.println("TEST: id=" + i + " num=" + num);
+ }
+ }
+
+ IndexReader r = w.getReader();
+ FieldTypes fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ int iters = atLeast(1000);
+ for(int iter=0;iter<iters;iter++) {
+ long x = random().nextLong();
+ long y = random().nextLong();
+
+ long min, max;
+ if (x < y) {
+ min = x;
+ max = y;
+ } else {
+ min = y;
+ max = x;
+ }
+ Set<Integer> expected = new HashSet<>();
+ for(int i=0;i<values.size();i++) {
+ float value = values.get(i).floatValue();
+ if (value >= min && value <= max) {
+ expected.add(i);
+ }
+ }
+ if (VERBOSE) {
+ System.out.println("TEST: iter " + iter + " count=" + expected.size() + " min=" + min + " max=" + max);
+ for(int value : expected) {
+ System.out.println(" " + value);
+ }
+ }
+
+ Set<Integer> actual = new HashSet<>();
+ Filter filter;
+ if (random().nextBoolean()) {
+ filter = fieldTypes.newLongRangeFilter("num", min, true, max, true);
+ } else {
+ filter = fieldTypes.newLongDocValuesRangeFilter("num", min, true, max, true);
+ }
+
+ boolean reversed = random().nextBoolean();
+ Sort sort = fieldTypes.newSort("num", reversed);
+ if (VERBOSE) {
+ System.out.println("TEST: filter=" + filter + " reversed=" + reversed + " sort=" + sort);
+ }
+ TopDocs hits = s.search(new MatchAllDocsQuery(), filter, numDocs, sort);
+ Long last = null;
+ boolean wrongValues = false;
+ for(ScoreDoc hit : hits.scoreDocs) {
+ Document doc = s.doc(hit.doc);
+ actual.add(doc.getInt("id"));
+ Long v = doc.getLong("num");
+ if (v.longValue() != ((Long) ((FieldDoc) hit).fields[0]).longValue()) {
+ System.out.println(" wrong: " + v + " vs " + ((FieldDoc) hit).fields[0]);
+ wrongValues = true;
+ }
+ if (VERBOSE) {
+ System.out.println(" hit doc=" + doc);
+ }
+ if (last != null) {
+ int cmp;
+ if (v.equals(last) == false) {
+ cmp = 0;
+ } else {
+ cmp = last.compareTo(v);
+ }
+ assertTrue((reversed && cmp >= 0) || (reversed == false && cmp <= 0));
+ }
+ last = v;
+ }
+
+ assertEquals(expected, actual);
+ assertFalse(wrongValues);
+ }
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedSort() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addLong("num", 45l);
+ doc.addLong("num", -22l);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addLong("num", -2l);
+ doc.addLong("num", 14l);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ TopDocs hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+
+ // Default selector is MIN:
+ assertEquals(0, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(2, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ fieldTypes.setMultiValuedNumericSortSelector("num", SortedNumericSelector.Type.MAX);
+ hits = s.search(new MatchAllDocsQuery(), 10, fieldTypes.newSort("num"));
+ assertEquals(2, s.doc(hits.scoreDocs[0].doc).get("id"));
+ assertEquals(0, s.doc(hits.scoreDocs[1].doc).get("id"));
+ assertEquals(1, s.doc(hits.scoreDocs[2].doc).get("id"));
+
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testMultiValuedRange() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = newRandomIndexWriter(dir);
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setMultiValued("num");
+
+ Document doc = w.newDocument();
+ doc.addUniqueInt("id", 0);
+ doc.addLong("num", 45l);
+ doc.addLong("num", -22l);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 1);
+ w.addDocument(doc);
+
+ doc = w.newDocument();
+ doc.addUniqueInt("id", 2);
+ doc.addLong("num", -2l);
+ doc.addLong("num", 14l);
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ fieldTypes = r.getFieldTypes();
+
+ IndexSearcher s = newSearcher(r);
+ assertEquals(2, s.search(new MatchAllDocsQuery(), fieldTypes.newLongRangeFilter("num", -100l, true, 100l, true), 1).totalHits);
+ assertEquals(1, s.search(new MatchAllDocsQuery(), fieldTypes.newLongRangeFilter("num", 40l, true, 45l, true), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testTermQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addLong("num", 180l);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ FieldTypes fieldTypes = s.getFieldTypes();
+ assertEquals(1, s.search(fieldTypes.newLongTermQuery("num", 180l), 1).totalHits);
+ r.close();
+ w.close();
+ dir.close();
+ }
+
+ public void testJustStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredLong("num", 180l);
+ w.addDocument(doc);
+ DirectoryReader r = DirectoryReader.open(w, true);
+ IndexSearcher s = newSearcher(r);
+ doc = s.doc(0);
+ assertEquals(180l, doc.getLong("num").longValue());
+ r.close();
+ w.close();
+ }
+
+ public void testExcIndexedThenStored() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addLong("num", 100);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addStoredLong("num", 200),
+ "field \"num\": cannot addStored: field was already added non-stored");
+ w.close();
+ }
+
+ public void testExcStoredThenIndexed() throws Exception {
+ IndexWriter w = newIndexWriter(dir);
+ Document doc = w.newDocument();
+ doc.addStoredLong("num", 100L);
+ w.addDocument(doc);
+ final Document doc2 = w.newDocument();
+ shouldFail(() -> doc2.addLong("num", 200L),
+ "field \"num\": this field is only stored; use addStoredXXX instead");
+ w.close();
+ }
+}
Added: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestNumericFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestNumericFields.java?rev=1649007&view=auto
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestNumericFields.java (added)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/document/TestNumericFields.java Fri Jan 2 12:02:31 2015
@@ -0,0 +1,89 @@
+package org.apache.lucene.document;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.DocValuesType;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.MultiDocValues;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.ConstantScoreQuery;
+import org.apache.lucene.search.FieldDoc;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.NumericUtils;
+
+public class TestNumericFields extends LuceneTestCase {
+
+ public void testSortedNumericDocValues() throws Exception {
+ Directory dir = newDirectory();
+
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
+ FieldTypes fieldTypes = w.getFieldTypes();
+ fieldTypes.setDocValuesType("sortednumeric", DocValuesType.SORTED_NUMERIC);
+ fieldTypes.setMultiValued("sortednumeric");
+
+ Document doc = w.newDocument();
+ doc.addInt("sortednumeric", 3);
+ doc.addInt("sortednumeric", 1);
+ doc.addInt("sortednumeric", 2);
+ w.addDocument(doc);
+ IndexReader r = DirectoryReader.open(w, true);
+ SortedNumericDocValues sndv = MultiDocValues.getSortedNumericValues(r, "sortednumeric");
+ sndv.setDocument(0);
+
+ assertEquals(3, sndv.count());
+ assertEquals(1, sndv.valueAt(0));
+ assertEquals(2, sndv.valueAt(1));
+ assertEquals(3, sndv.valueAt(2));
+ w.close();
+ r.close();
+ dir.close();
+ }
+
+ // Cannot change a field from INT to DOUBLE
+ public void testInvalidNumberTypeChange() throws Exception {
+ IndexWriter w = newIndexWriter();
+ Document doc = w.newDocument();
+ doc.addInt("int", 3);
+ shouldFail(() -> doc.addDouble("int", 4d),
+ "field \"int\": cannot change from value type INT to DOUBLE");
+ w.close();
+ }
+}
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/Test4GBStoredFields.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/Test4GBStoredFields.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/Test4GBStoredFields.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/Test4GBStoredFields.java Fri Jan 2 12:02:31 2015
@@ -61,7 +61,7 @@ public class Test4GBStoredFields extends
// random so that even compressing codecs can't compress it
value[i] = (byte) random().nextInt(256);
}
- doc.addStored("fld", value);
+ doc.addStoredBinary("fld", value);
final int numDocs = (int) ((1L << 32) / valueLength + 100);
for (int i = 0; i < numDocs; ++i) {
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestAbuseSchema.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestAbuseSchema.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestAbuseSchema.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestAbuseSchema.java Fri Jan 2 12:02:31 2015
@@ -883,4 +883,201 @@ public class TestAbuseSchema extends Luc
dir.close();
}
+
+ public void testMixedTypesAfterReopenAppend1() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer a = new MockAnalyzer(random());
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(a));
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(a, "foo", 0, IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.NUMERIC);
+ doc.add(field);
+ w.addDocument(doc);
+ w.close();
+
+ w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+ doc = new ArrayList<>();
+ field = new LowSchemaField(a, "foo", new BytesRef("hello"), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.SORTED);
+ doc.add(field);
+ try {
+ w.addDocument(doc);
+ fail("did not get expected exception");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ w.close();
+ dir.close();
+ }
+
+ public void testMixedTypesAfterReopenAppend2() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer a = new MockAnalyzer(random());
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(a));
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(a, "foo", new BytesRef("foo"), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.SORTED_SET);
+ doc.add(field);
+ w.addDocument(doc);
+ w.close();
+
+ w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+ doc = new ArrayList<>();
+ field = new LowSchemaField(a, "foo", "bar", IndexOptions.DOCS, false);
+ doc.add(field);
+ field = new LowSchemaField(a, "foo", new BytesRef("foo"), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.BINARY);
+ doc.add(field);
+ try {
+ w.addDocument(doc);
+ fail("did not get expected exception");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ w.close();
+ dir.close();
+ }
+
+ public void testMixedTypesAfterReopenAppend3() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer a = new MockAnalyzer(random());
+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(a));
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(a, "foo", new BytesRef("foo"), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.SORTED_SET);
+ doc.add(field);
+ w.addDocument(doc);
+ w.close();
+
+ w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
+ doc = new ArrayList<>();
+ field = new LowSchemaField(a, "foo", "bar", IndexOptions.DOCS, false);
+ doc.add(field);
+ field = new LowSchemaField(a, "foo", new BytesRef("foo"), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.BINARY);
+ doc.add(field);
+ try {
+ w.addDocument(doc);
+ fail("did not get expected exception");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ // Also add another document so there is a segment to write here:
+ w.addDocument(w.newDocument());
+ w.close();
+ dir.close();
+ }
+
+ public void testUntokenizedReader() throws Exception {
+ IndexWriter w = newIndexWriter();
+ List<LowSchemaField> doc = new ArrayList<>();
+ doc.add(new LowSchemaField(new MockAnalyzer(random()), "field", new StringReader("string"), IndexOptions.DOCS, false));
+ shouldFail(() -> w.addDocument(doc),
+ "field \"field\" is stored but does not have binaryValue, stringValue nor numericValue");
+ w.close();
+ }
+
+ public void testUpdateNumericDVFieldWithSameNameAsPostingField() throws Exception {
+ // this used to fail because FieldInfos.Builder neglected to update
+ // globalFieldMaps.docValuesTypes map
+ Analyzer a = new MockAnalyzer(random());
+ IndexWriterConfig conf = newIndexWriterConfig(a);
+ IndexWriter writer = new IndexWriter(dir, conf);
+
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(a, "f", "mock-value", IndexOptions.DOCS, false);
+ doc.add(field);
+
+ field = new LowSchemaField(a, "f", 5, IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.NUMERIC);
+ doc.add(field);
+ writer.addDocument(doc);
+
+ writer.commit();
+ writer.updateNumericDocValue(new Term("f", "mock-value"), "f", 17L);
+ writer.close();
+
+ DirectoryReader r = DirectoryReader.open(dir);
+ NumericDocValues ndv = r.leaves().get(0).reader().getNumericDocValues("f");
+ assertEquals(17, ndv.get(0));
+ r.close();
+ }
+
+ static BytesRef toBytes(long value) {
+ BytesRef bytes = new BytesRef(10); // negative longs may take 10 bytes
+ while ((value & ~0x7FL) != 0L) {
+ bytes.bytes[bytes.length++] = (byte) ((value & 0x7FL) | 0x80L);
+ value >>>= 7;
+ }
+ bytes.bytes[bytes.length++] = (byte) value;
+ return bytes;
+ }
+
+ static long getValue(BinaryDocValues bdv, int idx) {
+ BytesRef term = bdv.get(idx);
+ idx = term.offset;
+ byte b = term.bytes[idx++];
+ long value = b & 0x7FL;
+ for (int shift = 7; (b & 0x80L) != 0; shift += 7) {
+ b = term.bytes[idx++];
+ value |= (b & 0x7FL) << shift;
+ }
+ return value;
+ }
+
+ public void testUpdateBinaryDVFieldWithSameNameAsPostingField() throws Exception {
+ // this used to fail because FieldInfos.Builder neglected to update
+ // globalFieldMaps.docValuesTypes map
+ Analyzer a = new MockAnalyzer(random());
+ IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
+ IndexWriter writer = new IndexWriter(dir, conf);
+
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(a, "f", "mock-value", IndexOptions.DOCS, false);
+ doc.add(field);
+
+ field = new LowSchemaField(a, "f", toBytes(5L), IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.BINARY);
+ doc.add(field);
+
+ writer.addDocument(doc);
+ writer.commit();
+ writer.updateBinaryDocValue(new Term("f", "mock-value"), "f", toBytes(17L));
+ writer.close();
+
+ DirectoryReader r = DirectoryReader.open(dir);
+ BinaryDocValues bdv = r.leaves().get(0).reader().getBinaryDocValues("f");
+ assertEquals(17, getValue(bdv, 0));
+ r.close();
+ }
+
+ public void testHasUncommittedChangesAfterException() throws IOException {
+ Analyzer analyzer = new MockAnalyzer(random());
+
+ Directory directory = newDirectory();
+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!
+ IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
+ iwc.setMergePolicy(newLogMergePolicy());
+ IndexWriter iwriter = new IndexWriter(directory, iwc);
+ List<LowSchemaField> doc = new ArrayList<>();
+ LowSchemaField field = new LowSchemaField(analyzer, "dv", "foo!", IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.SORTED);
+ doc.add(field);
+
+ field = new LowSchemaField(analyzer, "dv", "bar!", IndexOptions.NONE, false);
+ field.setDocValuesType(DocValuesType.SORTED);
+ doc.add(field);
+
+ try {
+ iwriter.addDocument(doc);
+ fail("didn't hit expected exception");
+ } catch (IllegalArgumentException expected) {
+ // expected
+ }
+ iwriter.commit();
+ assertFalse(iwriter.hasUncommittedChanges());
+ iwriter.close();
+ directory.close();
+ }
+
}
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryDocValuesUpdates.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryDocValuesUpdates.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryDocValuesUpdates.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryDocValuesUpdates.java Fri Jan 2 12:02:31 2015
@@ -22,7 +22,6 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil;
-import org.junit.Ignore;
import org.junit.Test;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
@@ -763,8 +762,6 @@ public class TestBinaryDocValuesUpdates
IOUtils.close(reader, dir);
}
- // nocommit fixme LUCENE-6062
- @Ignore
public void testUpdateSegmentWithNoDocValues() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
@@ -821,8 +818,6 @@ public class TestBinaryDocValuesUpdates
dir.close();
}
- // nocommit fixme LUCENE-6062
- @Ignore
public void testUpdateSegmentWithPostingButNoDocValues() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
@@ -865,32 +860,6 @@ public class TestBinaryDocValuesUpdates
dir.close();
}
-
- public void testUpdateBinaryDVFieldWithSameNameAsPostingField() throws Exception {
- // this used to fail because FieldInfos.Builder neglected to update
- // globalFieldMaps.docValuesTypes map
- Directory dir = newDirectory();
- IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
- IndexWriter writer = new IndexWriter(dir, conf);
- FieldTypes fieldTypes = writer.getFieldTypes();
- fieldTypes.disableSorting("f");
-
- // nocommit use low schema API here:
- Document doc = writer.newDocument();
- doc.addAtom("fmock", "mock-value");
- doc.addBinary("f", toBytes(5L));
- writer.addDocument(doc);
- writer.commit();
- writer.updateBinaryDocValue(new Term("fmock", "mock-value"), "f", toBytes(17L));
- writer.close();
-
- DirectoryReader r = DirectoryReader.open(dir);
- BinaryDocValues bdv = r.leaves().get(0).reader().getBinaryDocValues("f");
- assertEquals(17, getValue(bdv, 0));
- r.close();
-
- dir.close();
- }
public void testStressMultiThreading() throws Exception {
final Directory dir = newDirectory();
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java Fri Jan 2 12:02:31 2015
@@ -42,7 +42,7 @@ public class TestBinaryTerms extends Luc
bytes.bytes[1] = (byte) (255 - i);
bytes.length = 2;
Document doc = iw.newDocument();
- doc.addStored("id", "" + i);
+ doc.addStoredString("id", "" + i);
doc.addLargeText("bytes", tokenStream);
iw.addDocument(doc);
}
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestConsistentFieldNumbers.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestConsistentFieldNumbers.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestConsistentFieldNumbers.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestConsistentFieldNumbers.java Fri Jan 2 12:02:31 2015
@@ -108,7 +108,7 @@ public class TestConsistentFieldNumbers
.setMergePolicy(NoMergePolicy.INSTANCE));
Document d = writer.newDocument();
d.addLargeText("f1", "d2 first field");
- d.addStored("f3", new byte[] { 1, 2, 3 });
+ d.addStoredBinary("f3", new byte[] { 1, 2, 3 });
writer.addDocument(d);
writer.close();
SegmentInfos sis = SegmentInfos.readLatestCommit(dir);
@@ -128,7 +128,7 @@ public class TestConsistentFieldNumbers
Document d = writer.newDocument();
d.addLargeText("f1", "d3 first field");
d.addLargeText("f2", "d3 second field");
- d.addStored("f3", new byte[] { 1, 2, 3, 4, 5 });
+ d.addStoredBinary("f3", new byte[] { 1, 2, 3, 4, 5 });
writer.addDocument(d);
writer.close();
SegmentInfos sis = SegmentInfos.readLatestCommit(dir);
@@ -230,7 +230,7 @@ public class TestConsistentFieldNumbers
int mode = number % 16;
switch (mode) {
case 0:
- d.addStored(fieldName, "some text");
+ d.addStoredString(fieldName, "some text");
return;
case 1:
d.addLargeText(fieldName, "some text");
Modified: lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestCustomNorms.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestCustomNorms.java?rev=1649007&r1=1649006&r2=1649007&view=diff
==============================================================================
--- lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestCustomNorms.java (original)
+++ lucene/dev/branches/lucene6005/lucene/core/src/test/org/apache/lucene/index/TestCustomNorms.java Fri Jan 2 12:02:31 2015
@@ -20,6 +20,7 @@ import java.io.IOException;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.FieldTypes;
import org.apache.lucene.search.CollectionStatistics;
import org.apache.lucene.search.TermStatistics;
import org.apache.lucene.search.similarities.DefaultSimilarity;
@@ -50,6 +51,43 @@ public class TestCustomNorms extends Luc
final LineFileDocs docs = new LineFileDocs(writer.w, random());
int num = atLeast(100);
for (int i = 0; i < num; i++) {
+ Document doc = docs.nextDoc();
+ float nextFloat = random().nextFloat();
+ doc.addLargeText(floatTestField, "" + nextFloat, nextFloat);
+ writer.addDocument(doc);
+ if (rarely()) {
+ writer.commit();
+ }
+ }
+ writer.commit();
+ writer.close();
+ LeafReader open = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(dir));
+ NumericDocValues norms = open.getNormValues(floatTestField);
+ assertNotNull(norms);
+ for (int i = 0; i < open.maxDoc(); i++) {
+ Document document = open.document(i);
+ float expected = Float.parseFloat(document.getString(floatTestField));
+ assertEquals(expected, Float.intBitsToFloat((int)norms.get(i)), 0.0f);
+ }
+ open.close();
+ dir.close();
+ docs.close();
+ }
+
+ // Use FieldTypes.setSimilarity:
+ public void testPerFieldFloatNorms() throws IOException {
+
+ Directory dir = newDirectory();
+ MockAnalyzer analyzer = new MockAnalyzer(random());
+ analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
+
+ IndexWriterConfig config = newIndexWriterConfig(analyzer);
+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir, config);
+ FieldTypes fieldTypes = writer.getFieldTypes();
+ fieldTypes.setSimilarity(floatTestField, new MySimProvider());
+ final LineFileDocs docs = new LineFileDocs(writer.w, random());
+ int num = atLeast(100);
+ for (int i = 0; i < num; i++) {
Document doc = docs.nextDoc();
float nextFloat = random().nextFloat();
doc.addLargeText(floatTestField, "" + nextFloat, nextFloat);