This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
/
Carrot2Response.java
313 lines (272 loc) · 9.75 KB
/
Carrot2Response.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package org.elasticsearch.plugin.infinitbyte;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.Facets;
import org.elasticsearch.search.internal.InternalSearchResponse;
import java.io.IOException;
import static org.elasticsearch.action.search.ShardSearchFailure.readShardSearchFailure;
import static org.elasticsearch.search.internal.InternalSearchResponse.readInternalSearchResponse;
/**
* Created by IntelliJ IDEA.
* User: Medcl'
* Date: 3/20/12
* Time: 1:58 PM
*/
public class Carrot2Response extends SearchResponse {
protected final ESLogger logger= Loggers.getLogger(getClass());
public Carrot2Response(InternalSearchResponse internalResponse, String scrollId, int totalShards, int successfulShards, long tookInMillis, ShardSearchFailure[] shardFailures) {
this.internalResponse = internalResponse;
this.scrollId = scrollId;
this.totalShards = totalShards;
this.successfulShards = successfulShards;
this.tookInMillis = tookInMillis;
this.shardFailures = shardFailures;
}
public Carrot2Response(){}
private InternalSearchResponse internalResponse;
private String scrollId;
private int totalShards;
private int successfulShards;
private ShardSearchFailure[] shardFailures;
private long tookInMillis;
public RestStatus status() {
if (shardFailures.length == 0) {
return RestStatus.OK;
}
if (successfulShards == 0 && totalShards > 0) {
RestStatus status = shardFailures[0].status();
if (shardFailures.length > 1) {
for (int i = 1; i < shardFailures.length; i++) {
if (shardFailures[i].status().getStatus() >= 500) {
status = shardFailures[i].status();
}
}
}
return status;
}
return RestStatus.OK;
}
/**
* The search hits.
*/
public SearchHits hits() {
return internalResponse.hits();
}
/**
* The search hits.
*/
public SearchHits getHits() {
return hits();
}
/**
* The search facets.
*/
public Facets facets() {
return internalResponse.facets();
}
/**
* The search facets.
*/
public Facets getFacets() {
return facets();
}
/**
* Has the search operation timed out.
*/
public boolean timedOut() {
return internalResponse.timedOut();
}
/**
* Has the search operation timed out.
*/
public boolean isTimedOut() {
return timedOut();
}
/**
* How long the search took.
*/
public TimeValue took() {
return new TimeValue(tookInMillis);
}
/**
* How long the search took.
*/
public TimeValue getTook() {
return took();
}
/**
* How long the search took in milliseconds.
*/
public long tookInMillis() {
return tookInMillis;
}
/**
* How long the search took in milliseconds.
*/
public long getTookInMillis() {
return tookInMillis();
}
/**
* The total number of shards the search was executed on.
*/
public int totalShards() {
return totalShards;
}
/**
* The total number of shards the search was executed on.
*/
public int getTotalShards() {
return totalShards;
}
/**
* The successful number of shards the search was executed on.
*/
public int successfulShards() {
return successfulShards;
}
/**
* The successful number of shards the search was executed on.
*/
public int getSuccessfulShards() {
return successfulShards;
}
/**
* The failed number of shards the search was executed on.
*/
public int failedShards() {
return totalShards - successfulShards;
}
/**
* The failed number of shards the search was executed on.
*/
public int getFailedShards() {
return failedShards();
}
/**
* The failures that occurred during the search.
*/
public ShardSearchFailure[] shardFailures() {
return this.shardFailures;
}
/**
* The failures that occurred during the search.
*/
public ShardSearchFailure[] getShardFailures() {
return shardFailures;
}
/**
* If scrolling was enabled ({@link org.elasticsearch.action.search.SearchRequest#scroll(org.elasticsearch.search.Scroll)}, the
* scroll id that can be used to continue scrolling.
*/
public String scrollId() {
return scrollId;
}
/**
* If scrolling was enabled ({@link org.elasticsearch.action.search.SearchRequest#scroll(org.elasticsearch.search.Scroll)}, the
* scroll id that can be used to continue scrolling.
*/
public String getScrollId() {
return scrollId;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (scrollId != null) {
builder.field(Fields._SCROLL_ID, scrollId);
}
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut());
builder.startObject(Fields._SHARDS);
builder.field(Fields.TOTAL, totalShards());
builder.field(Fields.SUCCESSFUL, successfulShards());
builder.field(Fields.FAILED, failedShards());
if (shardFailures.length > 0) {
builder.startArray(Fields.FAILURES);
for (ShardSearchFailure shardFailure : shardFailures) {
builder.startObject();
if (shardFailure.shard() != null) {
builder.field(Fields.INDEX, shardFailure.shard().index());
builder.field(Fields.SHARD, shardFailure.shard().shardId());
}
builder.field(Fields.STATUS, shardFailure.status().getStatus());
builder.field(Fields.REASON, shardFailure.reason());
builder.endObject();
}
builder.endArray();
}
builder.endObject();
internalResponse.toXContent(builder, params);
return builder;
}
static final class Fields {
static final XContentBuilderString _SCROLL_ID = new XContentBuilderString("_scroll_id");
static final XContentBuilderString _SHARDS = new XContentBuilderString("_shards");
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
static final XContentBuilderString SUCCESSFUL = new XContentBuilderString("successful");
static final XContentBuilderString FAILED = new XContentBuilderString("failed");
static final XContentBuilderString FAILURES = new XContentBuilderString("failures");
static final XContentBuilderString STATUS = new XContentBuilderString("status");
static final XContentBuilderString INDEX = new XContentBuilderString("index");
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
static final XContentBuilderString REASON = new XContentBuilderString("reason");
static final XContentBuilderString TOOK = new XContentBuilderString("took");
static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out");
}
@Override
public void readFrom(StreamInput in) throws IOException {
internalResponse = readInternalSearchResponse(in);
totalShards = in.readVInt();
successfulShards = in.readVInt();
int size = in.readVInt();
if (size == 0) {
shardFailures = ShardSearchFailure.EMPTY_ARRAY;
} else {
shardFailures = new ShardSearchFailure[size];
for (int i = 0; i < shardFailures.length; i++) {
shardFailures[i] = readShardSearchFailure(in);
}
}
if (in.readBoolean()) {
scrollId = in.readUTF();
}
tookInMillis = in.readVLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
internalResponse.writeTo(out);
out.writeVInt(totalShards);
out.writeVInt(successfulShards);
out.writeVInt(shardFailures.length);
for (ShardSearchFailure shardSearchFailure : shardFailures) {
shardSearchFailure.writeTo(out);
}
if (scrollId == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(scrollId);
}
out.writeVLong(tookInMillis);
}
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
builder.startObject();
toXContent(builder, EMPTY_PARAMS);
builder.endObject();
return builder.string();
} catch (IOException e) {
return "{ \"error\" : \"" + e.getMessage() + "\"}";
}
}
}