1
1
/*
2
- * Copyright 2019-2021 the original author or authors.
2
+ * Copyright 2019-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .List ;
20
+ import java .util .concurrent .CompletableFuture ;
20
21
import java .util .function .Function ;
21
22
23
+ import org .apache .commons .logging .Log ;
24
+ import org .apache .commons .logging .LogFactory ;
22
25
import org .apache .lucene .search .TotalHits ;
23
26
import org .elasticsearch .action .search .SearchResponse ;
24
27
import org .elasticsearch .common .text .Text ;
38
41
39
42
/**
40
43
* This represents the complete search response from Elasticsearch, including the returned documents. Instances must be
41
- * created with the {@link #from(SearchResponse,Function )} method.
44
+ * created with the {@link #from(SearchResponse, EntityCreator )} method.
42
45
*
43
46
* @author Peter-Josef Meisch
44
47
* @since 4.0
45
48
*/
46
49
public class SearchDocumentResponse {
47
50
51
+ private static final Log LOGGER = LogFactory .getLog (SearchDocumentResponse .class );
52
+
48
53
private final long totalHits ;
49
54
private final String totalHitsRelation ;
50
55
private final float maxScore ;
@@ -98,12 +103,11 @@ public Suggest getSuggest() {
98
103
* creates a SearchDocumentResponse from the {@link SearchResponse}
99
104
*
100
105
* @param searchResponse must not be {@literal null}
101
- * @param suggestEntityCreator function to create an entity from a {@link SearchDocument}
106
+ * @param entityCreator function to create an entity from a {@link SearchDocument}
102
107
* @param <T> entity type
103
108
* @return the SearchDocumentResponse
104
109
*/
105
- public static <T > SearchDocumentResponse from (SearchResponse searchResponse ,
106
- Function <SearchDocument , T > suggestEntityCreator ) {
110
+ public static <T > SearchDocumentResponse from (SearchResponse searchResponse , EntityCreator <T > entityCreator ) {
107
111
108
112
Assert .notNull (searchResponse , "searchResponse must not be null" );
109
113
@@ -112,7 +116,7 @@ public static <T> SearchDocumentResponse from(SearchResponse searchResponse,
112
116
Aggregations aggregations = searchResponse .getAggregations ();
113
117
org .elasticsearch .search .suggest .Suggest suggest = searchResponse .getSuggest ();
114
118
115
- return from (searchHits , scrollId , aggregations , suggest , suggestEntityCreator );
119
+ return from (searchHits , scrollId , aggregations , suggest , entityCreator );
116
120
}
117
121
118
122
/**
@@ -122,14 +126,14 @@ public static <T> SearchDocumentResponse from(SearchResponse searchResponse,
122
126
* @param scrollId scrollId
123
127
* @param aggregations aggregations
124
128
* @param suggestES the suggestion response from Elasticsearch
125
- * @param suggestEntityCreator function to create an entity from a {@link SearchDocument}
129
+ * @param entityCreator function to create an entity from a {@link SearchDocument}
126
130
* @param <T> entity type
127
131
* @return the {@link SearchDocumentResponse}
128
132
* @since 4.3
129
133
*/
130
134
public static <T > SearchDocumentResponse from (SearchHits searchHits , @ Nullable String scrollId ,
131
135
@ Nullable Aggregations aggregations , @ Nullable org .elasticsearch .search .suggest .Suggest suggestES ,
132
- Function < SearchDocument , T > suggestEntityCreator ) {
136
+ EntityCreator < T > entityCreator ) {
133
137
134
138
TotalHits responseTotalHits = searchHits .getTotalHits ();
135
139
@@ -153,14 +157,14 @@ public static <T> SearchDocumentResponse from(SearchHits searchHits, @Nullable S
153
157
}
154
158
}
155
159
156
- Suggest suggest = suggestFrom (suggestES , suggestEntityCreator );
160
+ Suggest suggest = suggestFrom (suggestES , entityCreator );
157
161
return new SearchDocumentResponse (totalHits , totalHitsRelation , maxScore , scrollId , searchDocuments , aggregations ,
158
162
suggest );
159
163
}
160
164
161
165
@ Nullable
162
166
private static <T > Suggest suggestFrom (@ Nullable org .elasticsearch .search .suggest .Suggest suggestES ,
163
- Function < SearchDocument , T > entityCreator ) {
167
+ EntityCreator < T > entityCreator ) {
164
168
165
169
if (suggestES == null ) {
166
170
return null ;
@@ -219,7 +223,19 @@ private static <T> Suggest suggestFrom(@Nullable org.elasticsearch.search.sugges
219
223
List <CompletionSuggestion .Entry .Option <T >> options = new ArrayList <>();
220
224
for (org .elasticsearch .search .suggest .completion .CompletionSuggestion .Entry .Option optionES : entryES ) {
221
225
SearchDocument searchDocument = optionES .getHit () != null ? DocumentAdapters .from (optionES .getHit ()) : null ;
222
- T hitEntity = searchDocument != null ? entityCreator .apply (searchDocument ) : null ;
226
+
227
+ T hitEntity = null ;
228
+
229
+ if (searchDocument != null ) {
230
+ try {
231
+ hitEntity = entityCreator .apply (searchDocument ).get ();
232
+ } catch (Exception e ) {
233
+ if (LOGGER .isWarnEnabled ()) {
234
+ LOGGER .warn ("Error creating entity from SearchDocument" );
235
+ }
236
+ }
237
+ }
238
+
223
239
options .add (new CompletionSuggestion .Entry .Option <T >(textToString (optionES .getText ()),
224
240
textToString (optionES .getHighlighted ()), optionES .getScore (), optionES .collateMatch (),
225
241
optionES .getContexts (), scoreDocFrom (optionES .getDoc ()), searchDocument , hitEntity ));
@@ -254,4 +270,14 @@ private static ScoreDoc scoreDocFrom(@Nullable org.apache.lucene.search.ScoreDoc
254
270
private static String textToString (@ Nullable Text text ) {
255
271
return text != null ? text .string () : "" ;
256
272
}
273
+
274
+ /**
275
+ * A function to convert a {@link SearchDocument} async into an entity. Asynchronous so that it can be used from the
276
+ * imperative and the reactive code.
277
+ *
278
+ * @param <T> the entity type
279
+ */
280
+ @ FunctionalInterface
281
+ public interface EntityCreator <T > extends Function <SearchDocument , CompletableFuture <T >> {}
282
+
257
283
}
0 commit comments