Skip to content

Commit fbf8328

Browse files
committed
HSEARCH-3761 Return the result of explanations as a JsonObject in the Elasticsearch backend
1 parent d443556 commit fbf8328

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/query/ElasticsearchSearchQuery.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.hibernate.search.engine.search.query.ExtendedSearchQuery;
1010

11+
import com.google.gson.JsonObject;
12+
1113
public interface ElasticsearchSearchQuery<H>
1214
extends ExtendedSearchQuery<H, ElasticsearchSearchResult<H>>, ElasticsearchSearchFetchable<H> {
1315

@@ -17,11 +19,11 @@ public interface ElasticsearchSearchQuery<H>
1719
* This is a shorthand for {@link #explain(String, String)} when the query only targets one index.
1820
*
1921
* @param id The id of the document to test.
20-
* @return A string representing a JSON object describing the score computation for the hit.
22+
* @return A {@link JsonObject} describing the score computation for the hit.
2123
* @throws org.hibernate.search.util.common.SearchException If the query targets multiple indexes,
2224
* or if the explain request fails.
2325
*/
24-
String explain(String id);
26+
JsonObject explain(String id);
2527

2628
/**
2729
* Explain score computation of this query for the document with the given id in the given index.
@@ -30,10 +32,10 @@ public interface ElasticsearchSearchQuery<H>
3032
*
3133
* @param indexName The name of the index containing the document to test.
3234
* @param id The id of the document to test.
33-
* @return A string representing a JSON object describing the score computation for the hit.
35+
* @return A {@link JsonObject} describing the score computation for the hit.
3436
* @throws org.hibernate.search.util.common.SearchException If the given index name does not refer to an index targeted by this query,
3537
* or if the explain request fails.
3638
*/
37-
String explain(String indexName, String id);
39+
JsonObject explain(String indexName, String id);
3840

3941
}

backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/query/impl/ElasticsearchSearchQueryImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public long fetchTotalHitCount() {
125125
}
126126

127127
@Override
128-
public String explain(String id) {
128+
public JsonObject explain(String id) {
129129
Contracts.assertNotNull( id, "id" );
130130

131131
Set<URLEncodedString> targetedIndexNames = searchContext.getIndexNames();
@@ -137,7 +137,7 @@ public String explain(String id) {
137137
}
138138

139139
@Override
140-
public String explain(String indexName, String id) {
140+
public JsonObject explain(String indexName, String id) {
141141
Contracts.assertNotNull( indexName, "indexName" );
142142
Contracts.assertNotNull( id, "id" );
143143

@@ -170,7 +170,7 @@ private Integer defaultedLimit(Integer limit, Integer offset) {
170170
}
171171
}
172172

173-
private String doExplain(URLEncodedString encodedIndexName, String id) {
173+
private JsonObject doExplain(URLEncodedString encodedIndexName, String id) {
174174
URLEncodedString elasticsearchId = URLEncodedString.fromString(
175175
searchContext.toElasticsearchId( sessionContext.getTenantIdentifier(), id )
176176
);
@@ -180,6 +180,6 @@ private String doExplain(URLEncodedString encodedIndexName, String id) {
180180
.build();
181181

182182
ExplainResult explainResult = Futures.unwrappedExceptionJoin( queryOrchestrator.submit( work ) );
183-
return searchContext.getUserFacingGson().toJson( explainResult.getJsonObject() );
183+
return explainResult.getJsonObject();
184184
}
185185
}

documentation/src/test/java/org/hibernate/search/documentation/search/query/QueryDslIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.junit.runner.RunWith;
4545
import org.junit.runners.Parameterized;
4646

47+
import com.google.gson.JsonObject;
4748
import org.apache.lucene.search.Explanation;
4849

4950
@RunWith(Parameterized.class)
@@ -296,14 +297,14 @@ public void explain_elasticsearch() {
296297
.matching( "robot" ) )
297298
.toQuery(); // <2>
298299

299-
String explanation1 = query.explain( "1" ); // <3>
300-
String explanation2 = query.explain( "Book", "1" ); // <4>
300+
JsonObject explanation1 = query.explain( "1" ); // <3>
301+
JsonObject explanation2 = query.explain( "Book", "1" ); // <4>
301302

302303
ElasticsearchSearchQuery<Book> elasticsearchQuery = query.extension( ElasticsearchExtension.get() ); // <5>
303304
// end::explain-elasticsearch[]
304305

305-
assertThat( explanation1 ).contains( "title" );
306-
assertThat( explanation2 ).contains( "title" );
306+
assertThat( explanation1 ).asString().contains( "title" );
307+
assertThat( explanation2 ).asString().contains( "title" );
307308
assertThat( elasticsearchQuery ).isNotNull();
308309
} );
309310
}

integrationtest/backend/elasticsearch/src/test/java/org/hibernate/search/integrationtest/backend/elasticsearch/ElasticsearchExtensionIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ public void query_explain_singleIndex() {
202202

203203
// Matching document
204204
Assertions.assertThat( query.explain( FIRST_ID ) )
205+
.asString()
205206
.contains( "\"description\":" )
206207
.contains( "\"details\":" );
207208

208209
// Non-matching document
209210
Assertions.assertThat( query.explain( FIFTH_ID ) )
211+
.asString()
210212
.contains( "\"description\":" )
211213
.contains( "\"details\":" );
212214
}
@@ -246,11 +248,13 @@ public void query_explain_multipleIndexes() {
246248

247249
// Matching document
248250
Assertions.assertThat( query.explain( INDEX_NAME, FIRST_ID ) )
251+
.asString()
249252
.contains( "\"description\":" )
250253
.contains( "\"details\":" );
251254

252255
// Non-matching document
253256
Assertions.assertThat( query.explain( INDEX_NAME, FIFTH_ID ) )
257+
.asString()
254258
.contains( "\"description\":" )
255259
.contains( "\"details\":" );
256260
}

0 commit comments

Comments
 (0)