1
+ /*
2
+ * Licensed to Elastic Search and Shay Banon under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. Elastic Search licenses this
6
+ * file to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with 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,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ package org .elasticsearch .index .mapper .json ;
21
+
22
+ import org .apache .lucene .document .Field ;
23
+ import org .apache .lucene .document .Fieldable ;
24
+ import org .apache .lucene .search .*;
25
+ import org .apache .lucene .util .NumericUtils ;
26
+ import org .codehaus .jackson .JsonToken ;
27
+ import org .elasticsearch .index .analysis .NamedAnalyzer ;
28
+ import org .elasticsearch .index .analysis .NumericIntegerAnalyzer ;
29
+ import org .elasticsearch .util .Numbers ;
30
+ import org .elasticsearch .util .json .JsonBuilder ;
31
+
32
+ import java .io .IOException ;
33
+
34
+ /**
35
+ * @author kimchy (shay.banon)
36
+ */
37
+ public class JsonShortFieldMapper extends JsonNumberFieldMapper <Short > {
38
+
39
+ public static final String JSON_TYPE = "short" ;
40
+
41
+ public static class Defaults extends JsonNumberFieldMapper .Defaults {
42
+ public static final Short NULL_VALUE = null ;
43
+ }
44
+
45
+ public static class Builder extends JsonNumberFieldMapper .Builder <Builder , JsonShortFieldMapper > {
46
+
47
+ protected Short nullValue = Defaults .NULL_VALUE ;
48
+
49
+ public Builder (String name ) {
50
+ super (name );
51
+ builder = this ;
52
+ }
53
+
54
+ public Builder nullValue (short nullValue ) {
55
+ this .nullValue = nullValue ;
56
+ return this ;
57
+ }
58
+
59
+ @ Override public JsonShortFieldMapper build (BuilderContext context ) {
60
+ JsonShortFieldMapper fieldMapper = new JsonShortFieldMapper (buildNames (context ),
61
+ precisionStep , index , store , boost , omitNorms , omitTermFreqAndPositions , nullValue );
62
+ fieldMapper .includeInAll (includeInAll );
63
+ return fieldMapper ;
64
+ }
65
+ }
66
+
67
+ private final Short nullValue ;
68
+
69
+ private final String nullValueAsString ;
70
+
71
+ protected JsonShortFieldMapper (Names names , int precisionStep , Field .Index index , Field .Store store ,
72
+ float boost , boolean omitNorms , boolean omitTermFreqAndPositions ,
73
+ Short nullValue ) {
74
+ super (names , precisionStep , index , store , boost , omitNorms , omitTermFreqAndPositions ,
75
+ new NamedAnalyzer ("_short/" + precisionStep , new NumericIntegerAnalyzer (precisionStep )),
76
+ new NamedAnalyzer ("_short/max" , new NumericIntegerAnalyzer (Integer .MAX_VALUE )));
77
+ this .nullValue = nullValue ;
78
+ this .nullValueAsString = nullValue == null ? null : nullValue .toString ();
79
+ }
80
+
81
+ @ Override protected int maxPrecisionStep () {
82
+ return 32 ;
83
+ }
84
+
85
+ @ Override public Short value (Fieldable field ) {
86
+ byte [] value = field .getBinaryValue ();
87
+ if (value == null ) {
88
+ return Short .MIN_VALUE ;
89
+ }
90
+ return Numbers .bytesToShort (value );
91
+ }
92
+
93
+ @ Override public String indexedValue (String value ) {
94
+ return indexedValue (Short .parseShort (value ));
95
+ }
96
+
97
+ @ Override public String indexedValue (Short value ) {
98
+ return NumericUtils .intToPrefixCoded (value );
99
+ }
100
+
101
+ @ Override public Object valueFromTerm (String term ) {
102
+ final int shift = term .charAt (0 ) - NumericUtils .SHIFT_START_INT ;
103
+ if (shift > 0 && shift <= 31 ) {
104
+ return null ;
105
+ }
106
+ return NumericUtils .prefixCodedToInt (term );
107
+ }
108
+
109
+ @ Override public Object valueFromString (String text ) {
110
+ return Short .parseShort (text );
111
+ }
112
+
113
+ @ Override public Query rangeQuery (String lowerTerm , String upperTerm , boolean includeLower , boolean includeUpper ) {
114
+ return NumericRangeQuery .newIntRange (names .indexName (), precisionStep ,
115
+ lowerTerm == null ? null : Integer .parseInt (lowerTerm ),
116
+ upperTerm == null ? null : Integer .parseInt (upperTerm ),
117
+ includeLower , includeUpper );
118
+ }
119
+
120
+ @ Override public Filter rangeFilter (String lowerTerm , String upperTerm , boolean includeLower , boolean includeUpper ) {
121
+ return NumericRangeFilter .newIntRange (names .indexName (), precisionStep ,
122
+ lowerTerm == null ? null : Integer .parseInt (lowerTerm ),
123
+ upperTerm == null ? null : Integer .parseInt (upperTerm ),
124
+ includeLower , includeUpper );
125
+ }
126
+
127
+ @ Override protected Field parseCreateField (JsonParseContext jsonContext ) throws IOException {
128
+ int value ;
129
+ if (jsonContext .jp ().getCurrentToken () == JsonToken .VALUE_NULL ) {
130
+ if (nullValue == null ) {
131
+ return null ;
132
+ }
133
+ value = nullValue ;
134
+ if (includeInAll == null || includeInAll ) {
135
+ jsonContext .allEntries ().addText (names .fullName (), nullValueAsString , boost );
136
+ }
137
+ } else {
138
+ if (jsonContext .jp ().getCurrentToken () == JsonToken .VALUE_STRING ) {
139
+ value = Integer .parseInt (jsonContext .jp ().getText ());
140
+ } else {
141
+ value = jsonContext .jp ().getIntValue ();
142
+ }
143
+ if (includeInAll == null || includeInAll ) {
144
+ jsonContext .allEntries ().addText (names .fullName (), jsonContext .jp ().getText (), boost );
145
+ }
146
+ }
147
+ Field field = null ;
148
+ if (stored ()) {
149
+ field = new Field (names .indexName (), Numbers .shortToBytes (value ), store );
150
+ if (indexed ()) {
151
+ field .setTokenStream (popCachedStream (precisionStep ).setIntValue (value ));
152
+ }
153
+ } else if (indexed ()) {
154
+ field = new Field (names .indexName (), popCachedStream (precisionStep ).setIntValue (value ));
155
+ }
156
+ return field ;
157
+ }
158
+
159
+ @ Override public int sortType () {
160
+ return SortField .SHORT ;
161
+ }
162
+
163
+ @ Override protected String jsonType () {
164
+ return JSON_TYPE ;
165
+ }
166
+
167
+ @ Override protected void doJsonBody (JsonBuilder builder ) throws IOException {
168
+ super .doJsonBody (builder );
169
+ if (nullValue != null ) {
170
+ builder .field ("nullValue" , nullValue );
171
+ }
172
+ if (includeInAll != null ) {
173
+ builder .field ("includeInAll" , includeInAll );
174
+ }
175
+ }
176
+ }
0 commit comments