add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestMultiValuedNumericRangeQuery.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.util.Locale;
21 import java.text.DecimalFormat;
22 import java.text.DecimalFormatSymbols;
23
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;
33
34 public class TestMultiValuedNumericRangeQuery extends LuceneTestCase {
35
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.
40    */
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)));
46     
47     DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.US));
48     
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));
56       }
57       writer.addDocument(doc);
58     }
59     IndexReader reader = writer.getReader();
60     writer.close();
61     
62     IndexSearcher searcher=newSearcher(reader);
63     num = atLeast(50);
64     for (int i = 0; i < num; i++) {
65       int lower=random.nextInt(Integer.MAX_VALUE);
66       int upper=random.nextInt(Integer.MAX_VALUE);
67       if (lower>upper) {
68         int a=lower; lower=upper; upper=a;
69       }
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 );
75     }
76     searcher.close();
77     reader.close();
78     directory.close();
79   }
80   
81 }