1
package org.apache.solr.search;
3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
import org.apache.lucene.search.Sort;
20
import org.apache.lucene.search.SortField;
21
import org.apache.solr.SolrTestCaseJ4;
22
import org.apache.solr.common.SolrException;
23
import org.apache.solr.request.SolrQueryRequest;
24
import org.apache.solr.schema.IndexSchema;
25
import org.junit.BeforeClass;
26
import org.junit.Test;
32
public class QueryParsingTest extends SolrTestCaseJ4 {
34
public static void beforeClass() throws Exception {
35
initCore("solrconfig.xml","schema.xml");
39
public void testSort() throws Exception {
41
SolrQueryRequest req = req();
43
IndexSchema schema = h.getCore().getSchema();
44
sort = QueryParsing.parseSort("score desc", req);
45
assertNull("sort", sort);//only 1 thing in the list, no Sort specified
47
sort = QueryParsing.parseSort("score asc", req);
48
SortField[] flds = sort.getSort();
49
assertEquals(flds[0].getType(), SortField.SCORE);
50
assertTrue(flds[0].getReverse());
52
sort = QueryParsing.parseSort("weight desc", req);
53
flds = sort.getSort();
54
assertEquals(flds[0].getType(), SortField.FLOAT);
55
assertEquals(flds[0].getField(), "weight");
56
assertEquals(flds[0].getReverse(), true);
57
sort = QueryParsing.parseSort("weight desc,bday asc", req);
58
flds = sort.getSort();
59
assertEquals(flds[0].getType(), SortField.FLOAT);
60
assertEquals(flds[0].getField(), "weight");
61
assertEquals(flds[0].getReverse(), true);
62
assertEquals(flds[1].getType(), SortField.LONG);
63
assertEquals(flds[1].getField(), "bday");
64
assertEquals(flds[1].getReverse(), false);
66
sort = QueryParsing.parseSort("weight top,bday asc", req);
67
flds = sort.getSort();
68
assertEquals(flds[0].getType(), SortField.FLOAT);
69
assertEquals(flds[0].getField(), "weight");
70
assertEquals(flds[0].getReverse(), true);
71
assertEquals(flds[1].getType(), SortField.LONG);
72
assertEquals(flds[1].getField(), "bday");
73
assertEquals(flds[1].getReverse(), false);
74
sort = QueryParsing.parseSort("weight top,bday bottom", req);
75
flds = sort.getSort();
76
assertEquals(flds[0].getType(), SortField.FLOAT);
77
assertEquals(flds[0].getField(), "weight");
78
assertEquals(flds[0].getReverse(), true);
79
assertEquals(flds[1].getType(), SortField.LONG);
80
assertEquals(flds[1].getField(), "bday");
81
assertEquals(flds[1].getReverse(), false);
84
sort = QueryParsing.parseSort("weight desc, bday asc", req);
85
flds = sort.getSort();
86
assertEquals(flds[0].getType(), SortField.FLOAT);
87
assertEquals(flds[0].getField(), "weight");
88
assertEquals(flds[1].getField(), "bday");
89
assertEquals(flds[1].getType(), SortField.LONG);
90
//handles trailing commas
91
sort = QueryParsing.parseSort("weight desc,", req);
92
flds = sort.getSort();
93
assertEquals(flds[0].getType(), SortField.FLOAT);
94
assertEquals(flds[0].getField(), "weight");
97
sort = QueryParsing.parseSort("pow(weight, 2) desc", req);
98
flds = sort.getSort();
99
assertEquals(flds[0].getType(), SortField.CUSTOM);
100
//Not thrilled about the fragility of string matching here, but...
101
//the value sources get wrapped, so the out field is different than the input
102
assertEquals(flds[0].getField(), "pow(float(weight),const(2.0))");
104
//test functions (more deep)
105
sort = QueryParsing.parseSort("sum(product(r_f1,sum(d_f1,t_f1,1)),a_f1) asc", req);
106
flds = sort.getSort();
107
assertEquals(flds[0].getType(), SortField.CUSTOM);
108
assertEquals(flds[0].getField(), "sum(product(float(r_f1),sum(float(d_f1),float(t_f1),const(1.0))),float(a_f1))");
110
sort = QueryParsing.parseSort("pow(weight, 2) desc", req);
111
flds = sort.getSort();
112
assertEquals(flds[0].getType(), SortField.CUSTOM);
113
//Not thrilled about the fragility of string matching here, but...
114
//the value sources get wrapped, so the out field is different than the input
115
assertEquals(flds[0].getField(), "pow(float(weight),const(2.0))");
118
sort = QueryParsing.parseSort("pow(weight, 2) desc, weight desc, bday asc", req);
119
flds = sort.getSort();
120
assertEquals(flds[0].getType(), SortField.CUSTOM);
122
//Not thrilled about the fragility of string matching here, but...
123
//the value sources get wrapped, so the out field is different than the input
124
assertEquals(flds[0].getField(), "pow(float(weight),const(2.0))");
126
assertEquals(flds[1].getType(), SortField.FLOAT);
127
assertEquals(flds[1].getField(), "weight");
128
assertEquals(flds[2].getField(), "bday");
129
assertEquals(flds[2].getType(), SortField.LONG);
131
//handles trailing commas
132
sort = QueryParsing.parseSort("weight desc,", req);
133
flds = sort.getSort();
134
assertEquals(flds[0].getType(), SortField.FLOAT);
135
assertEquals(flds[0].getField(), "weight");
137
//Test literals in functions
138
sort = QueryParsing.parseSort("strdist(foo_s1, \"junk\", jw) desc", req);
139
flds = sort.getSort();
140
assertEquals(flds[0].getType(), SortField.CUSTOM);
141
//the value sources get wrapped, so the out field is different than the input
142
assertEquals(flds[0].getField(), "strdist(str(foo_s1),literal(junk), dist=org.apache.lucene.search.spell.JaroWinklerDistance)");
144
sort = QueryParsing.parseSort("", req);
151
public void testBad() throws Exception {
153
SolrQueryRequest req = req();
155
IndexSchema schema = h.getCore().getSchema();
158
sort = QueryParsing.parseSort("weight, desc", req);
160
} catch (SolrException e) {
164
sort = QueryParsing.parseSort("w", req);
166
} catch (SolrException e) {
170
sort = QueryParsing.parseSort("weight desc, bday", req);
172
} catch (SolrException e) {
176
//bad number of commas
177
sort = QueryParsing.parseSort("pow(weight,,2) desc, bday asc", req);
179
} catch (SolrException e) {
184
sort = QueryParsing.parseSort("pow() desc, bday asc", req);
186
} catch (SolrException e) {
190
//bad number of parens
191
sort = QueryParsing.parseSort("pow((weight,2) desc, bday asc", req);
193
} catch (SolrException e) {