Add “took” timing info to response for _msearch/template API (#30961)
Add “took” timing info to response for _msearch/template API Closes #30957
This commit is contained in:
parent
b716b08197
commit
facbb2b2dc
|
@ -20,12 +20,14 @@
|
|||
package org.elasticsearch.script.mustache;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionResponse;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.io.stream.Streamable;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.ToXContentObject;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -107,12 +109,14 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
|
|||
}
|
||||
|
||||
private Item[] items;
|
||||
private long tookInMillis;
|
||||
|
||||
MultiSearchTemplateResponse() {
|
||||
}
|
||||
|
||||
public MultiSearchTemplateResponse(Item[] items) {
|
||||
public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
|
||||
this.items = items;
|
||||
this.tookInMillis = tookInMillis;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -127,6 +131,13 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
|
|||
return this.items;
|
||||
}
|
||||
|
||||
/**
|
||||
* How long the msearch_template took.
|
||||
*/
|
||||
public TimeValue getTook() {
|
||||
return new TimeValue(tookInMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
|
@ -134,6 +145,9 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
|
|||
for (int i = 0; i < items.length; i++) {
|
||||
items[i] = Item.readItem(in);
|
||||
}
|
||||
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
|
||||
tookInMillis = in.readVLong();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -143,11 +157,15 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
|
|||
for (Item item : items) {
|
||||
item.writeTo(out);
|
||||
}
|
||||
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
|
||||
out.writeVLong(tookInMillis);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field("took", tookInMillis);
|
||||
builder.startArray(Fields.RESPONSES);
|
||||
for (Item item : items) {
|
||||
if (item.isFailure()) {
|
||||
|
|
|
@ -94,7 +94,7 @@ public class TransportMultiSearchTemplateAction extends HandledTransportAction<M
|
|||
items[originalSlot].getResponse().setResponse(item.getResponse());
|
||||
}
|
||||
}
|
||||
listener.onResponse(new MultiSearchTemplateResponse(items));
|
||||
listener.onResponse(new MultiSearchTemplateResponse(items, r.getTook().millis()));
|
||||
}, listener::onFailure));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
|
||||
|
@ -140,6 +141,7 @@ public class MultiSearchTemplateIT extends ESIntegTestCase {
|
|||
|
||||
MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get();
|
||||
assertThat(response.getResponses(), arrayWithSize(5));
|
||||
assertThat(response.getTook().millis(), greaterThan(0L));
|
||||
|
||||
MultiSearchTemplateResponse.Item response1 = response.getResponses()[0];
|
||||
assertThat(response1.isFailure(), is(false));
|
||||
|
|
Loading…
Reference in New Issue