Skip to content

Commit 93e0253

Browse files
committed
Terms API: Support numbers/dates, closes #78.
1 parent bc03d89 commit 93e0253

19 files changed

+650
-339
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FieldTermsFreq implements Streamable, Iterable<TermFreq> {
4141

4242
private TermFreq[] termsFreqs;
4343

44-
private transient ExtTObjectIntHasMap<String> termsFreqMap;
44+
private transient ExtTObjectIntHasMap<Object> termsFreqMap;
4545

4646
private FieldTermsFreq() {
4747

@@ -69,15 +69,16 @@ public TermFreq[] termsFreqs() {
6969
/**
7070
* Returns the document frequency of a term, <tt>-1</tt> if the term does not exists.
7171
*/
72-
public int docFreq(String term) {
72+
public int docFreq(Object term) {
73+
// we use "toString" on the term so we get hits when we the termValue is Long, and we lookup with int
7374
if (termsFreqMap == null) {
74-
ExtTObjectIntHasMap<String> termsFreqMap = new ExtTObjectIntHasMap<String>().defaultReturnValue(-1);
75+
ExtTObjectIntHasMap<Object> termsFreqMap = new ExtTObjectIntHasMap<Object>().defaultReturnValue(-1);
7576
for (TermFreq termFreq : termsFreqs) {
76-
termsFreqMap.put(termFreq.term(), termFreq.docFreq());
77+
termsFreqMap.put(termFreq.term().toString(), termFreq.docFreq());
7778
}
7879
this.termsFreqMap = termsFreqMap;
7980
}
80-
return termsFreqMap.get(term);
81+
return termsFreqMap.get(term.toString());
8182
}
8283

8384
@Override public Iterator<TermFreq> iterator() {

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class ShardTermsRequest extends BroadcastShardOperationRequest {
4646

4747
private int size = 10;
4848

49-
private boolean convert = true;
50-
5149
private TermsRequest.SortType sortType;
5250

5351
private boolean exact = false;
@@ -65,7 +63,6 @@ public ShardTermsRequest(String index, int shardId, TermsRequest request) {
6563
this.prefix = request.prefix();
6664
this.regexp = request.regexp();
6765
this.size = request.size();
68-
this.convert = request.convert();
6966
this.sortType = request.sortType();
7067
this.exact = request.exact();
7168
}
@@ -102,10 +99,6 @@ public int size() {
10299
return size;
103100
}
104101

105-
public boolean convert() {
106-
return convert;
107-
}
108-
109102
public TermsRequest.SortType sortType() {
110103
return sortType;
111104
}
@@ -135,7 +128,6 @@ public boolean exact() {
135128
regexp = in.readUTF();
136129
}
137130
size = in.readVInt();
138-
convert = in.readBoolean();
139131
sortType = TermsRequest.SortType.fromValue(in.readByte());
140132
exact = in.readBoolean();
141133
}
@@ -173,7 +165,6 @@ public boolean exact() {
173165
out.writeUTF(regexp);
174166
}
175167
out.writeVInt(size);
176-
out.writeBoolean(convert);
177168
out.writeByte(sortType.value());
178169
out.writeBoolean(exact);
179170
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.util.gnu.trove.TObjectIntIterator;
2525
import org.elasticsearch.util.io.stream.StreamInput;
2626
import org.elasticsearch.util.io.stream.StreamOutput;
27+
import org.elasticsearch.util.lucene.Lucene;
2728

2829
import java.io.IOException;
2930
import java.util.HashMap;
@@ -34,7 +35,7 @@
3435
*/
3536
class ShardTermsResponse extends BroadcastShardOperationResponse {
3637

37-
private Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs = new HashMap<String, TObjectIntHashMap<String>>();
38+
private Map<String, TObjectIntHashMap<Object>> fieldsTermsFreqs = new HashMap<String, TObjectIntHashMap<Object>>();
3839

3940
private int numDocs;
4041

@@ -64,11 +65,11 @@ int numDeletedDocs() {
6465
return this.numDeletedDocs;
6566
}
6667

67-
void put(String fieldName, TObjectIntHashMap<String> termsFreqs) {
68+
void put(String fieldName, TObjectIntHashMap<Object> termsFreqs) {
6869
fieldsTermsFreqs.put(fieldName, termsFreqs);
6970
}
7071

71-
Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
72+
Map<String, TObjectIntHashMap<Object>> fieldsTermsFreqs() {
7273
return fieldsTermsFreqs;
7374
}
7475

@@ -81,10 +82,10 @@ Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
8182
for (int i = 0; i < size; i++) {
8283
String fieldName = in.readUTF();
8384

84-
TObjectIntHashMap<String> termsFreq = new TObjectIntHashMap<String>();
85+
TObjectIntHashMap<Object> termsFreq = new TObjectIntHashMap<Object>();
8586
int size1 = in.readVInt();
8687
for (int j = 0; j < size1; j++) {
87-
termsFreq.put(in.readUTF(), in.readVInt());
88+
termsFreq.put(Lucene.readFieldValue(in), in.readVInt());
8889
}
8990

9091
fieldsTermsFreqs.put(fieldName, termsFreq);
@@ -97,12 +98,12 @@ Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
9798
out.writeVInt(maxDoc);
9899
out.writeVInt(numDeletedDocs);
99100
out.writeVInt(fieldsTermsFreqs.size());
100-
for (Map.Entry<String, TObjectIntHashMap<String>> entry : fieldsTermsFreqs.entrySet()) {
101+
for (Map.Entry<String, TObjectIntHashMap<Object>> entry : fieldsTermsFreqs.entrySet()) {
101102
out.writeUTF(entry.getKey());
102103
out.writeVInt(entry.getValue().size());
103-
for (TObjectIntIterator<String> it = entry.getValue().iterator(); it.hasNext();) {
104+
for (TObjectIntIterator<Object> it = entry.getValue().iterator(); it.hasNext();) {
104105
it.advance();
105-
out.writeUTF(it.key());
106+
Lucene.writeFieldValue(out, it.key());
106107
out.writeVInt(it.value());
107108
}
108109
}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.util.io.stream.StreamInput;
2323
import org.elasticsearch.util.io.stream.StreamOutput;
2424
import org.elasticsearch.util.io.stream.Streamable;
25+
import org.elasticsearch.util.lucene.Lucene;
2526

2627
import java.io.IOException;
2728
import java.util.Comparator;
@@ -40,7 +41,7 @@ public class TermFreq implements Streamable {
4041
@Override public int compare(TermFreq o1, TermFreq o2) {
4142
int i = o2.docFreq() - o1.docFreq();
4243
if (i == 0) {
43-
i = o1.term().compareTo(o2.term());
44+
i = ((Comparable) o1.term()).compareTo(o2.term());
4445
}
4546
return i;
4647
}
@@ -51,7 +52,7 @@ public class TermFreq implements Streamable {
5152
*/
5253
private static final Comparator<TermFreq> termComparator = new Comparator<TermFreq>() {
5354
@Override public int compare(TermFreq o1, TermFreq o2) {
54-
int i = o1.term().compareTo(o2.term());
55+
int i = ((Comparable) o1.term()).compareTo(o2.term());
5556
if (i == 0) {
5657
i = o1.docFreq() - o2.docFreq();
5758
}
@@ -73,7 +74,7 @@ public static Comparator<TermFreq> termComparator() {
7374
return termComparator;
7475
}
7576

76-
private String term;
77+
private Object term;
7778

7879
private int docFreq;
7980

@@ -87,18 +88,22 @@ private TermFreq() {
8788
* @param term The term
8889
* @param docFreq The document frequency
8990
*/
90-
TermFreq(String term, int docFreq) {
91+
TermFreq(Object term, int docFreq) {
9192
this.term = term;
9293
this.docFreq = docFreq;
9394
}
9495

9596
/**
9697
* The term.
9798
*/
98-
public String term() {
99+
public Object term() {
99100
return term;
100101
}
101102

103+
public String termAsString() {
104+
return term.toString();
105+
}
106+
102107
/**
103108
* The document frequency of the term (in how many documents this term exists).
104109
*/
@@ -113,12 +118,12 @@ public static TermFreq readTermFreq(StreamInput in) throws IOException {
113118
}
114119

115120
@Override public void readFrom(StreamInput in) throws IOException {
116-
term = in.readUTF();
121+
term = Lucene.readFieldValue(in);
117122
docFreq = in.readVInt();
118123
}
119124

120125
@Override public void writeTo(StreamOutput out) throws IOException {
121-
out.writeUTF(term);
126+
Lucene.writeFieldValue(out, term);
122127
out.writeVInt(docFreq);
123128
}
124129
}

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

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,21 @@ public String from() {
179179
* The lower bound (lex) term from which the iteration will start. Defaults to start from the
180180
* first.
181181
*/
182-
public TermsRequest from(String from) {
183-
this.from = from;
182+
public TermsRequest from(Object from) {
183+
this.from = from.toString();
184184
return this;
185185
}
186186

187187
/**
188-
* Should the first from (if set using {@link #from(String)} be inclusive or not. Defaults
188+
* Should the first from (if set using {@link #from(Object)} be inclusive or not. Defaults
189189
* to <tt>false</tt> (not inclusive / exclusive).
190190
*/
191191
public boolean fromInclusive() {
192192
return fromInclusive;
193193
}
194194

195195
/**
196-
* Should the first from (if set using {@link #from(String)} be inclusive or not. Defaults
196+
* Should the first from (if set using {@link #from(Object)} be inclusive or not. Defaults
197197
* to <tt>false</tt> (not inclusive / exclusive).
198198
*/
199199
public TermsRequest fromInclusive(boolean fromInclusive) {
@@ -211,21 +211,21 @@ public String to() {
211211
/**
212212
* The upper bound (lex) term to which the iteration will end. Defaults to unbound (<tt>null</tt>).
213213
*/
214-
public TermsRequest to(String to) {
215-
this.to = to;
214+
public TermsRequest to(Object to) {
215+
this.to = to.toString();
216216
return this;
217217
}
218218

219219
/**
220-
* Should the last to (if set using {@link #to(String)} be inclusive or not. Defaults to
220+
* Should the last to (if set using {@link #to(Object)} be inclusive or not. Defaults to
221221
* <tt>true</tt>.
222222
*/
223223
public boolean toInclusive() {
224224
return toInclusive;
225225
}
226226

227227
/**
228-
* Should the last to (if set using {@link #to(String)} be inclusive or not. Defaults to
228+
* Should the last to (if set using {@link #to(Object)} be inclusive or not. Defaults to
229229
* <tt>true</tt>.
230230
*/
231231
public TermsRequest toInclusive(boolean toInclusive) {
@@ -309,23 +309,6 @@ public TermsRequest size(int size) {
309309
return this;
310310
}
311311

312-
/**
313-
* Should an attempt be made to convert the {@link #to(String)} and {@link #from(String)}.
314-
* Defaults to <tt>true</tt>.
315-
*/
316-
public boolean convert() {
317-
return convert;
318-
}
319-
320-
/**
321-
* Should an attempt be made to convert the {@link #to(String)} and {@link #from(String)}.
322-
* Defaults to <tt>true</tt>.
323-
*/
324-
public TermsRequest convert(boolean convert) {
325-
this.convert = convert;
326-
return this;
327-
}
328-
329312
/**
330313
* The type of sorting for term / doc freq. Can either sort on term (lex) or doc frequency. Defaults to
331314
* {@link TermsRequest.SortType#TERM}.

0 commit comments

Comments
 (0)