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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/request/TestFaceting.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.request;
19
 
 
20
 
import org.apache.lucene.index.Term;
21
 
import org.apache.solr.SolrTestCaseJ4;
22
 
import org.junit.After;
23
 
import org.junit.BeforeClass;
24
 
import org.junit.Test;
25
 
import java.util.Locale;
26
 
import java.util.Random;
27
 
 
28
 
/**
29
 
 * @version $Id: TestFaceting.java 1150416 2011-07-24 15:37:54Z rmuir $
30
 
 */
31
 
public class TestFaceting extends SolrTestCaseJ4 {
32
 
  @BeforeClass
33
 
  public static void beforeClass() throws Exception {
34
 
    initCore("solrconfig.xml","schema11.xml");
35
 
  }
36
 
 
37
 
  @Override
38
 
  public void setUp() throws Exception {
39
 
    super.setUp();
40
 
    clearIndex();
41
 
  }
42
 
 
43
 
  @After
44
 
  @Override
45
 
  public void tearDown() throws Exception {
46
 
    close();
47
 
    super.tearDown();
48
 
  }
49
 
 
50
 
  String t(int tnum) {
51
 
    return String.format(Locale.US, "%08d", tnum);
52
 
  }
53
 
  
54
 
  void createIndex(int nTerms) {
55
 
    assertU(delQ("*:*"));
56
 
    for (int i=0; i<nTerms; i++) {
57
 
      assertU(adoc("id", Float.toString(i), proto.field(), t(i) ));
58
 
    }
59
 
    assertU(optimize()); // squeeze out any possible deleted docs
60
 
  }
61
 
 
62
 
  Term proto = new Term("field_s","");
63
 
  SolrQueryRequest req; // used to get a searcher
64
 
  void close() {
65
 
    if (req!=null) req.close();
66
 
    req = null;
67
 
  }
68
 
 
69
 
  void doTermEnum(int size) throws Exception {
70
 
    close();
71
 
    createIndex(size);
72
 
    req = lrf.makeRequest("q","*:*");
73
 
 
74
 
    TermIndex ti = new TermIndex(proto.field());
75
 
    NumberedTermEnum te = ti.getEnumerator(req.getSearcher().getReader());
76
 
 
77
 
    // iterate through first
78
 
    while(te.term() != null) te.next();
79
 
    assertEquals(size, te.getTermNumber());
80
 
    te.close();
81
 
 
82
 
    te = ti.getEnumerator(req.getSearcher().getReader());
83
 
 
84
 
    Random r = new Random(size);
85
 
    // test seeking by term string
86
 
    for (int i=0; i<size*2+10; i++) {
87
 
      int rnum = r.nextInt(size+2);
88
 
      String s = t(rnum);
89
 
      boolean b = te.skipTo(proto.createTerm(s));
90
 
      assertEquals(b, rnum < size);
91
 
      if (rnum < size) {
92
 
        assertEquals(rnum, te.pos);
93
 
        assertEquals(s, te.term().text());
94
 
      } else {
95
 
        assertEquals(null, te.term());
96
 
        assertEquals(size, te.getTermNumber());
97
 
      }
98
 
    }
99
 
 
100
 
    // test seeking before term
101
 
    assertEquals(size>0, te.skipTo(proto.createTerm("000")));
102
 
    assertEquals(0, te.getTermNumber());
103
 
    if (size>0) {
104
 
      assertEquals(t(0), te.term().text());
105
 
    } else {
106
 
      assertEquals(null, te.term());
107
 
    }
108
 
 
109
 
    if (size>0) {
110
 
      // test seeking by term number
111
 
      for (int i=0; i<size*2+10; i++) {
112
 
        int rnum = r.nextInt(size);
113
 
        String s = t(rnum);
114
 
        boolean b = te.skipTo(rnum);
115
 
        assertEquals(true, b);
116
 
        assertEquals(rnum, te.pos);
117
 
        assertEquals(s, te.term().text());
118
 
      }
119
 
    }
120
 
  }
121
 
 
122
 
  @Test
123
 
  public void testTermEnum() throws Exception {
124
 
    doTermEnum(0);
125
 
    doTermEnum(1);
126
 
    doTermEnum(TermIndex.interval - 1);  // test boundaries around the block size
127
 
    doTermEnum(TermIndex.interval);
128
 
    doTermEnum(TermIndex.interval + 1);
129
 
    doTermEnum(TermIndex.interval * 2 + 2);    
130
 
    // doTermEnum(TermIndex.interval * 3 + 3);    
131
 
  }
132
 
 
133
 
  @Test
134
 
  public void testFacets() throws Exception {
135
 
    StringBuilder sb = new StringBuilder();
136
 
 
137
 
    // go over 4096 to test some of the buffer resizing
138
 
    for (int i=0; i<5000; i++) {
139
 
      sb.append(t(i));
140
 
      sb.append(' ');     
141
 
    }
142
 
 
143
 
    assertU(adoc("id", "1", "many_ws", sb.toString()));
144
 
    assertU(commit());
145
 
 
146
 
    assertQ("check many tokens",
147
 
            req("q", "id:1","indent","true"
148
 
                ,"facet", "true", "facet.method","fc"
149
 
                ,"facet.field", "many_ws"
150
 
                ,"facet.limit", "-1"
151
 
                )
152
 
            ,"*[count(//lst[@name='many_ws']/int)=5000]"
153
 
            ,"//lst[@name='many_ws']/int[@name='" + t(0) + "'][.='1']"
154
 
            ,"//lst[@name='many_ws']/int[@name='" + t(1) + "'][.='1']"
155
 
            ,"//lst[@name='many_ws']/int[@name='" + t(2) + "'][.='1']"
156
 
            ,"//lst[@name='many_ws']/int[@name='" + t(3) + "'][.='1']"
157
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4) + "'][.='1']"
158
 
            ,"//lst[@name='many_ws']/int[@name='" + t(5) + "'][.='1']"
159
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4092) + "'][.='1']"
160
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4093) + "'][.='1']"
161
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4094) + "'][.='1']"
162
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4095) + "'][.='1']"
163
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4096) + "'][.='1']"
164
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4097) + "'][.='1']"
165
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4098) + "'][.='1']"
166
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4090) + "'][.='1']"
167
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4999) + "'][.='1']"
168
 
            );
169
 
 
170
 
    // test gaps that take more than one byte
171
 
    sb = new StringBuilder();
172
 
    sb.append(t(0)).append(' ');
173
 
    sb.append(t(150)).append(' ');
174
 
    sb.append(t(301)).append(' ');
175
 
    sb.append(t(453)).append(' ');
176
 
    sb.append(t(606)).append(' ');
177
 
    sb.append(t(1000)).append(' ');
178
 
    sb.append(t(2010)).append(' ');
179
 
    sb.append(t(3050)).append(' ');
180
 
    sb.append(t(4999)).append(' ');
181
 
    assertU(adoc("id", "2", "many_ws", sb.toString()));
182
 
    assertQ("check many tokens",
183
 
            req("q", "id:1","indent","true"
184
 
                ,"facet", "true", "facet.method","fc"
185
 
                ,"facet.field", "many_ws"
186
 
                ,"facet.limit", "-1"
187
 
                )
188
 
            ,"*[count(//lst[@name='many_ws']/int)=5000]"
189
 
            ,"//lst[@name='many_ws']/int[@name='" + t(0) + "'][.='1']"
190
 
            ,"//lst[@name='many_ws']/int[@name='" + t(150) + "'][.='1']"
191
 
            ,"//lst[@name='many_ws']/int[@name='" + t(301) + "'][.='1']"
192
 
            ,"//lst[@name='many_ws']/int[@name='" + t(453) + "'][.='1']"
193
 
            ,"//lst[@name='many_ws']/int[@name='" + t(606) + "'][.='1']"
194
 
            ,"//lst[@name='many_ws']/int[@name='" + t(1000) + "'][.='1']"
195
 
            ,"//lst[@name='many_ws']/int[@name='" + t(2010) + "'][.='1']"
196
 
            ,"//lst[@name='many_ws']/int[@name='" + t(3050) + "'][.='1']"
197
 
            ,"//lst[@name='many_ws']/int[@name='" + t(4999) + "'][.='1']"
198
 
              );
199
 
  }
200
 
 
201
 
  @Test
202
 
  public void testRegularBig() throws Exception {
203
 
    StringBuilder sb = new StringBuilder();
204
 
 
205
 
    // go over 4096 to test some of the buffer resizing
206
 
    int nTerms=7;
207
 
    for (int i=0; i<nTerms; i++) {
208
 
      sb.append(t(i));
209
 
      sb.append(' ');
210
 
    }
211
 
    String many_ws = sb.toString();
212
 
 
213
 
    int i1=1000000;
214
 
 
215
 
    // int iter=65536+10;
216
 
    int iter=1000;
217
 
    int commitInterval=iter/9;
218
 
 
219
 
    for (int i=0; i<iter; i++) {
220
 
      // assertU(adoc("id", t(i), "many_ws", many_ws + t(i1+i) + " " + t(i1*2+i)));
221
 
      assertU(adoc("id", t(i), "many_ws", t(i1+i) + " " + t(i1*2+i)));
222
 
      if (iter % commitInterval == 0) {
223
 
        assertU(commit());
224
 
      }
225
 
    }
226
 
    assertU(commit());
227
 
 
228
 
    for (int i=0; i<iter; i+=iter/10) {
229
 
    assertQ("check many tokens",
230
 
            req("q", "id:"+t(i),"indent","true"
231
 
                ,"facet", "true", "facet.method","fc"
232
 
                ,"facet.field", "many_ws"
233
 
                ,"facet.limit", "-1"
234
 
                ,"facet.mincount", "1"
235
 
                )
236
 
            ,"*[count(//lst[@name='many_ws']/int)=" + 2 + "]"
237
 
            ,"//lst[@name='many_ws']/int[@name='" + t(i1+i) + "'][.='1']"
238
 
            ,"//lst[@name='many_ws']/int[@name='" + t(i1*2+i) + "'][.='1']"
239
 
            );
240
 
    }
241
 
 
242
 
    int i=iter-1;
243
 
    assertQ("check many tokens",
244
 
            req("q", "id:"+t(i),"indent","true"
245
 
                ,"facet", "true", "facet.method","fc"
246
 
                ,"facet.field", "many_ws"
247
 
                ,"facet.limit", "-1"
248
 
                ,"facet.mincount", "1"
249
 
 
250
 
                )
251
 
            ,"*[count(//lst[@name='many_ws']/int)=" + 2 + "]"
252
 
            ,"//lst[@name='many_ws']/int[@name='" + t(i1+i) + "'][.='1']"
253
 
            ,"//lst[@name='many_ws']/int[@name='" + t(i1*2+i) + "'][.='1']"
254
 
            );
255
 
  }
256
 
 
257
 
 
258
 
}
 
 
b'\\ No newline at end of file'