Skip to content

Commit ffe1b02

Browse files
committed
Add “took” timing info to response for _msearch/template API
Closes #30957
1 parent cdbbbaf commit ffe1b02

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
package org.elasticsearch.script.mustache;
2121

2222
import org.elasticsearch.ElasticsearchException;
23+
import org.elasticsearch.Version;
2324
import org.elasticsearch.action.ActionResponse;
2425
import org.elasticsearch.common.Nullable;
2526
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.io.stream.StreamInput;
2728
import org.elasticsearch.common.io.stream.StreamOutput;
2829
import org.elasticsearch.common.io.stream.Streamable;
30+
import org.elasticsearch.common.unit.TimeValue;
2931
import org.elasticsearch.common.xcontent.ToXContent;
3032
import org.elasticsearch.common.xcontent.ToXContentObject;
3133
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -107,12 +109,14 @@ public Exception getFailure() {
107109
}
108110

109111
private Item[] items;
110-
112+
private long tookInMillis;
113+
111114
MultiSearchTemplateResponse() {
112115
}
113116

114-
public MultiSearchTemplateResponse(Item[] items) {
117+
public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
115118
this.items = items;
119+
this.tookInMillis = tookInMillis;
116120
}
117121

118122
@Override
@@ -126,6 +130,13 @@ public Iterator<Item> iterator() {
126130
public Item[] getResponses() {
127131
return this.items;
128132
}
133+
134+
/**
135+
* How long the msearch_template took.
136+
*/
137+
public TimeValue getTook() {
138+
return new TimeValue(tookInMillis);
139+
}
129140

130141
@Override
131142
public void readFrom(StreamInput in) throws IOException {
@@ -134,6 +145,9 @@ public void readFrom(StreamInput in) throws IOException {
134145
for (int i = 0; i < items.length; i++) {
135146
items[i] = Item.readItem(in);
136147
}
148+
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
149+
tookInMillis = in.readVLong();
150+
}
137151
}
138152

139153
@Override
@@ -143,11 +157,15 @@ public void writeTo(StreamOutput out) throws IOException {
143157
for (Item item : items) {
144158
item.writeTo(out);
145159
}
160+
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
161+
out.writeVLong(tookInMillis);
162+
}
146163
}
147164

148165
@Override
149166
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
150167
builder.startObject();
168+
builder.field("took", tookInMillis);
151169
builder.startArray(Fields.RESPONSES);
152170
for (Item item : items) {
153171
if (item.isFailure()) {

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportMultiSearchTemplateAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected void doExecute(MultiSearchTemplateRequest request, ActionListener<Mult
9494
items[originalSlot].getResponse().setResponse(item.getResponse());
9595
}
9696
}
97-
listener.onResponse(new MultiSearchTemplateResponse(items));
97+
listener.onResponse(new MultiSearchTemplateResponse(items, r.getTook().millis()));
9898
}, listener::onFailure));
9999
}
100100
}

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
3838
import static org.hamcrest.Matchers.arrayWithSize;
3939
import static org.hamcrest.Matchers.equalTo;
40+
import static org.hamcrest.Matchers.greaterThan;
4041
import static org.hamcrest.Matchers.instanceOf;
4142
import static org.hamcrest.core.Is.is;
4243

@@ -140,6 +141,7 @@ public void testBasic() throws Exception {
140141

141142
MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get();
142143
assertThat(response.getResponses(), arrayWithSize(5));
144+
assertThat(response.getTook().millis(), greaterThan(0l));
143145

144146
MultiSearchTemplateResponse.Item response1 = response.getResponses()[0];
145147
assertThat(response1.isFailure(), is(false));

0 commit comments

Comments
 (0)