2020import org .elasticsearch .index .query .NestedQueryBuilder ;
2121import org .elasticsearch .inference .Model ;
2222import org .elasticsearch .inference .TaskType ;
23+ import org .elasticsearch .search .fetch .subphase .highlight .HighlightBuilder ;
2324import org .elasticsearch .test .rest .ObjectPath ;
2425import org .elasticsearch .xcontent .XContentBuilder ;
2526import org .elasticsearch .xcontent .XContentFactory ;
3233
3334import java .io .IOException ;
3435import java .util .Arrays ;
36+ import java .util .HashSet ;
3537import java .util .List ;
38+ import java .util .Map ;
39+ import java .util .Set ;
3640
3741import static org .elasticsearch .xpack .inference .mapper .SemanticTextFieldMapperTests .addSemanticTextInferenceResults ;
3842import static org .elasticsearch .xpack .inference .mapper .SemanticTextFieldTests .randomSemanticText ;
3943import static org .hamcrest .CoreMatchers .equalTo ;
44+ import static org .hamcrest .CoreMatchers .instanceOf ;
45+ import static org .hamcrest .CoreMatchers .notNullValue ;
4046
4147public 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