1
package org.apache.solr.schema;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.solr.SolrTestCaseJ4;
21
import org.apache.solr.common.SolrInputDocument;
22
import org.junit.BeforeClass;
23
import org.junit.Test;
26
public class NumericFieldsTest extends SolrTestCaseJ4 {
28
public static void beforeClass() throws Exception {
29
initCore("solrconfig-master.xml", "schema-numeric.xml");
32
static String[] types = new String[]{"int", "long", "float", "double", "date"};
34
public static SolrInputDocument getDoc(String id, Integer number, String date) {
35
SolrInputDocument doc = new SolrInputDocument();
36
doc.addField("id", id);
37
for (String t : types) {
38
if ("date".equals(t)) {
39
doc.addField(t, date);
40
doc.addField(t + "_last", date);
41
doc.addField(t + "_first", date);
43
doc.addField(t, number);
44
doc.addField(t + "_last", number);
45
doc.addField(t + "_first", number);
52
public void testSortMissingFirstLast() {
55
assertU(adoc("id", "M1"));
56
assertU(adoc(getDoc("+4", 4, "2011-04-04T00:00:00Z")));
57
assertU(adoc(getDoc("+5", 5, "2011-05-05T00:00:00Z")));
58
assertU(adoc(getDoc("-3", -3, "2011-01-01T00:00:00Z")));
59
assertU(adoc("id", "M2"));
61
// 'normal' sorting. Missing Values are 0
63
for (String t : types) {
64
if ("date".equals(t)) {
65
assertQ("Sorting Asc: " + t + suffix,
66
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " asc"),
68
"//result/doc[1]/str[@name='id'][.='M1']",
69
"//result/doc[2]/str[@name='id'][.='M2']",
70
"//result/doc[3]/str[@name='id'][.='-3']",
71
"//result/doc[4]/str[@name='id'][.='+4']",
72
"//result/doc[5]/str[@name='id'][.='+5']"
75
assertQ("Sorting Desc: " + t + suffix,
76
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc"),
78
"//result/doc[1]/str[@name='id'][.='+5']",
79
"//result/doc[2]/str[@name='id'][.='+4']",
80
"//result/doc[3]/str[@name='id'][.='-3']",
81
"//result/doc[4]/str[@name='id'][.='M1']",
82
"//result/doc[5]/str[@name='id'][.='M2']"
85
assertQ("Sorting Asc: " + t + suffix,
86
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " asc"),
88
"//result/doc[1]/str[@name='id'][.='-3']",
89
"//result/doc[2]/str[@name='id'][.='M1']",
90
"//result/doc[3]/str[@name='id'][.='M2']",
91
"//result/doc[4]/str[@name='id'][.='+4']",
92
"//result/doc[5]/str[@name='id'][.='+5']"
95
assertQ("Sorting Desc: " + t + suffix,
96
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc"),
98
"//result/doc[1]/str[@name='id'][.='+5']",
99
"//result/doc[2]/str[@name='id'][.='+4']",
100
"//result/doc[3]/str[@name='id'][.='M1']",
101
"//result/doc[4]/str[@name='id'][.='M2']",
102
"//result/doc[5]/str[@name='id'][.='-3']"
109
// sortMissingLast = true
111
for (String t : types) {
112
assertQ("Sorting Asc: " + t + suffix,
113
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " asc"),
114
"//*[@numFound='5']",
115
"//result/doc[1]/str[@name='id'][.='-3']",
116
"//result/doc[2]/str[@name='id'][.='+4']",
117
"//result/doc[3]/str[@name='id'][.='+5']",
118
"//result/doc[4]/str[@name='id'][.='M1']",
119
"//result/doc[5]/str[@name='id'][.='M2']"
122
// This does not match
123
assertQ("Sorting Desc: " + t + suffix,
124
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc", "indent", "on"),
125
"//*[@numFound='5']",
126
"//result/doc[1]/str[@name='id'][.='+5']",
127
"//result/doc[2]/str[@name='id'][.='+4']",
128
"//result/doc[3]/str[@name='id'][.='-3']",
129
"//result/doc[4]/str[@name='id'][.='M1']",
130
"//result/doc[5]/str[@name='id'][.='M2']"
134
// sortMissingFirst = true
136
for (String t : types) {
137
assertQ("Sorting Asc: " + t + suffix,
138
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " asc", "indent", "on"),
139
"//*[@numFound='5']",
140
"//result/doc[1]/str[@name='id'][.='M1']",
141
"//result/doc[2]/str[@name='id'][.='M2']",
142
"//result/doc[3]/str[@name='id'][.='-3']",
143
"//result/doc[4]/str[@name='id'][.='+4']",
144
"//result/doc[5]/str[@name='id'][.='+5']"
147
// This does not match
148
assertQ("Sorting Desc: " + t + suffix,
149
req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc", "indent", "on"),
150
"//*[@numFound='5']",
151
"//result/doc[1]/str[@name='id'][.='M1']",
152
"//result/doc[2]/str[@name='id'][.='M2']",
153
"//result/doc[3]/str[@name='id'][.='+5']",
154
"//result/doc[4]/str[@name='id'][.='+4']",
155
"//result/doc[5]/str[@name='id'][.='-3']"