Skip to content

Commit f97dd2f

Browse files
committed
Clean up query response assertion
1 parent d176446 commit f97dd2f

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SemanticTextUpgradeIT.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapperTests.addSemanticTextInferenceResults;
4747
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText;
4848
import static org.hamcrest.CoreMatchers.equalTo;
49-
import static org.hamcrest.CoreMatchers.instanceOf;
5049
import static org.hamcrest.CoreMatchers.notNullValue;
5150

5251
public class SemanticTextUpgradeIT extends AbstractUpgradeTestCase {
@@ -123,10 +122,10 @@ private void performIndexQueryHighlightOps() throws IOException {
123122
indexDoc(DOC_2_ID, DOC_VALUES.get(DOC_2_ID));
124123

125124
ObjectPath sparseQueryObjectPath = semanticQuery(SPARSE_FIELD, SPARSE_MODEL, "test value", 3);
126-
assertQueryResponse(sparseQueryObjectPath, SPARSE_FIELD);
125+
assertQueryResponseWithHighlights(sparseQueryObjectPath, SPARSE_FIELD);
127126

128127
ObjectPath denseQueryObjectPath = semanticQuery(DENSE_FIELD, DENSE_MODEL, "test value", 3);
129-
assertQueryResponse(denseQueryObjectPath, DENSE_FIELD);
128+
assertQueryResponseWithHighlights(denseQueryObjectPath, DENSE_FIELD);
130129
}
131130

132131
private String getIndexName() {
@@ -227,24 +226,20 @@ private ObjectPath semanticQuery(String field, Model fieldModel, String query, I
227226
return assertOKAndCreateObjectPath(response);
228227
}
229228

230-
@SuppressWarnings("unchecked")
231-
private static void assertQueryResponse(ObjectPath queryObjectPath, String field) throws IOException {
229+
private static void assertQueryResponseWithHighlights(ObjectPath queryObjectPath, String field) throws IOException {
232230
assertThat(queryObjectPath.evaluate("hits.total.value"), equalTo(2));
233231
assertThat(queryObjectPath.evaluateArraySize("hits.hits"), equalTo(2));
234232

235233
Set<String> docIds = new HashSet<>();
236-
List<Object> hits = queryObjectPath.evaluate("hits.hits");
237-
for (Object hit : hits) {
238-
assertThat(hit, instanceOf(Map.class));
239-
Map<String, Object> hitMap = (Map<String, Object>) hit;
240-
241-
String id = (String) hitMap.get("_id");
234+
List<Map<String, Object>> hits = queryObjectPath.evaluate("hits.hits");
235+
for (Map<String, Object> hit : hits) {
236+
String id = ObjectPath.evaluate(hit, "_id");
242237
assertThat(id, notNullValue());
243238
docIds.add(id);
244239

245240
List<String> expectedHighlight = DOC_VALUES.get(id);
246241
assertThat(expectedHighlight, notNullValue());
247-
assertThat(((Map<String, Object>) hitMap.get("highlight")).get(field), equalTo(expectedHighlight));
242+
assertThat(ObjectPath.evaluate(hit, "highlight." + field), equalTo(expectedHighlight));
248243
}
249244

250245
assertThat(docIds, equalTo(Set.of(DOC_1_ID, DOC_2_ID)));

0 commit comments

Comments
 (0)