1 package org.apache.lucene.search;
3 import java.io.IOException;
4 import java.text.Collator;
5 import java.util.Locale;
7 import org.apache.lucene.document.Document;
8 import org.apache.lucene.document.Field;
9 import org.apache.lucene.index.IndexReader;
10 import org.apache.lucene.index.RandomIndexWriter;
11 import org.apache.lucene.search.BooleanClause.Occur;
12 import org.apache.lucene.store.Directory;
13 import org.apache.lucene.util.LuceneTestCase;
14 import org.apache.lucene.util._TestUtil;
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
19 * Licensed to the Apache Software Foundation (ASF) under one or more
20 * contributor license agreements. See the NOTICE file distributed with
21 * this work for additional information regarding copyright ownership.
22 * The ASF licenses this file to You under the Apache License, Version 2.0
23 * (the "License"); you may not use this file except in compliance with
24 * the License. You may obtain a copy of the License at
26 * http://www.apache.org/licenses/LICENSE-2.0
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
36 * Tests Locale-based sort and range search
38 public class TestLocaleMethods extends LuceneTestCase {
39 private static Locale locale;
40 private static Collator collator;
41 private static IndexSearcher searcher;
42 private static IndexReader reader;
43 private static Directory dir;
44 private static int numDocs;
47 public static void beforeClass() throws Exception {
48 locale = LuceneTestCase.randomLocale(random);
49 collator = Collator.getInstance(locale);
50 numDocs = 1000 * RANDOM_MULTIPLIER;
52 RandomIndexWriter iw = new RandomIndexWriter(random, dir);
53 for (int i = 0; i < numDocs; i++) {
54 Document doc = new Document();
55 String value = _TestUtil.randomUnicodeString(random);
56 Field field = newField("field", value, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
60 reader = iw.getReader();
63 searcher = newSearcher(reader);
67 public static void afterClass() throws Exception {
78 public void testSort() throws Exception {
79 SortField sf = new SortField("field", locale);
80 TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), null, numDocs, new Sort(sf));
82 for (ScoreDoc doc : docs.scoreDocs) {
83 String value = reader.document(doc.doc).get("field");
84 assertTrue(collator.compare(value, prev) >= 0);
89 public void testSort2() throws Exception {
90 SortField sf = new SortField("field", new FieldComparatorSource() {
92 public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException {
93 return new FieldComparator.StringComparatorLocale(numHits, fieldname, locale);
96 TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), null, numDocs, new Sort(sf));
98 for (ScoreDoc doc : docs.scoreDocs) {
99 String value = reader.document(doc.doc).get("field");
100 assertTrue(collator.compare(value, prev) >= 0);
105 private void doTestRanges(String startPoint, String endPoint, Query query) throws Exception {
107 TopDocs docs = searcher.search(query, numDocs);
108 for (ScoreDoc doc : docs.scoreDocs) {
109 String value = reader.document(doc.doc).get("field");
110 assertTrue(collator.compare(value, startPoint) >= 0);
111 assertTrue(collator.compare(value, endPoint) <= 0);
115 BooleanQuery bq = new BooleanQuery();
116 bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
117 bq.add(query, Occur.MUST_NOT);
118 docs = searcher.search(bq, numDocs);
119 for (ScoreDoc doc : docs.scoreDocs) {
120 String value = reader.document(doc.doc).get("field");
121 assertTrue(collator.compare(value, startPoint) < 0 || collator.compare(value, endPoint) > 0);
125 public void testRangeQuery() throws Exception {
126 int numQueries = 100*RANDOM_MULTIPLIER;
127 for (int i = 0; i < numQueries; i++) {
128 String startPoint = _TestUtil.randomUnicodeString(random);
129 String endPoint = _TestUtil.randomUnicodeString(random);
130 Query query = new TermRangeQuery("field", startPoint, endPoint, true, true, collator);
131 doTestRanges(startPoint, endPoint, query);
135 public void testRangeFilter() throws Exception {
136 int numQueries = 100*RANDOM_MULTIPLIER;
137 for (int i = 0; i < numQueries; i++) {
138 String startPoint = _TestUtil.randomUnicodeString(random);
139 String endPoint = _TestUtil.randomUnicodeString(random);
140 Query query = new ConstantScoreQuery(new TermRangeFilter("field", startPoint, endPoint, true, true, collator));
141 doTestRanges(startPoint, endPoint, query);