Skip to content

Commit 4c519dd

Browse files
committed
Perform highlighting and check query response
1 parent 296d240 commit 4c519dd

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.index.query.NestedQueryBuilder;
2121
import org.elasticsearch.inference.Model;
2222
import org.elasticsearch.inference.TaskType;
23+
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
2324
import org.elasticsearch.test.rest.ObjectPath;
2425
import org.elasticsearch.xcontent.XContentBuilder;
2526
import org.elasticsearch.xcontent.XContentFactory;
@@ -32,11 +33,16 @@
3233

3334
import java.io.IOException;
3435
import java.util.Arrays;
36+
import java.util.HashSet;
3537
import java.util.List;
38+
import java.util.Map;
39+
import java.util.Set;
3640

3741
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapperTests.addSemanticTextInferenceResults;
3842
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText;
3943
import static org.hamcrest.CoreMatchers.equalTo;
44+
import static org.hamcrest.CoreMatchers.instanceOf;
45+
import static org.hamcrest.CoreMatchers.notNullValue;
4046

4147
public class SemanticTextUpgradeIT extends AbstractUpgradeTestCase {
4248
private static final String INDEX_BASE_NAME = "semantic_text_test_index";
@@ -93,8 +99,8 @@ private void createAndPopulateIndex() throws IOException {
9399

94100
private void performIndexQueryHighlightOps() throws IOException {
95101
indexDoc("doc_2", List.of("another test value"));
96-
ObjectPath queryObjectPath = semanticQuery("test value");
97-
assertThat(queryObjectPath.evaluate("hits.total.value"), equalTo(2));
102+
ObjectPath queryObjectPath = semanticQuery("test value", 3);
103+
assertQueryResponse(queryObjectPath);
98104
}
99105

100106
private String getIndexName() {
@@ -129,7 +135,7 @@ private void indexDoc(String id, List<String> semanticTextFieldValue) throws IOE
129135
assertOK(response);
130136
}
131137

132-
private ObjectPath semanticQuery(String query) throws IOException {
138+
private ObjectPath semanticQuery(String query, Integer numOfHighlightFragments) throws IOException {
133139
// We can't perform a real semantic query because that requires performing inference, so instead we perform an equivalent nested
134140
// query
135141
List<WeightedToken> weightedTokens = Arrays.stream(query.split("\\s")).map(t -> new WeightedToken(t, 1.0f)).toList();
@@ -150,6 +156,15 @@ private ObjectPath semanticQuery(String query) throws IOException {
150156
XContentBuilder builder = XContentFactory.jsonBuilder();
151157
builder.startObject();
152158
builder.field("query", nestedQueryBuilder);
159+
if (numOfHighlightFragments != null) {
160+
HighlightBuilder.Field highlightField = new HighlightBuilder.Field(SEMANTIC_TEXT_FIELD);
161+
highlightField.numOfFragments(numOfHighlightFragments);
162+
163+
HighlightBuilder highlightBuilder = new HighlightBuilder();
164+
highlightBuilder.field(highlightField);
165+
166+
builder.field("highlight", highlightBuilder);
167+
}
153168
builder.endObject();
154169

155170
Request request = new Request("GET", getIndexName() + "/_search");
@@ -158,4 +173,34 @@ private ObjectPath semanticQuery(String query) throws IOException {
158173
Response response = client().performRequest(request);
159174
return assertOKAndCreateObjectPath(response);
160175
}
176+
177+
@SuppressWarnings("unchecked")
178+
private static void assertQueryResponse(ObjectPath queryObjectPath) throws IOException {
179+
final Map<String, List<String>> expectedHighlights = Map.of(
180+
"doc_1",
181+
List.of("a test value", "with multiple test values"),
182+
"doc_2",
183+
List.of("another test value")
184+
);
185+
186+
assertThat(queryObjectPath.evaluate("hits.total.value"), equalTo(2));
187+
assertThat(queryObjectPath.evaluateArraySize("hits.hits"), equalTo(2));
188+
189+
Set<String> docIds = new HashSet<>();
190+
List<Object> hits = queryObjectPath.evaluate("hits.hits");
191+
for (Object hit : hits) {
192+
assertThat(hit, instanceOf(Map.class));
193+
Map<String, Object> hitMap = (Map<String, Object>) hit;
194+
195+
String id = (String) hitMap.get("_id");
196+
assertThat(id, notNullValue());
197+
docIds.add(id);
198+
199+
List<String> expectedHighlight = expectedHighlights.get(id);
200+
assertThat(expectedHighlight, notNullValue());
201+
assertThat(((Map<String, Object>) hitMap.get("highlight")).get(SEMANTIC_TEXT_FIELD), equalTo(expectedHighlight));
202+
}
203+
204+
assertThat(docIds, equalTo(Set.of("doc_1", "doc_2")));
205+
}
161206
}

0 commit comments

Comments
 (0)