1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.Locale;
21 import java.text.DecimalFormat;
22 import java.text.DecimalFormatSymbols;
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.document.NumericField;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.RandomIndexWriter;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32 import org.apache.lucene.util._TestUtil;
34 public class TestMultiValuedNumericRangeQuery extends LuceneTestCase {
36 /** Tests NumericRangeQuery on a multi-valued field (multiple numeric values per document).
37 * This test ensures, that a classical TermRangeQuery returns exactly the same document numbers as
38 * NumericRangeQuery (see SOLR-1322 for discussion) and the multiple precision terms per numeric value
39 * do not interfere with multiple numeric values.
41 public void testMultiValuedNRQ() throws Exception {
42 Directory directory = newDirectory();
43 RandomIndexWriter writer = new RandomIndexWriter(random, directory,
44 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
45 .setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
47 DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.US));
49 int num = atLeast(500);
50 for (int l = 0; l < num; l++) {
51 Document doc = new Document();
52 for (int m=0, c=random.nextInt(10); m<=c; m++) {
53 int value = random.nextInt(Integer.MAX_VALUE);
54 doc.add(newField("asc", format.format(value), Field.Store.NO, Field.Index.NOT_ANALYZED));
55 doc.add(new NumericField("trie", Field.Store.NO, true).setIntValue(value));
57 writer.addDocument(doc);
59 IndexReader reader = writer.getReader();
62 IndexSearcher searcher=newSearcher(reader);
64 for (int i = 0; i < num; i++) {
65 int lower=random.nextInt(Integer.MAX_VALUE);
66 int upper=random.nextInt(Integer.MAX_VALUE);
68 int a=lower; lower=upper; upper=a;
70 TermRangeQuery cq=new TermRangeQuery("asc", format.format(lower), format.format(upper), true, true);
71 NumericRangeQuery<Integer> tq=NumericRangeQuery.newIntRange("trie", lower, upper, true, true);
72 TopDocs trTopDocs = searcher.search(cq, 1);
73 TopDocs nrTopDocs = searcher.search(tq, 1);
74 assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", trTopDocs.totalHits, nrTopDocs.totalHits );