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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/search/TestQueryTypes.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
 
package org.apache.solr.search;
18
 
 
19
 
import org.apache.solr.util.AbstractSolrTestCase;
20
 
 
21
 
public class TestQueryTypes extends AbstractSolrTestCase {
22
 
 
23
 
  @Override
24
 
  public String getSchemaFile() { return "schema11.xml"; }
25
 
  @Override
26
 
  public String getSolrConfigFile() { return "solrconfig.xml"; }
27
 
  public String getCoreName() { return "basic"; }
28
 
 
29
 
 
30
 
  @Override
31
 
  public void setUp() throws Exception {
32
 
    // if you override setUp or tearDown, you better call
33
 
    // the super classes version
34
 
    super.setUp();
35
 
  }
36
 
  @Override
37
 
  public void tearDown() throws Exception {
38
 
    // if you override setUp or tearDown, you better call
39
 
    // the super classes version
40
 
    super.tearDown();
41
 
  }
42
 
 
43
 
 
44
 
  public void testQueryTypes() {
45
 
    assertU(adoc("id","1", "v_t","Hello Dude"));
46
 
    assertU(adoc("id","2", "v_t","Hello Yonik"));
47
 
    assertU(adoc("id","3", "v_s","{!literal}"));
48
 
    assertU(adoc("id","4", "v_s","other stuff"));
49
 
    assertU(adoc("id","5", "v_f","3.14159"));
50
 
    assertU(adoc("id","6", "v_f","8983"));
51
 
    assertU(adoc("id","7", "v_f","1.5"));
52
 
    assertU(adoc("id","8", "v_ti","5"));
53
 
    assertU(adoc("id","9", "v_s","internal\"quote"));
54
 
 
55
 
    Object[] arr = new Object[] {
56
 
    "id",999.0
57
 
    ,"v_s","wow dude"
58
 
    ,"v_t","wow"
59
 
    ,"v_ti",-1
60
 
    ,"v_tis",-1
61
 
    ,"v_tl",-1234567891234567890L
62
 
    ,"v_tls",-1234567891234567890L
63
 
    ,"v_tf",-2.0f
64
 
    ,"v_tfs",-2.0f
65
 
    ,"v_td",-2.0
66
 
    ,"v_tds",-2.0
67
 
    ,"v_tdt","2000-05-10T01:01:01Z"
68
 
    ,"v_tdts","2002-08-26T01:01:01Z"
69
 
    };
70
 
    String[] sarr = new String[arr.length];
71
 
    for (int i=0; i<arr.length; i++) {
72
 
      sarr[i] = arr[i].toString();
73
 
    }
74
 
 
75
 
    assertU(adoc(sarr));
76
 
    assertU(optimize());
77
 
 
78
 
    // test field queries
79
 
    for (int i=0; i<arr.length; i+=2) {
80
 
      String f = arr[i].toString();
81
 
      String v = arr[i+1].toString();
82
 
 
83
 
      // normal lucene fielded query
84
 
      assertQ(req( "q",f+":\""+v+'"')
85
 
              ,"//result[@numFound='1']"
86
 
              ,"//*[@name='id'][.='999.0']"
87
 
              ,"//*[@name='" + f + "'][.='" + v + "']"
88
 
              );
89
 
 
90
 
      // field qparser
91
 
      assertQ(req( "q", "{!field f="+f+"}"+v)
92
 
              ,"//result[@numFound='1']"
93
 
              );
94
 
 
95
 
      // lucene range
96
 
      assertQ(req( "q", f + ":[\"" + v + "\" TO \"" + v + "\"]" )
97
 
              ,"//result[@numFound='1']"
98
 
              );
99
 
    }
100
 
 
101
 
    // frange and function query only work on single valued field types
102
 
    Object[] fc_vals = new Object[] {
103
 
      "id",999.0
104
 
      ,"v_s","wow dude"
105
 
      ,"v_ti",-1
106
 
      ,"v_tl",-1234567891234567890L
107
 
      ,"v_tf",-2.0f
108
 
      ,"v_td",-2.0
109
 
      ,"v_tdt","2000-05-10T01:01:01Z"
110
 
    };
111
 
    
112
 
    for (int i=0; i<fc_vals.length; i+=2) {
113
 
      String f = fc_vals[i].toString();
114
 
      String v = fc_vals[i+1].toString();
115
 
      
116
 
      // frange qparser
117
 
      assertQ(req( "q", "{!frange v="+f+" l='"+v+"' u='"+v+"'}" )
118
 
              ,"//result[@numFound='1']"
119
 
              );
120
 
 
121
 
       // frange as filter not cached
122
 
      assertQ(req( "q","*:*", "fq", "{!frange cache=false v="+f+" l='"+v+"' u='"+v+"'}" )
123
 
              ,"//result[@numFound='1']"
124
 
              );
125
 
 
126
 
       // frange as filter run after the main query
127
 
      assertQ(req( "q","*:*", "fq", "{!frange cache=false cost=100 v="+f+" l='"+v+"' u='"+v+"'}" )
128
 
              ,"//result[@numFound='1']"
129
 
              );
130
 
      
131
 
      // function query... just make sure it doesn't throw an exception
132
 
      assertQ(req( "q", "+id:999 _val_:\"" + f + "\"")
133
 
              ,"//result[@numFound='1']"
134
 
              );
135
 
    }
136
 
 
137
 
    // Some basic tests to ensure that parsing local params is working
138
 
    assertQ("test prefix query",
139
 
            req("q","{!prefix f=v_t}hel")
140
 
            ,"//result[@numFound='2']"
141
 
            );
142
 
 
143
 
    assertQ("test raw query",
144
 
            req("q","{!raw f=v_t}hello")
145
 
            ,"//result[@numFound='2']"
146
 
            );
147
 
 
148
 
    // no analysis is done, so these should match nothing
149
 
    assertQ("test raw query",
150
 
            req("q","{!raw f=v_t}Hello")
151
 
            ,"//result[@numFound='0']"
152
 
            );
153
 
    assertQ("test raw query",
154
 
            req("q","{!raw f=v_f}1.5")
155
 
            ,"//result[@numFound='0']"
156
 
            );
157
 
 
158
 
    // test "term" qparser, which should only do readableToIndexed
159
 
    assertQ(
160
 
            req("q","{!term f=v_f}1.5")
161
 
            ,"//result[@numFound='1']"
162
 
            );
163
 
    
164
 
    // text fields are *not* analyzed since they may not be idempotent
165
 
    assertQ(
166
 
           req("q","{!term f=v_t}Hello")
167
 
           ,"//result[@numFound='0']"
168
 
           );
169
 
     assertQ(
170
 
           req("q","{!term f=v_t}hello")
171
 
           ,"//result[@numFound='2']"
172
 
           );
173
 
 
174
 
 
175
 
    //
176
 
    // test escapes in quoted strings
177
 
    //
178
 
 
179
 
    // the control... unescaped queries looking for internal"quote
180
 
    assertQ(req("q","{!raw f=v_s}internal\"quote")
181
 
            ,"//result[@numFound='1']"
182
 
            );
183
 
 
184
 
    // test that single quoted string needs no escape
185
 
    assertQ(req("q","{!raw f=v_s v='internal\"quote'}")
186
 
            ,"//result[@numFound='1']"
187
 
            );
188
 
 
189
 
    // but it's OK if the escape is done
190
 
    assertQ(req("q","{!raw f=v_s v='internal\\\"quote'}")
191
 
            ,"//result[@numFound='1']"
192
 
            );
193
 
 
194
 
    // test unicode escape
195
 
    assertQ(req("q","{!raw f=v_s v=\"internal\\u0022quote\"}")
196
 
            ,"//result[@numFound='1']"
197
 
            );
198
 
 
199
 
    // inside a quoted string, internal"quote needs to be escaped
200
 
    assertQ(req("q","{!raw f=v_s v=\"internal\\\"quote\"}")
201
 
            ,"//result[@numFound='1']"
202
 
            );
203
 
 
204
 
    assertQ("test custom plugin query",
205
 
            req("q","{!foo f=v_t}hello")
206
 
            ,"//result[@numFound='2']"
207
 
            );
208
 
 
209
 
 
210
 
    assertQ("test single term field query on text type",
211
 
            req("q","{!field f=v_t}HELLO")
212
 
            ,"//result[@numFound='2']"
213
 
            );
214
 
 
215
 
    assertQ("test single term field query on type with diff internal rep",
216
 
            req("q","{!field f=v_f}1.5")
217
 
            ,"//result[@numFound='1']"
218
 
            );    
219
 
 
220
 
    assertQ(
221
 
            req("q","{!field f=v_ti}5")
222
 
            ,"//result[@numFound='1']"
223
 
            );
224
 
 
225
 
     assertQ("test multi term field query on text type",
226
 
            req("q","{!field f=v_t}Hello  DUDE")
227
 
            ,"//result[@numFound='1']"
228
 
            );
229
 
 
230
 
 
231
 
    assertQ("test prefix query with value in local params",
232
 
            req("q","{!prefix f=v_t v=hel}")
233
 
            ,"//result[@numFound='2']"
234
 
    );
235
 
 
236
 
    assertQ("test optional quotes",
237
 
            req("q","{!prefix f='v_t' v=\"hel\"}")
238
 
            ,"//result[@numFound='2']"
239
 
    );
240
 
 
241
 
    assertQ("test extra whitespace",
242
 
            req("q","{!prefix   f=v_t   v=hel   }")
243
 
            ,"//result[@numFound='2']"
244
 
    );
245
 
 
246
 
    assertQ("test literal with {! in it",
247
 
            req("q","{!prefix f=v_s}{!lit")
248
 
            ,"//result[@numFound='1']"
249
 
    );
250
 
 
251
 
    assertQ("test param subst",
252
 
            req("q","{!prefix f=$myf v=$my.v}"
253
 
                ,"myf","v_t", "my.v", "hel"
254
 
            )
255
 
            ,"//result[@numFound='2']"
256
 
    );
257
 
 
258
 
    // test wacky param names
259
 
    assertQ(
260
 
            req("q","{!prefix f=$a/b/c v=$'a b/c'}"
261
 
                ,"a/b/c","v_t", "a b/c", "hel"
262
 
            )
263
 
            ,"//result[@numFound='2']"
264
 
    );
265
 
 
266
 
    assertQ("test param subst with literal",
267
 
            req("q","{!prefix f=$myf v=$my.v}"
268
 
                ,"myf","v_s", "my.v", "{!lit"
269
 
            )
270
 
            ,"//result[@numFound='1']"
271
 
    );
272
 
 
273
 
   // lucene queries
274
 
   assertQ("test lucene query",
275
 
            req("q","{!lucene}v_t:hel*")
276
 
            ,"//result[@numFound='2']"
277
 
            );
278
 
 
279
 
   // lucene queries
280
 
   assertQ("test lucene default field",
281
 
            req("q","{!df=v_t}hel*")
282
 
            ,"//result[@numFound='2']"
283
 
            );
284
 
 
285
 
   // lucene operator
286
 
   assertQ("test lucene operator",
287
 
            req("q","{!q.op=OR df=v_t}Hello Yonik")
288
 
            ,"//result[@numFound='2']"
289
 
            );
290
 
   assertQ("test lucene operator",
291
 
            req("q","{!q.op=AND df=v_t}Hello Yonik")
292
 
            ,"//result[@numFound='1']"
293
 
            );
294
 
 
295
 
    // test boost queries
296
 
    assertQ("test boost",
297
 
            req("q","{!boost b=sum(v_f,1)}id:[5 TO 6]"
298
 
                ,"fl","*,score"
299
 
            )
300
 
            ,"//result[@numFound='2']"
301
 
            ,"//doc[./float[@name='v_f']='3.14159' and ./float[@name='score']='4.14159']"
302
 
    );
303
 
 
304
 
    assertQ("test boost and default type of func",
305
 
            req("q","{!boost v=$q1 b=$q2}"
306
 
                ,"q1", "{!func}v_f", "q2","v_f"
307
 
                ,"fl","*,score"
308
 
            )
309
 
            ,"//doc[./float[@name='v_f']='1.5' and ./float[@name='score']='2.25']"
310
 
    );
311
 
 
312
 
 
313
 
    // dismax query from std request handler
314
 
    assertQ("test dismax query",
315
 
             req("q","{!dismax}hello"
316
 
                ,"qf","v_t"
317
 
                ,"bf","sqrt(v_f)^100 log(sum(v_f,1))^50"
318
 
                ,"bq","{!prefix f=v_t}he"
319
 
                ,"debugQuery","on"
320
 
             )
321
 
             ,"//result[@numFound='2']"
322
 
             );
323
 
 
324
 
    // dismax query from std request handler, using local params
325
 
    assertQ("test dismax query w/ local params",
326
 
             req("q","{!dismax qf=v_t}hello"
327
 
                ,"qf","v_f"
328
 
             )
329
 
             ,"//result[@numFound='2']"
330
 
             );
331
 
 
332
 
    assertQ("test nested query",
333
 
            req("q","_query_:\"{!query v=$q1}\"", "q1","{!prefix f=v_t}hel")
334
 
            ,"//result[@numFound='2']"
335
 
            );
336
 
 
337
 
    assertQ("test nested nested query",
338
 
            req("q","_query_:\"{!query defType=query v=$q1}\"", "q1","{!v=$q2}","q2","{!prefix f=v_t v=$qqq}","qqq","hel")
339
 
            ,"//result[@numFound='2']"
340
 
            );
341
 
 
342
 
  }
343
 
}