~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/search/TestLocaleMethods.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search;
2
 
 
3
 
import java.io.IOException;
4
 
import java.text.Collator;
5
 
import java.util.Locale;
6
 
 
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;
17
 
 
18
 
/**
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
25
 
 *
26
 
 *     http://www.apache.org/licenses/LICENSE-2.0
27
 
 *
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.
33
 
 */
34
 
 
35
 
/**
36
 
 * Tests Locale-based sort and range search
37
 
 */
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;
45
 
 
46
 
  @BeforeClass
47
 
  public static void beforeClass() throws Exception {
48
 
    locale = LuceneTestCase.randomLocale(random);
49
 
    collator = Collator.getInstance(locale);
50
 
    numDocs = 1000 * RANDOM_MULTIPLIER;
51
 
    dir = newDirectory();
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);
57
 
      doc.add(field);
58
 
      iw.addDocument(doc);
59
 
    }
60
 
    reader = iw.getReader();
61
 
    iw.close();
62
 
 
63
 
    searcher = newSearcher(reader);
64
 
  }
65
 
  
66
 
  @AfterClass
67
 
  public static void afterClass() throws Exception {
68
 
    searcher.close();
69
 
    reader.close();
70
 
    dir.close();
71
 
    locale = null;
72
 
    collator = null;
73
 
    searcher = null;
74
 
    reader = null;
75
 
    dir = null;
76
 
  }
77
 
  
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));
81
 
    String prev = "";
82
 
    for (ScoreDoc doc : docs.scoreDocs) {
83
 
      String value = reader.document(doc.doc).get("field");
84
 
      assertTrue(collator.compare(value, prev) >= 0);
85
 
      prev = value;
86
 
    }
87
 
  }
88
 
  
89
 
  public void testSort2() throws Exception {
90
 
    SortField sf = new SortField("field", new FieldComparatorSource() {
91
 
      @Override
92
 
      public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException {
93
 
        return new FieldComparator.StringComparatorLocale(numHits, fieldname, locale);
94
 
      }
95
 
    });
96
 
    TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), null, numDocs, new Sort(sf));
97
 
    String prev = "";
98
 
    for (ScoreDoc doc : docs.scoreDocs) {
99
 
      String value = reader.document(doc.doc).get("field");
100
 
      assertTrue(collator.compare(value, prev) >= 0);
101
 
      prev = value;
102
 
    }
103
 
  }
104
 
  
105
 
  private void doTestRanges(String startPoint, String endPoint, Query query) throws Exception {
106
 
    // positive test
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);
112
 
    }
113
 
    
114
 
    // negative test
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);
122
 
    }
123
 
  }
124
 
  
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);
132
 
    }
133
 
  }
134
 
  
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);
142
 
    }
143
 
  }
144
 
}