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.
17
package org.apache.solr.search;
19
import org.apache.solr.util.AbstractSolrTestCase;
21
public class TestQueryTypes extends AbstractSolrTestCase {
24
public String getSchemaFile() { return "schema11.xml"; }
26
public String getSolrConfigFile() { return "solrconfig.xml"; }
27
public String getCoreName() { return "basic"; }
31
public void setUp() throws Exception {
32
// if you override setUp or tearDown, you better call
33
// the super classes version
37
public void tearDown() throws Exception {
38
// if you override setUp or tearDown, you better call
39
// the super classes version
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"));
55
Object[] arr = new Object[] {
61
,"v_tl",-1234567891234567890L
62
,"v_tls",-1234567891234567890L
67
,"v_tdt","2000-05-10T01:01:01Z"
68
,"v_tdts","2002-08-26T01:01:01Z"
70
String[] sarr = new String[arr.length];
71
for (int i=0; i<arr.length; i++) {
72
sarr[i] = arr[i].toString();
79
for (int i=0; i<arr.length; i+=2) {
80
String f = arr[i].toString();
81
String v = arr[i+1].toString();
83
// normal lucene fielded query
84
assertQ(req( "q",f+":\""+v+'"')
85
,"//result[@numFound='1']"
86
,"//*[@name='id'][.='999.0']"
87
,"//*[@name='" + f + "'][.='" + v + "']"
91
assertQ(req( "q", "{!field f="+f+"}"+v)
92
,"//result[@numFound='1']"
96
assertQ(req( "q", f + ":[\"" + v + "\" TO \"" + v + "\"]" )
97
,"//result[@numFound='1']"
101
// frange and function query only work on single valued field types
102
Object[] fc_vals = new Object[] {
106
,"v_tl",-1234567891234567890L
109
,"v_tdt","2000-05-10T01:01:01Z"
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();
117
assertQ(req( "q", "{!frange v="+f+" l='"+v+"' u='"+v+"'}" )
118
,"//result[@numFound='1']"
121
// frange as filter not cached
122
assertQ(req( "q","*:*", "fq", "{!frange cache=false v="+f+" l='"+v+"' u='"+v+"'}" )
123
,"//result[@numFound='1']"
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']"
131
// function query... just make sure it doesn't throw an exception
132
assertQ(req( "q", "+id:999 _val_:\"" + f + "\"")
133
,"//result[@numFound='1']"
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']"
143
assertQ("test raw query",
144
req("q","{!raw f=v_t}hello")
145
,"//result[@numFound='2']"
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']"
153
assertQ("test raw query",
154
req("q","{!raw f=v_f}1.5")
155
,"//result[@numFound='0']"
158
// test "term" qparser, which should only do readableToIndexed
160
req("q","{!term f=v_f}1.5")
161
,"//result[@numFound='1']"
164
// text fields are *not* analyzed since they may not be idempotent
166
req("q","{!term f=v_t}Hello")
167
,"//result[@numFound='0']"
170
req("q","{!term f=v_t}hello")
171
,"//result[@numFound='2']"
176
// test escapes in quoted strings
179
// the control... unescaped queries looking for internal"quote
180
assertQ(req("q","{!raw f=v_s}internal\"quote")
181
,"//result[@numFound='1']"
184
// test that single quoted string needs no escape
185
assertQ(req("q","{!raw f=v_s v='internal\"quote'}")
186
,"//result[@numFound='1']"
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']"
194
// test unicode escape
195
assertQ(req("q","{!raw f=v_s v=\"internal\\u0022quote\"}")
196
,"//result[@numFound='1']"
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']"
204
assertQ("test custom plugin query",
205
req("q","{!foo f=v_t}hello")
206
,"//result[@numFound='2']"
210
assertQ("test single term field query on text type",
211
req("q","{!field f=v_t}HELLO")
212
,"//result[@numFound='2']"
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']"
221
req("q","{!field f=v_ti}5")
222
,"//result[@numFound='1']"
225
assertQ("test multi term field query on text type",
226
req("q","{!field f=v_t}Hello DUDE")
227
,"//result[@numFound='1']"
231
assertQ("test prefix query with value in local params",
232
req("q","{!prefix f=v_t v=hel}")
233
,"//result[@numFound='2']"
236
assertQ("test optional quotes",
237
req("q","{!prefix f='v_t' v=\"hel\"}")
238
,"//result[@numFound='2']"
241
assertQ("test extra whitespace",
242
req("q","{!prefix f=v_t v=hel }")
243
,"//result[@numFound='2']"
246
assertQ("test literal with {! in it",
247
req("q","{!prefix f=v_s}{!lit")
248
,"//result[@numFound='1']"
251
assertQ("test param subst",
252
req("q","{!prefix f=$myf v=$my.v}"
253
,"myf","v_t", "my.v", "hel"
255
,"//result[@numFound='2']"
258
// test wacky param names
260
req("q","{!prefix f=$a/b/c v=$'a b/c'}"
261
,"a/b/c","v_t", "a b/c", "hel"
263
,"//result[@numFound='2']"
266
assertQ("test param subst with literal",
267
req("q","{!prefix f=$myf v=$my.v}"
268
,"myf","v_s", "my.v", "{!lit"
270
,"//result[@numFound='1']"
274
assertQ("test lucene query",
275
req("q","{!lucene}v_t:hel*")
276
,"//result[@numFound='2']"
280
assertQ("test lucene default field",
281
req("q","{!df=v_t}hel*")
282
,"//result[@numFound='2']"
286
assertQ("test lucene operator",
287
req("q","{!q.op=OR df=v_t}Hello Yonik")
288
,"//result[@numFound='2']"
290
assertQ("test lucene operator",
291
req("q","{!q.op=AND df=v_t}Hello Yonik")
292
,"//result[@numFound='1']"
295
// test boost queries
296
assertQ("test boost",
297
req("q","{!boost b=sum(v_f,1)}id:[5 TO 6]"
300
,"//result[@numFound='2']"
301
,"//doc[./float[@name='v_f']='3.14159' and ./float[@name='score']='4.14159']"
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"
309
,"//doc[./float[@name='v_f']='1.5' and ./float[@name='score']='2.25']"
313
// dismax query from std request handler
314
assertQ("test dismax query",
315
req("q","{!dismax}hello"
317
,"bf","sqrt(v_f)^100 log(sum(v_f,1))^50"
318
,"bq","{!prefix f=v_t}he"
321
,"//result[@numFound='2']"
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"
329
,"//result[@numFound='2']"
332
assertQ("test nested query",
333
req("q","_query_:\"{!query v=$q1}\"", "q1","{!prefix f=v_t}hel")
334
,"//result[@numFound='2']"
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']"