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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/search/QueryParsingTest.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
 
package org.apache.solr.search;
2
 
/**
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
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
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.
17
 
 */
18
 
 
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;
27
 
 
28
 
/**
29
 
 *
30
 
 *
31
 
 **/
32
 
public class QueryParsingTest extends SolrTestCaseJ4 {
33
 
  @BeforeClass
34
 
  public static void beforeClass() throws Exception {
35
 
    initCore("solrconfig.xml","schema.xml");
36
 
  }
37
 
 
38
 
  @Test
39
 
  public void testSort() throws Exception {
40
 
    Sort sort;
41
 
    SolrQueryRequest req = req();
42
 
 
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
46
 
 
47
 
    sort = QueryParsing.parseSort("score asc", req);
48
 
    SortField[] flds = sort.getSort();
49
 
    assertEquals(flds[0].getType(), SortField.SCORE);
50
 
    assertTrue(flds[0].getReverse());
51
 
 
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);
65
 
    //order aliases
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);
82
 
 
83
 
    //test weird spacing
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");
95
 
 
96
 
    //test functions
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))");
103
 
    
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))");
109
 
 
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))");
116
 
 
117
 
 
118
 
    sort = QueryParsing.parseSort("pow(weight, 2) desc, weight    desc,   bday    asc", req);
119
 
    flds = sort.getSort();
120
 
    assertEquals(flds[0].getType(), SortField.CUSTOM);
121
 
 
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))");
125
 
 
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);
130
 
    
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");
136
 
 
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)");
143
 
 
144
 
    sort = QueryParsing.parseSort("", req);
145
 
    assertNull(sort);
146
 
 
147
 
    req.close();
148
 
  }
149
 
 
150
 
  @Test
151
 
  public void testBad() throws Exception {
152
 
    Sort sort;
153
 
    SolrQueryRequest req = req();
154
 
 
155
 
    IndexSchema schema = h.getCore().getSchema();
156
 
    //test some bad vals
157
 
    try {
158
 
      sort = QueryParsing.parseSort("weight, desc", req);
159
 
      assertTrue(false);
160
 
    } catch (SolrException e) {
161
 
      //expected
162
 
    }
163
 
    try {
164
 
      sort = QueryParsing.parseSort("w", req);
165
 
      assertTrue(false);
166
 
    } catch (SolrException e) {
167
 
      //expected
168
 
    }
169
 
    try {
170
 
      sort = QueryParsing.parseSort("weight desc, bday", req);
171
 
      assertTrue(false);
172
 
    } catch (SolrException e) {
173
 
    }
174
 
 
175
 
    try {
176
 
      //bad number of commas
177
 
      sort = QueryParsing.parseSort("pow(weight,,2) desc, bday asc", req);
178
 
      assertTrue(false);
179
 
    } catch (SolrException e) {
180
 
    }
181
 
 
182
 
    try {
183
 
      //bad function
184
 
      sort = QueryParsing.parseSort("pow() desc, bday asc", req);
185
 
      assertTrue(false);
186
 
    } catch (SolrException e) {
187
 
    }
188
 
 
189
 
    try {
190
 
      //bad number of parens
191
 
      sort = QueryParsing.parseSort("pow((weight,2) desc, bday asc", req);
192
 
      assertTrue(false);
193
 
    } catch (SolrException e) {
194
 
    }
195
 
 
196
 
    req.close();
197
 
  }
198
 
 
199
 
}