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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.request;
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;
29
* @version $Id: TestFaceting.java 1150416 2011-07-24 15:37:54Z rmuir $
31
public class TestFaceting extends SolrTestCaseJ4 {
33
public static void beforeClass() throws Exception {
34
initCore("solrconfig.xml","schema11.xml");
38
public void setUp() throws Exception {
45
public void tearDown() throws Exception {
51
return String.format(Locale.US, "%08d", tnum);
54
void createIndex(int nTerms) {
56
for (int i=0; i<nTerms; i++) {
57
assertU(adoc("id", Float.toString(i), proto.field(), t(i) ));
59
assertU(optimize()); // squeeze out any possible deleted docs
62
Term proto = new Term("field_s","");
63
SolrQueryRequest req; // used to get a searcher
65
if (req!=null) req.close();
69
void doTermEnum(int size) throws Exception {
72
req = lrf.makeRequest("q","*:*");
74
TermIndex ti = new TermIndex(proto.field());
75
NumberedTermEnum te = ti.getEnumerator(req.getSearcher().getReader());
77
// iterate through first
78
while(te.term() != null) te.next();
79
assertEquals(size, te.getTermNumber());
82
te = ti.getEnumerator(req.getSearcher().getReader());
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);
89
boolean b = te.skipTo(proto.createTerm(s));
90
assertEquals(b, rnum < size);
92
assertEquals(rnum, te.pos);
93
assertEquals(s, te.term().text());
95
assertEquals(null, te.term());
96
assertEquals(size, te.getTermNumber());
100
// test seeking before term
101
assertEquals(size>0, te.skipTo(proto.createTerm("000")));
102
assertEquals(0, te.getTermNumber());
104
assertEquals(t(0), te.term().text());
106
assertEquals(null, te.term());
110
// test seeking by term number
111
for (int i=0; i<size*2+10; i++) {
112
int rnum = r.nextInt(size);
114
boolean b = te.skipTo(rnum);
115
assertEquals(true, b);
116
assertEquals(rnum, te.pos);
117
assertEquals(s, te.term().text());
123
public void testTermEnum() throws Exception {
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);
134
public void testFacets() throws Exception {
135
StringBuilder sb = new StringBuilder();
137
// go over 4096 to test some of the buffer resizing
138
for (int i=0; i<5000; i++) {
143
assertU(adoc("id", "1", "many_ws", sb.toString()));
146
assertQ("check many tokens",
147
req("q", "id:1","indent","true"
148
,"facet", "true", "facet.method","fc"
149
,"facet.field", "many_ws"
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']"
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"
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']"
202
public void testRegularBig() throws Exception {
203
StringBuilder sb = new StringBuilder();
205
// go over 4096 to test some of the buffer resizing
207
for (int i=0; i<nTerms; i++) {
211
String many_ws = sb.toString();
215
// int iter=65536+10;
217
int commitInterval=iter/9;
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) {
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"
234
,"facet.mincount", "1"
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']"
243
assertQ("check many tokens",
244
req("q", "id:"+t(i),"indent","true"
245
,"facet", "true", "facet.method","fc"
246
,"facet.field", "many_ws"
248
,"facet.mincount", "1"
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']"
b'\\ No newline at end of file'