Skip to content

Commit bc03d89

Browse files
committed
work on terms api to work properly with number types
1 parent a039a6c commit bc03d89

13 files changed

+146
-10
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/terms/TermsRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.terms;
2121

2222
import org.elasticsearch.ElasticSearchIllegalArgumentException;
23+
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
2425
import org.elasticsearch.index.mapper.AllFieldMapper;
2526
import org.elasticsearch.util.io.stream.StreamInput;
@@ -141,6 +142,14 @@ public TermsRequest(String... indices) {
141142
super(indices, null);
142143
}
143144

145+
@Override public ActionRequestValidationException validate() {
146+
ActionRequestValidationException validationException = super.validate();
147+
if (fields == null || fields.length == 0) {
148+
fields = DEFAULT_FIELDS;
149+
}
150+
return validationException;
151+
}
152+
144153
/**
145154
* The fields within each document which terms will be iterated over and returned with the
146155
* document frequencies.

modules/elasticsearch/src/main/java/org/elasticsearch/action/terms/TransportTermsAction.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ public class TransportTermsAction extends TransportBroadcastOperationAction<Term
196196
if (term == null || indexFieldName != term.field()) { // StirngHelper.intern
197197
break;
198198
}
199+
// convert to actual term text
200+
if (fieldMapper != null && fieldMapper.requiresStringToStringConversion()) {
201+
// valueAsString returns null indicating that this is not interesting
202+
term = term.createTerm(fieldMapper.valueAsString(term.text()));
203+
// if we need to break on this term enumeration, bail
204+
if (fieldMapper.shouldBreakTermEnumeration(term.text())) {
205+
break;
206+
}
207+
if (term.text() == null) {
208+
continue;
209+
}
210+
}
199211
// does it match on the prefix?
200212
if (request.prefix() != null && !term.text().startsWith(request.prefix())) {
201213
break;
@@ -240,6 +252,18 @@ public class TransportTermsAction extends TransportBroadcastOperationAction<Term
240252
if (term == null || indexFieldName != term.field()) { // StirngHelper.intern
241253
break;
242254
}
255+
// convert to actual term text
256+
if (fieldMapper != null && fieldMapper.requiresStringToStringConversion()) {
257+
// valueAsString returns null indicating that this is not interesting
258+
term = term.createTerm(fieldMapper.valueAsString(term.text()));
259+
// if we need to break on this term enumeration, bail
260+
if (fieldMapper.shouldBreakTermEnumeration(term.text())) {
261+
break;
262+
}
263+
if (term.text() == null) {
264+
continue;
265+
}
266+
}
243267
// does it match on the prefix?
244268
if (request.prefix() != null && !term.text().startsWith(request.prefix())) {
245269
break;

modules/elasticsearch/src/main/java/org/elasticsearch/cluster/node/Node.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.cluster.node;
2121

2222
import com.google.common.collect.ImmutableList;
23+
import org.apache.lucene.util.StringHelper;
2324
import org.elasticsearch.util.io.stream.StreamInput;
2425
import org.elasticsearch.util.io.stream.StreamOutput;
2526
import org.elasticsearch.util.io.stream.Streamable;
@@ -36,7 +37,7 @@ public class Node implements Streamable, Serializable {
3637

3738
public static final ImmutableList<Node> EMPTY_LIST = ImmutableList.of();
3839

39-
private String nodeName = "";
40+
private String nodeName = StringHelper.intern("");
4041

4142
private String nodeId;
4243

@@ -52,12 +53,13 @@ public Node(String nodeId, TransportAddress address) {
5253
}
5354

5455
public Node(String nodeName, boolean dataNode, String nodeId, TransportAddress address) {
55-
this.nodeName = nodeName;
56-
this.dataNode = dataNode;
57-
if (this.nodeName == null) {
58-
this.nodeName = "";
56+
if (nodeName == null) {
57+
this.nodeName = StringHelper.intern("");
58+
} else {
59+
this.nodeName = StringHelper.intern(nodeName);
5960
}
60-
this.nodeId = nodeId;
61+
this.dataNode = dataNode;
62+
this.nodeId = StringHelper.intern(nodeId);
6163
this.address = address;
6264
}
6365

@@ -96,9 +98,9 @@ public static Node readNode(StreamInput in) throws IOException {
9698
}
9799

98100
@Override public void readFrom(StreamInput in) throws IOException {
99-
nodeName = in.readUTF();
101+
nodeName = StringHelper.intern(in.readUTF());
100102
dataNode = in.readBoolean();
101-
nodeId = in.readUTF();
103+
nodeId = StringHelper.intern(in.readUTF());
102104
address = TransportAddressSerializers.addressFromStream(in);
103105
}
104106

modules/elasticsearch/src/main/java/org/elasticsearch/index/Index.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index;
2121

22+
import org.apache.lucene.util.StringHelper;
2223
import org.elasticsearch.util.concurrent.Immutable;
2324
import org.elasticsearch.util.io.stream.StreamInput;
2425
import org.elasticsearch.util.io.stream.StreamOutput;
@@ -40,7 +41,7 @@ private Index() {
4041
}
4142

4243
public Index(String name) {
43-
this.name = name;
44+
this.name = StringHelper.intern(name);
4445
}
4546

4647
public String name() {
@@ -73,7 +74,7 @@ public static Index readIndexName(StreamInput in) throws IOException {
7374
}
7475

7576
@Override public void readFrom(StreamInput in) throws IOException {
76-
name = in.readUTF();
77+
name = StringHelper.intern(in.readUTF());
7778
}
7879

7980
@Override public void writeTo(StreamOutput out) throws IOException {

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ public String fullName() {
128128
*/
129129
String valueAsString(Fieldable field);
130130

131+
/**
132+
* Returns <tt>true</tt> if {@link #valueAsString(String)} is required to convert
133+
* from text value to text value.
134+
*/
135+
boolean requiresStringToStringConversion();
136+
137+
/**
138+
* Converts from the internal/indexed (term) text to the actual string representation.
139+
* Can return <tt>null</tt> indicating that this is "uninteresting" value (for example, with
140+
* numbers). Useful for example when enumerating terms. See {@link #shouldBreakTermEnumeration(String)}.
141+
*/
142+
String valueAsString(String text);
143+
144+
/**
145+
* Return <tt>true</tt> if this term value indicates breaking out of term enumeration on this
146+
* field. The term text passed is the one returned from {@link #valueAsString(String)}.
147+
*/
148+
boolean shouldBreakTermEnumeration(String text);
149+
131150
/**
132151
* Returns the indexed value.
133152
*/

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonBoostFieldMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ protected JsonBoostFieldMapper(String name, String indexName, int precisionStep,
110110
return NumericUtils.floatToPrefixCoded(value);
111111
}
112112

113+
@Override public String valueAsString(String text) {
114+
final int shift = text.charAt(0) - NumericUtils.SHIFT_START_INT;
115+
if (shift > 0 && shift <= 31) {
116+
return null;
117+
}
118+
return Float.toString(NumericUtils.prefixCodedToFloat(text));
119+
}
120+
113121
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
114122
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,
115123
lowerTerm == null ? null : Float.parseFloat(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDateFieldMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ protected JsonDateFieldMapper(Names names, FormatDateTimeFormatter dateTimeForma
115115
return NumericUtils.longToPrefixCoded(value);
116116
}
117117

118+
@Override public String valueAsString(String text) {
119+
final int shift = text.charAt(0) - NumericUtils.SHIFT_START_LONG;
120+
if (shift > 0 && shift <= 63) {
121+
return null;
122+
}
123+
return dateTimeFormatter.printer().print(NumericUtils.prefixCodedToLong(text));
124+
}
125+
118126
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
119127
return NumericRangeQuery.newLongRange(names.indexName(), precisionStep,
120128
lowerTerm == null ? null : dateTimeFormatter.parser().parseMillis(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDoubleFieldMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ protected JsonDoubleFieldMapper(Names names, int precisionStep,
9999
return NumericUtils.doubleToPrefixCoded(value);
100100
}
101101

102+
@Override public String valueAsString(String text) {
103+
final int shift = text.charAt(0) - NumericUtils.SHIFT_START_LONG;
104+
if (shift > 0 && shift <= 63) {
105+
return null;
106+
}
107+
return Double.toString(NumericUtils.prefixCodedToDouble(text));
108+
}
109+
102110
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
103111
return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep,
104112
lowerTerm == null ? null : Double.parseDouble(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ protected JsonFieldMapper(Names names, Field.Index index, Field.Store store, Fie
292292
return valueAsString(field);
293293
}
294294

295+
/**
296+
* Default base does not require stringToString conversion.
297+
*/
298+
@Override public boolean requiresStringToStringConversion() {
299+
return false;
300+
}
301+
302+
/**
303+
* Simply returns the same string.
304+
*/
305+
@Override public String valueAsString(String text) {
306+
return text;
307+
}
308+
309+
/**
310+
* Never break on this term enumeration value.
311+
*/
312+
@Override public boolean shouldBreakTermEnumeration(String text) {
313+
return false;
314+
}
315+
295316
@Override public String indexedValue(String value) {
296317
return value;
297318
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFloatFieldMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ protected JsonFloatFieldMapper(Names names, int precisionStep, Field.Index index
9999
return NumericUtils.floatToPrefixCoded(value);
100100
}
101101

102+
@Override public String valueAsString(String text) {
103+
final int shift = text.charAt(0) - NumericUtils.SHIFT_START_INT;
104+
if (shift > 0 && shift <= 31) {
105+
return null;
106+
}
107+
return Float.toString(NumericUtils.prefixCodedToFloat(text));
108+
}
109+
102110
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
103111
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,
104112
lowerTerm == null ? null : Float.parseFloat(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonIntegerFieldMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ protected JsonIntegerFieldMapper(Names names, int precisionStep, Field.Index ind
9898
return NumericUtils.intToPrefixCoded(value);
9999
}
100100

101+
@Override public String valueAsString(String text) {
102+
return Integer.toString(NumericUtils.prefixCodedToInt(text));
103+
}
104+
101105
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
102106
return NumericRangeQuery.newIntRange(names.indexName(), precisionStep,
103107
lowerTerm == null ? null : Integer.parseInt(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonLongFieldMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ protected JsonLongFieldMapper(Names names, int precisionStep, Field.Index index,
9898
return NumericUtils.longToPrefixCoded(value);
9999
}
100100

101+
@Override public String valueAsString(String text) {
102+
final int shift = text.charAt(0) - NumericUtils.SHIFT_START_LONG;
103+
if (shift > 0 && shift <= 63) {
104+
return null;
105+
}
106+
return Long.toString(NumericUtils.prefixCodedToLong(text));
107+
}
108+
101109
@Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) {
102110
return NumericRangeQuery.newLongRange(names.indexName(), precisionStep,
103111
lowerTerm == null ? null : Long.parseLong(lowerTerm),

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonNumberFieldMapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ public int precisionStep() {
129129
return value(field).toString();
130130
}
131131

132+
/**
133+
* Numbers require string conversion.
134+
*/
135+
@Override public boolean requiresStringToStringConversion() {
136+
return true;
137+
}
138+
139+
@Override public abstract String valueAsString(String text);
140+
141+
/**
142+
* Breaks on this text if its <tt>null</tt>.
143+
*/
144+
@Override public boolean shouldBreakTermEnumeration(String text) {
145+
return text == null;
146+
}
147+
132148
@Override protected void doJsonBody(JsonBuilder builder) throws IOException {
133149
super.doJsonBody(builder);
134150
builder.field("precisionStep", precisionStep);

0 commit comments

Comments
 (0)