Skip to content

Commit 77ec78c

Browse files
committed
Add support for retrieving request executionDuration.
Added support for retreving the query execution time from took() method of ResponseBody class as java.time.Duration in SearchDocumentResponseBuilder class. Closes #2986
1 parent 24b2a8a commit 77ec78c

File tree

15 files changed

+122
-69
lines changed

15 files changed

+122
-69
lines changed

src/main/antora/modules/ROOT/pages/elasticsearch/template.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ When a document is retrieved with the methods of the `DocumentOperations` inter
8181
When searching with the methods of the `SearchOperations` interface, additional information is available for each entity, for example the _score_ or the _sortValues_ of the found entity.
8282

8383
In order to return this information, each entity is wrapped in a `SearchHit` object that contains this entity-specific additional information.
84-
These `SearchHit` objects themselves are returned within a `SearchHits` object which additionally contains informations about the whole search like the _maxScore_ or requested aggregations.
84+
These `SearchHit` objects themselves are returned within a `SearchHits` object which additionally contains informations about the whole search like the _maxScore_ or requested aggregations or the execution duration it took to complete the request.
8585
The following classes and interfaces are now available:
8686

8787
.SearchHit<T>

src/main/java/org/springframework/data/elasticsearch/client/elc/DocumentAdapters.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Peter-Josef Meisch
5252
* @author Haibo Liu
53+
* @author Mohamed El Harrougui
5354
* @since 4.4
5455
*/
5556
final class DocumentAdapters {
@@ -74,7 +75,7 @@ public static SearchDocument from(Hit<?> hit, JsonpMapper jsonpMapper) {
7475
Map<String, SearchDocumentResponse> innerHits = new LinkedHashMap<>();
7576
hit.innerHits().forEach((name, innerHitsResult) -> {
7677
// noinspection ReturnOfNull
77-
innerHits.put(name, SearchDocumentResponseBuilder.from(innerHitsResult.hits(), null, null, null, null, null,
78+
innerHits.put(name, SearchDocumentResponseBuilder.from(innerHitsResult.hits(), null, null, null, 0, null, null,
7879
searchDocument -> null, jsonpMapper));
7980
});
8081

src/main/java/org/springframework/data/elasticsearch/client/elc/SearchDocumentResponseBuilder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import co.elastic.clients.elasticsearch.core.search.TotalHits;
3030
import co.elastic.clients.json.JsonpMapper;
3131

32+
import java.time.Duration;
3233
import java.util.ArrayList;
3334
import java.util.HashMap;
3435
import java.util.List;
@@ -56,6 +57,7 @@
5657
*
5758
* @author Peter-Josef Meisch
5859
* @author Haibo Liu
60+
* @author Mohamed El Harrougui
5961
* @since 4.4
6062
*/
6163
class SearchDocumentResponseBuilder {
@@ -83,8 +85,10 @@ public static <T> SearchDocumentResponse from(ResponseBody<EntityAsMap> response
8385
Map<String, List<Suggestion<EntityAsMap>>> suggest = responseBody.suggest();
8486
var pointInTimeId = responseBody.pitId();
8587
var shards = responseBody.shards();
88+
var executionDurationInMillis = responseBody.took();
8689

87-
return from(hitsMetadata, shards, scrollId, pointInTimeId, aggregations, suggest, entityCreator, jsonpMapper);
90+
return from(hitsMetadata, shards, scrollId, pointInTimeId, executionDurationInMillis, aggregations, suggest,
91+
entityCreator, jsonpMapper);
8892
}
8993

9094
/**
@@ -109,8 +113,10 @@ public static <T> SearchDocumentResponse from(SearchTemplateResponse<EntityAsMap
109113
var aggregations = response.aggregations();
110114
var suggest = response.suggest();
111115
var pointInTimeId = response.pitId();
116+
var executionDurationInMillis = response.took();
112117

113-
return from(hitsMetadata, shards, scrollId, pointInTimeId, aggregations, suggest, entityCreator, jsonpMapper);
118+
return from(hitsMetadata, shards, scrollId, pointInTimeId, executionDurationInMillis, aggregations, suggest,
119+
entityCreator, jsonpMapper);
114120
}
115121

116122
/**
@@ -127,7 +133,7 @@ public static <T> SearchDocumentResponse from(SearchTemplateResponse<EntityAsMap
127133
* @return the {@link SearchDocumentResponse}
128134
*/
129135
public static <T> SearchDocumentResponse from(HitsMetadata<?> hitsMetadata, @Nullable ShardStatistics shards,
130-
@Nullable String scrollId, @Nullable String pointInTimeId, @Nullable Map<String, Aggregate> aggregations,
136+
@Nullable String scrollId, @Nullable String pointInTimeId, long executionDurationInMillis, @Nullable Map<String, Aggregate> aggregations,
131137
Map<String, List<Suggestion<EntityAsMap>>> suggestES, SearchDocumentResponse.EntityCreator<T> entityCreator,
132138
JsonpMapper jsonpMapper) {
133139

@@ -151,6 +157,8 @@ public static <T> SearchDocumentResponse from(HitsMetadata<?> hitsMetadata, @Nul
151157

152158
float maxScore = hitsMetadata.maxScore() != null ? hitsMetadata.maxScore().floatValue() : Float.NaN;
153159

160+
Duration executionDuration = Duration.ofMillis(executionDurationInMillis);
161+
154162
List<SearchDocument> searchDocuments = new ArrayList<>();
155163
for (Hit<?> hit : hitsMetadata.hits()) {
156164
searchDocuments.add(DocumentAdapters.from(hit, jsonpMapper));
@@ -163,7 +171,7 @@ public static <T> SearchDocumentResponse from(HitsMetadata<?> hitsMetadata, @Nul
163171

164172
SearchShardStatistics shardStatistics = shards != null ? shardsFrom(shards) : null;
165173

166-
return new SearchDocumentResponse(totalHits, totalHitsRelation, maxScore, scrollId, pointInTimeId, searchDocuments,
174+
return new SearchDocumentResponse(totalHits, totalHitsRelation, maxScore, executionDuration, scrollId, pointInTimeId, searchDocuments,
167175
aggregationsContainer, suggest, shardStatistics);
168176
}
169177

src/main/java/org/springframework/data/elasticsearch/core/ReactiveSearchHits.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import reactor.core.publisher.Flux;
1919

20+
import java.time.Duration;
21+
2022
import org.springframework.data.elasticsearch.core.suggest.response.Suggest;
2123
import org.springframework.lang.Nullable;
2224

@@ -25,6 +27,7 @@
2527
*
2628
* @param <T> the result data class.
2729
* @author Peter-Josef Meisch
30+
* @author Mohamed El Harrougui
2831
* @since 4.4
2932
*/
3033
public interface ReactiveSearchHits<T> {
@@ -37,6 +40,11 @@ public interface ReactiveSearchHits<T> {
3740

3841
float getMaxScore();
3942

43+
/**
44+
* @return the execution duration it took to complete the request
45+
*/
46+
Duration getExecutionDuration();
47+
4048
/**
4149
* @return the {@link SearchHit}s from the search result.
4250
*/

src/main/java/org/springframework/data/elasticsearch/core/ReactiveSearchHitsImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
import reactor.core.publisher.Flux;
1919

20+
import java.time.Duration;
21+
2022
import org.springframework.data.elasticsearch.core.suggest.response.Suggest;
2123
import org.springframework.lang.Nullable;
2224

2325
/**
2426
* @author Peter-Josef Meisch
27+
* @author Mohamed El Harrougui
2528
* @since 4.4
2629
*/
2730
public class ReactiveSearchHitsImpl<T> implements ReactiveSearchHits<T> {
@@ -58,6 +61,11 @@ public float getMaxScore() {
5861
return delegate.getMaxScore();
5962
}
6063

64+
@Override
65+
public Duration getExecutionDuration() {
66+
return delegate.getExecutionDuration();
67+
}
68+
6169
@Override
6270
public boolean hasSearchHits() {
6371
return delegate.hasSearchHits();

src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.time.Duration;
1819
import java.util.ArrayList;
1920
import java.util.LinkedHashMap;
2021
import java.util.LinkedList;
@@ -47,6 +48,7 @@
4748
* @author Sascha Woo
4849
* @author Jakob Hoeper
4950
* @author Haibo Liu
51+
* @author Mohamed El Harrougui
5052
* @since 4.0
5153
*/
5254
public class SearchHitMapping<T> {
@@ -87,6 +89,7 @@ private SearchHitsImpl<T> mapHitsFromResponse(SearchDocumentResponse searchDocum
8789
long totalHits = searchDocumentResponse.getTotalHits();
8890
SearchShardStatistics shardStatistics = searchDocumentResponse.getSearchShardStatistics();
8991
float maxScore = searchDocumentResponse.getMaxScore();
92+
Duration executionDuration = searchDocumentResponse.getExecutionDuration();
9093
String scrollId = searchDocumentResponse.getScrollId();
9194
String pointInTimeId = searchDocumentResponse.getPointInTimeId();
9295

@@ -104,8 +107,8 @@ private SearchHitsImpl<T> mapHitsFromResponse(SearchDocumentResponse searchDocum
104107
Suggest suggest = searchDocumentResponse.getSuggest();
105108
mapHitsInCompletionSuggestion(suggest);
106109

107-
return new SearchHitsImpl<>(totalHits, totalHitsRelation, maxScore, scrollId, pointInTimeId, searchHits,
108-
aggregations, suggest, shardStatistics);
110+
return new SearchHitsImpl<>(totalHits, totalHitsRelation, maxScore, executionDuration, scrollId, pointInTimeId,
111+
searchHits, aggregations, suggest, shardStatistics);
109112
}
110113

111114
@SuppressWarnings("unchecked")
@@ -238,6 +241,7 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
238241
return new SearchHitsImpl<>(searchHits.getTotalHits(),
239242
searchHits.getTotalHitsRelation(),
240243
searchHits.getMaxScore(),
244+
searchHits.getExecutionDuration(),
241245
scrollId,
242246
searchHits.getPointInTimeId(),
243247
convertedSearchHits,

src/main/java/org/springframework/data/elasticsearch/core/SearchHits.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.time.Duration;
1819
import java.util.Iterator;
1920
import java.util.List;
2021

@@ -28,6 +29,7 @@
2829
* @param <T> the result data class.
2930
* @author Sascha Woo
3031
* @author Haibo Liu
32+
* @author Mohamed El Harrougui
3133
* @since 4.0
3234
*/
3335
public interface SearchHits<T> extends Streamable<SearchHit<T>> {
@@ -43,6 +45,11 @@ public interface SearchHits<T> extends Streamable<SearchHit<T>> {
4345
*/
4446
float getMaxScore();
4547

48+
/**
49+
* @return the execution duration it took to complete the request
50+
*/
51+
Duration getExecutionDuration();
52+
4653
/**
4754
* @param index position in List.
4855
* @return the {@link SearchHit} at position {index}

src/main/java/org/springframework/data/elasticsearch/core/SearchHitsImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.time.Duration;
1819
import java.util.Collections;
1920
import java.util.List;
2021

@@ -30,13 +31,15 @@
3031
* @author Peter-Josef Meisch
3132
* @author Sascha Woo
3233
* @author Haibo Liu
34+
* @author Mohamed El Harrougui
3335
* @since 4.0
3436
*/
3537
public class SearchHitsImpl<T> implements SearchScrollHits<T> {
3638

3739
private final long totalHits;
3840
private final TotalHitsRelation totalHitsRelation;
3941
private final float maxScore;
42+
private final Duration executionDuration;
4043
@Nullable private final String scrollId;
4144
private final List<? extends SearchHit<T>> searchHits;
4245
private final Lazy<List<SearchHit<T>>> unmodifiableSearchHits;
@@ -49,12 +52,13 @@ public class SearchHitsImpl<T> implements SearchScrollHits<T> {
4952
* @param totalHits the number of total hits for the search
5053
* @param totalHitsRelation the relation {@see TotalHitsRelation}, must not be {@literal null}
5154
* @param maxScore the maximum score
55+
* @param executionDuration the execution duration it took to complete the request
5256
* @param scrollId the scroll id if available
5357
* @param searchHits must not be {@literal null}
5458
* @param aggregations the aggregations if available
5559
*/
56-
public SearchHitsImpl(long totalHits, TotalHitsRelation totalHitsRelation, float maxScore, @Nullable String scrollId,
57-
@Nullable String pointInTimeId, List<? extends SearchHit<T>> searchHits,
60+
public SearchHitsImpl(long totalHits, TotalHitsRelation totalHitsRelation, float maxScore, Duration executionDuration,
61+
@Nullable String scrollId, @Nullable String pointInTimeId, List<? extends SearchHit<T>> searchHits,
5862
@Nullable AggregationsContainer<?> aggregations, @Nullable Suggest suggest,
5963
@Nullable SearchShardStatistics searchShardStatistics) {
6064

@@ -63,6 +67,7 @@ public SearchHitsImpl(long totalHits, TotalHitsRelation totalHitsRelation, float
6367
this.totalHits = totalHits;
6468
this.totalHitsRelation = totalHitsRelation;
6569
this.maxScore = maxScore;
70+
this.executionDuration = executionDuration;
6671
this.scrollId = scrollId;
6772
this.pointInTimeId = pointInTimeId;
6873
this.searchHits = searchHits;
@@ -88,6 +93,11 @@ public float getMaxScore() {
8893
return maxScore;
8994
}
9095

96+
@Override
97+
public Duration getExecutionDuration() {
98+
return executionDuration;
99+
}
100+
91101
@Override
92102
@Nullable
93103
public String getScrollId() {
@@ -133,6 +143,7 @@ public String toString() {
133143
"totalHits=" + totalHits + //
134144
", totalHitsRelation=" + totalHitsRelation + //
135145
", maxScore=" + maxScore + //
146+
", executionDuration=" + executionDuration + //
136147
", scrollId='" + scrollId + '\'' + //
137148
", pointInTimeId='" + pointInTimeId + '\'' + //
138149
", searchHits={" + searchHits.size() + " elements}" + //

src/main/java/org/springframework/data/elasticsearch/core/SearchHitsIterator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.time.Duration;
19+
1820
import org.springframework.data.util.CloseableIterator;
1921
import org.springframework.lang.Nullable;
2022

@@ -23,6 +25,7 @@
2325
* {@link java.util.stream.Stream}.
2426
*
2527
* @author Sascha Woo
28+
* @author Mohamed El Harrougui
2629
* @param <T>
2730
* @since 4.0
2831
*/
@@ -39,6 +42,11 @@ public interface SearchHitsIterator<T> extends CloseableIterator<SearchHit<T>> {
3942
*/
4043
float getMaxScore();
4144

45+
/**
46+
* @return the execution duration it took to complete the request
47+
*/
48+
Duration getExecutionDuration();
49+
4250
/**
4351
* @return the number of total hits.
4452
*/

src/main/java/org/springframework/data/elasticsearch/core/StreamQueries.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.time.Duration;
1819
import java.util.Iterator;
1920
import java.util.List;
2021
import java.util.NoSuchElementException;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Mark Paluch
3334
* @author Sascha Woo
35+
* @author Mohamed El Harrougui
3436
* @since 3.2
3537
*/
3638
abstract class StreamQueries {
@@ -56,6 +58,7 @@ static <T> SearchHitsIterator<T> streamResults(int maxCount, SearchScrollHits<T>
5658

5759
AggregationsContainer<?> aggregations = searchHits.getAggregations();
5860
float maxScore = searchHits.getMaxScore();
61+
Duration executionDuration = searchHits.getExecutionDuration();
5962
long totalHits = searchHits.getTotalHits();
6063
TotalHitsRelation totalHitsRelation = searchHits.getTotalHitsRelation();
6164

@@ -86,6 +89,11 @@ public float getMaxScore() {
8689
return maxScore;
8790
}
8891

92+
@Override
93+
public Duration getExecutionDuration() {
94+
return executionDuration;
95+
}
96+
8997
@Override
9098
public long getTotalHits() {
9199
return totalHits;

0 commit comments

Comments
 (0)