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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/schema/NumericFieldsTest.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.schema;
2
 
 
3
 
/*
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
import org.apache.solr.SolrTestCaseJ4;
21
 
import org.apache.solr.common.SolrInputDocument;
22
 
import org.junit.BeforeClass;
23
 
import org.junit.Test;
24
 
 
25
 
 
26
 
public class NumericFieldsTest extends SolrTestCaseJ4 {
27
 
  @BeforeClass
28
 
  public static void beforeClass() throws Exception {
29
 
    initCore("solrconfig-master.xml", "schema-numeric.xml");
30
 
  }
31
 
 
32
 
  static String[] types = new String[]{"int", "long", "float", "double", "date"};
33
 
 
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);
42
 
      } else {
43
 
        doc.addField(t, number);
44
 
        doc.addField(t + "_last", number);
45
 
        doc.addField(t + "_first", number);
46
 
      }
47
 
    }
48
 
    return doc;
49
 
  }
50
 
 
51
 
  @Test
52
 
  public void testSortMissingFirstLast() {
53
 
    clearIndex();
54
 
 
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"));
60
 
    assertU(commit());
61
 
    // 'normal' sorting.  Missing Values are 0
62
 
    String suffix = "";
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"),
67
 
            "//*[@numFound='5']",
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']"
73
 
        );
74
 
 
75
 
        assertQ("Sorting Desc: " + t + suffix,
76
 
            req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc"),
77
 
            "//*[@numFound='5']",
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']"
83
 
        );
84
 
      } else {
85
 
        assertQ("Sorting Asc: " + t + suffix,
86
 
            req("fl", "id", "q", "*:*", "sort", (t + suffix) + " asc"),
87
 
            "//*[@numFound='5']",
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']"
93
 
        );
94
 
 
95
 
        assertQ("Sorting Desc: " + t + suffix,
96
 
            req("fl", "id", "q", "*:*", "sort", (t + suffix) + " desc"),
97
 
            "//*[@numFound='5']",
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']"
103
 
        );
104
 
 
105
 
      }
106
 
    }
107
 
 
108
 
 
109
 
    // sortMissingLast = true 
110
 
    suffix = "_last";
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']"
120
 
      );
121
 
 
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']"
131
 
      );
132
 
    }
133
 
 
134
 
    // sortMissingFirst = true 
135
 
    suffix = "_first";
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']"
145
 
      );
146
 
 
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']"
156
 
      );
157
 
    }
158
 
  }
159
 
}