Skip to content

Commit 5d78196

Browse files
committed
Terms API: Allow to get terms for one or more field. Closes #21.
1 parent 06cbc0a commit 5d78196

File tree

56 files changed

+2320
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2320
-102
lines changed

.idea/dictionaries/kimchy.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.action.search.TransportSearchAction;
4848
import org.elasticsearch.action.search.TransportSearchScrollAction;
4949
import org.elasticsearch.action.search.type.*;
50+
import org.elasticsearch.action.terms.TransportTermsAction;
5051

5152
/**
5253
* @author kimchy (Shay Banon)
@@ -80,12 +81,13 @@ public class TransportActionModule extends AbstractModule {
8081
bind(TransportIndexAction.class).asEagerSingleton();
8182
bind(TransportGetAction.class).asEagerSingleton();
8283
bind(TransportDeleteAction.class).asEagerSingleton();
84+
bind(TransportCountAction.class).asEagerSingleton();
85+
bind(TransportTermsAction.class).asEagerSingleton();
8386

8487
bind(TransportShardDeleteByQueryAction.class).asEagerSingleton();
8588
bind(TransportIndexDeleteByQueryAction.class).asEagerSingleton();
8689
bind(TransportDeleteByQueryAction.class).asEagerSingleton();
8790

88-
bind(TransportCountAction.class).asEagerSingleton();
8991

9092
bind(TransportSearchCache.class).asEagerSingleton();
9193
bind(TransportSearchDfsQueryThenFetchAction.class).asEagerSingleton();

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class TransportActions {
3838

3939
public static final String SEARCH_SCROLL = "indices/searchScroll";
4040

41+
public static final String TERMS = "indices/terms";
42+
4143
public static class Admin {
4244

4345
public static class Indices {

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/status/IndexStatus.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public class IndexStatus implements Iterable<IndexShardStatus> {
3737
public static class Docs {
3838
public static final Docs UNKNOWN = new Docs();
3939

40-
int numDocs = -1;
41-
int maxDoc = -1;
42-
int deletedDocs = -1;
40+
long numDocs = -1;
41+
long maxDoc = -1;
42+
long deletedDocs = -1;
4343

44-
public int numDocs() {
44+
public long numDocs() {
4545
return numDocs;
4646
}
4747

48-
public int maxDoc() {
48+
public long maxDoc() {
4949
return maxDoc;
5050
}
5151

52-
public int deletedDocs() {
52+
public long deletedDocs() {
5353
return deletedDocs;
5454
}
5555
}
@@ -83,6 +83,10 @@ public String index() {
8383
return this.index;
8484
}
8585

86+
/**
87+
* A shard id to index shard status map (note, index shard status is the replication shard group that maps
88+
* to the shard id).
89+
*/
8690
public Map<Integer, IndexShardStatus> shards() {
8791
return this.indexShards;
8892
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ public SearchRequest size(int size) {
290290
} else {
291291
out.writeInt(queryBoost.size());
292292
for (TObjectFloatIterator<String> it = queryBoost.iterator(); it.hasNext();) {
293+
it.advance();
293294
out.writeUTF(it.key());
294295
out.writeFloat(it.value());
295-
it.advance();
296296
}
297297
}
298298
out.writeInt(types.length);

modules/elasticsearch/src/main/java/org/elasticsearch/action/support/broadcast/TransportBroadcastOperationAction.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,19 @@ private void finishHim(boolean alreadyThreaded) {
254254
if (request.listenerThreaded() && !alreadyThreaded) {
255255
threadPool.execute(new Runnable() {
256256
@Override public void run() {
257-
listener.onResponse(newResponse(request, shardsResponses, clusterState));
257+
try {
258+
listener.onResponse(newResponse(request, shardsResponses, clusterState));
259+
} catch (Exception e) {
260+
listener.onFailure(e);
261+
}
258262
}
259263
});
260264
} else {
261-
listener.onResponse(newResponse(request, shardsResponses, clusterState));
265+
try {
266+
listener.onResponse(newResponse(request, shardsResponses, clusterState));
267+
} catch (Exception e) {
268+
listener.onFailure(e);
269+
}
262270
}
263271
}
264272
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.action.terms;
21+
22+
import com.google.common.collect.Iterators;
23+
import org.elasticsearch.util.io.Streamable;
24+
import org.elasticsearch.util.trove.ExtTObjectIntHasMap;
25+
26+
import java.io.DataInput;
27+
import java.io.DataOutput;
28+
import java.io.IOException;
29+
import java.util.Iterator;
30+
31+
import static org.elasticsearch.action.terms.TermFreq.*;
32+
33+
/**
34+
* @author kimchy (Shay Banon)
35+
*/
36+
public class FieldTermsFreq implements Streamable, Iterable<TermFreq> {
37+
38+
private String fieldName;
39+
40+
private TermFreq[] termsFreqs;
41+
42+
private transient ExtTObjectIntHasMap<String> termsFreqMap;
43+
44+
private FieldTermsFreq() {
45+
46+
}
47+
48+
public FieldTermsFreq(String fieldName, TermFreq[] termsFreqs) {
49+
this.fieldName = fieldName;
50+
this.termsFreqs = termsFreqs;
51+
}
52+
53+
public String fieldName() {
54+
return this.fieldName;
55+
}
56+
57+
public TermFreq[] termsFreqs() {
58+
return this.termsFreqs;
59+
}
60+
61+
/**
62+
* Returns the document frequency of a term, <tt>-1</tt> if the term does not exists.
63+
*/
64+
public int docFreq(String term) {
65+
if (termsFreqMap == null) {
66+
ExtTObjectIntHasMap<String> termsFreqMap = new ExtTObjectIntHasMap<String>().defaultReturnValue(-1);
67+
for (TermFreq termFreq : termsFreqs) {
68+
termsFreqMap.put(termFreq.term(), termFreq.docFreq());
69+
}
70+
this.termsFreqMap = termsFreqMap;
71+
}
72+
return termsFreqMap.get(term);
73+
}
74+
75+
@Override public Iterator<TermFreq> iterator() {
76+
return Iterators.forArray(termsFreqs);
77+
}
78+
79+
public static FieldTermsFreq readFieldTermsFreq(DataInput in) throws IOException, ClassNotFoundException {
80+
FieldTermsFreq fieldTermsFreq = new FieldTermsFreq();
81+
fieldTermsFreq.readFrom(in);
82+
return fieldTermsFreq;
83+
}
84+
85+
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
86+
fieldName = in.readUTF();
87+
termsFreqs = new TermFreq[in.readInt()];
88+
for (int i = 0; i < termsFreqs.length; i++) {
89+
termsFreqs[i] = readTermFreq(in);
90+
}
91+
}
92+
93+
@Override public void writeTo(DataOutput out) throws IOException {
94+
out.writeUTF(fieldName);
95+
out.writeInt(termsFreqs.length);
96+
for (TermFreq termFreq : termsFreqs) {
97+
termFreq.writeTo(out);
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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.action.terms;
21+
22+
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
23+
24+
import java.io.DataInput;
25+
import java.io.DataOutput;
26+
import java.io.IOException;
27+
28+
/**
29+
* @author kimchy (Shay Banon)
30+
*/
31+
public class ShardTermsRequest extends BroadcastShardOperationRequest {
32+
33+
private String[] fields;
34+
35+
private String from;
36+
37+
private String to;
38+
39+
private boolean fromInclusive = true;
40+
41+
private boolean toInclusive = false;
42+
43+
private String prefix;
44+
45+
private String regexp;
46+
47+
private int size = 10;
48+
49+
private boolean convert = true;
50+
51+
private TermsRequest.SortType sortType;
52+
53+
private boolean exact = false;
54+
55+
ShardTermsRequest() {
56+
}
57+
58+
public ShardTermsRequest(String index, int shardId, TermsRequest request) {
59+
super(index, shardId);
60+
this.fields = request.fields();
61+
this.from = request.from();
62+
this.to = request.to();
63+
this.fromInclusive = request.fromInclusive();
64+
this.toInclusive = request.toInclusive();
65+
this.prefix = request.prefix();
66+
this.regexp = request.regexp();
67+
this.size = request.size();
68+
this.convert = request.convert();
69+
this.sortType = request.sortType();
70+
this.exact = request.exact();
71+
}
72+
73+
public String[] fields() {
74+
return fields;
75+
}
76+
77+
public String from() {
78+
return from;
79+
}
80+
81+
public String to() {
82+
return to;
83+
}
84+
85+
public boolean fromInclusive() {
86+
return fromInclusive;
87+
}
88+
89+
public boolean toInclusive() {
90+
return toInclusive;
91+
}
92+
93+
public String prefix() {
94+
return prefix;
95+
}
96+
97+
public String regexp() {
98+
return regexp;
99+
}
100+
101+
public int size() {
102+
return size;
103+
}
104+
105+
public boolean convert() {
106+
return convert;
107+
}
108+
109+
public TermsRequest.SortType sortType() {
110+
return sortType;
111+
}
112+
113+
public boolean exact() {
114+
return this.exact;
115+
}
116+
117+
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
118+
super.readFrom(in);
119+
fields = new String[in.readInt()];
120+
for (int i = 0; i < fields.length; i++) {
121+
fields[i] = in.readUTF();
122+
}
123+
if (in.readBoolean()) {
124+
from = in.readUTF();
125+
}
126+
if (in.readBoolean()) {
127+
to = in.readUTF();
128+
}
129+
fromInclusive = in.readBoolean();
130+
toInclusive = in.readBoolean();
131+
if (in.readBoolean()) {
132+
prefix = in.readUTF();
133+
}
134+
if (in.readBoolean()) {
135+
regexp = in.readUTF();
136+
}
137+
size = in.readInt();
138+
convert = in.readBoolean();
139+
sortType = TermsRequest.SortType.fromValue(in.readByte());
140+
exact = in.readBoolean();
141+
}
142+
143+
@Override public void writeTo(DataOutput out) throws IOException {
144+
super.writeTo(out);
145+
out.writeInt(fields.length);
146+
for (String field : fields) {
147+
out.writeUTF(field);
148+
}
149+
if (from == null) {
150+
out.writeBoolean(false);
151+
} else {
152+
out.writeBoolean(true);
153+
out.writeUTF(from);
154+
}
155+
if (to == null) {
156+
out.writeBoolean(false);
157+
} else {
158+
out.writeBoolean(true);
159+
out.writeUTF(to);
160+
}
161+
out.writeBoolean(fromInclusive);
162+
out.writeBoolean(toInclusive);
163+
if (prefix == null) {
164+
out.writeBoolean(false);
165+
} else {
166+
out.writeBoolean(true);
167+
out.writeUTF(prefix);
168+
}
169+
if (regexp == null) {
170+
out.writeBoolean(false);
171+
} else {
172+
out.writeBoolean(true);
173+
out.writeUTF(regexp);
174+
}
175+
out.writeInt(size);
176+
out.writeBoolean(convert);
177+
out.writeByte(sortType.value());
178+
out.writeBoolean(exact);
179+
}
180+
}

0 commit comments

Comments
 (0)