77
88import static org .junit .Assert .assertEquals ;
99import static org .junit .Assert .assertFalse ;
10- import static org .junit .Assert .assertNotNull ;
1110import static org .junit .Assert .assertTrue ;
11+ import static org .opensearch .sql .legacy .TestsConstants .TEST_INDEX_ACCOUNT ;
1212
1313import java .io .IOException ;
1414import java .util .Locale ;
2222
2323public class CalcitePPLHighlightIT extends PPLIntegTestCase {
2424
25- private static final String TEST_INDEX = "highlight_test" ;
26-
2725 @ Override
2826 public void init () throws Exception {
2927 super .init ();
3028 enableCalcite ();
31-
32- // Create index with text fields
33- Request createIndex = new Request ("PUT" , "/" + TEST_INDEX );
34- createIndex .setJsonEntity (
35- "{"
36- + "\" settings\" : {\" number_of_shards\" : 1, \" number_of_replicas\" : 0},"
37- + "\" mappings\" : {\" properties\" : {"
38- + "\" message\" : {\" type\" : \" text\" },"
39- + "\" status\" : {\" type\" : \" text\" },"
40- + "\" code\" : {\" type\" : \" integer\" }"
41- + "}}"
42- + "}" );
43- client ().performRequest (createIndex );
44-
45- // Index test documents
46- Request bulk = new Request ("POST" , "/" + TEST_INDEX + "/_bulk?refresh=true" );
47- bulk .setJsonEntity (
48- "{\" index\" : {}}\n "
49- + "{\" message\" : \" Connection error occurred\" , \" status\" : \" error response\" ,"
50- + " \" code\" : 500}\n "
51- + "{\" index\" : {}}\n "
52- + "{\" message\" : \" Request completed successfully\" , \" status\" : \" ok\" , \" code\" :"
53- + " 200}\n "
54- + "{\" index\" : {}}\n "
55- + "{\" message\" : \" Timeout error in service\" , \" status\" : \" error timeout\" , \" code\" :"
56- + " 504}\n " );
57- client ().performRequest (bulk );
29+ loadIndex (Index .ACCOUNT );
5830 }
5931
6032 @ Test
6133 public void testHighlightWithWildcardFields () throws IOException {
6234 JSONObject result =
6335 executeQueryWithHighlight (
64- "search source=" + TEST_INDEX + " \" error \" " ,
36+ "search source=" + TEST_INDEX_ACCOUNT + " \\ \" Street \\ \" " ,
6537 "{\" fields\" : {\" *\" : {}}, \" pre_tags\" : [\" <em>\" ], \" post_tags\" : [\" </em>\" ]}" );
6638
6739 assertTrue (result .has ("highlights" ));
6840 JSONArray highlights = result .getJSONArray ("highlights" );
6941 assertEquals (result .getInt ("size" ), highlights .length ());
7042
71- // At least one highlight entry should contain "error" wrapped in tags
7243 boolean foundHighlight = false ;
7344 for (int i = 0 ; i < highlights .length (); i ++) {
7445 if (!highlights .isNull (i )) {
7546 String hlStr = highlights .get (i ).toString ();
76- if (hlStr .contains ("<em>error</em>" ) || hlStr . contains ( "<em>Error </em>" )) {
47+ if (hlStr .contains ("<em>Street </em>" )) {
7748 foundHighlight = true ;
7849 break ;
7950 }
@@ -86,18 +57,18 @@ public void testHighlightWithWildcardFields() throws IOException {
8657 public void testHighlightWithSpecificField () throws IOException {
8758 JSONObject result =
8859 executeQueryWithHighlight (
89- "search source=" + TEST_INDEX + " \" error \" " ,
90- "{\" fields\" : {\" message \" : {}}, \" pre_tags\" : [\" <em>\" ], \" post_tags\" :"
60+ "search source=" + TEST_INDEX_ACCOUNT + " \\ \" Street \\ \" " ,
61+ "{\" fields\" : {\" address \" : {}}, \" pre_tags\" : [\" <em>\" ], \" post_tags\" :"
9162 + " [\" </em>\" ]}" );
9263
9364 assertTrue (result .has ("highlights" ));
9465 JSONArray highlights = result .getJSONArray ("highlights" );
9566
96- // Check that highlights only contain "message" field, not "status"
9767 for (int i = 0 ; i < highlights .length (); i ++) {
9868 if (!highlights .isNull (i )) {
9969 JSONObject hl = highlights .getJSONObject (i );
100- assertFalse ("Should not highlight status field" , hl .has ("status" ));
70+ // Only address field should be highlighted, not other text fields
71+ assertFalse ("Should not highlight firstname field" , hl .has ("firstname" ));
10172 }
10273 }
10374 }
@@ -106,8 +77,9 @@ public void testHighlightWithSpecificField() throws IOException {
10677 public void testHighlightWithCustomTags () throws IOException {
10778 JSONObject result =
10879 executeQueryWithHighlight (
109- "search source=" + TEST_INDEX + " \" error\" " ,
110- "{\" fields\" : {\" *\" : {}}, \" pre_tags\" : [\" <mark>\" ], \" post_tags\" : [\" </mark>\" ]}" );
80+ "search source=" + TEST_INDEX_ACCOUNT + " \\ \" Street\\ \" " ,
81+ "{\" fields\" : {\" *\" : {}}, \" pre_tags\" : [\" <mark>\" ], \" post_tags\" :"
82+ + " [\" </mark>\" ]}" );
11183
11284 assertTrue (result .has ("highlights" ));
11385 JSONArray highlights = result .getJSONArray ("highlights" );
@@ -127,7 +99,8 @@ public void testHighlightWithCustomTags() throws IOException {
12799
128100 @ Test
129101 public void testNoHighlightWhenNotRequested () throws IOException {
130- JSONObject result = executeQuery ("search source=" + TEST_INDEX + " \" error\" " );
102+ JSONObject result =
103+ executeQueryNoHighlight ("search source=" + TEST_INDEX_ACCOUNT + " \\ \" Street\\ \" " );
131104
132105 assertFalse ("Should not have highlights when not requested" , result .has ("highlights" ));
133106 }
@@ -136,15 +109,13 @@ public void testNoHighlightWhenNotRequested() throws IOException {
136109 public void testHighlightWithPipedFilter () throws IOException {
137110 JSONObject result =
138111 executeQueryWithHighlight (
139- "search source=" + TEST_INDEX + " \" error \ " | where code > 500 " ,
112+ "search source=" + TEST_INDEX_ACCOUNT + " \\ \" Street \\ \ " | where age > 30 " ,
140113 "{\" fields\" : {\" *\" : {}}, \" pre_tags\" : [\" <em>\" ], \" post_tags\" : [\" </em>\" ]}" );
141114
142115 assertTrue (result .has ("highlights" ));
143- // Only the doc with code=504 should match
144- assertEquals (1 , result .getInt ("size" ));
116+ assertTrue (result .getInt ("size" ) > 0 );
145117 JSONArray highlights = result .getJSONArray ("highlights" );
146- assertEquals (1 , highlights .length ());
147- assertNotNull (highlights .get (0 ));
118+ assertEquals (result .getInt ("size" ), highlights .length ());
148119 }
149120
150121 @ Test
@@ -153,18 +124,17 @@ public void testExplainWithHighlight() throws IOException {
153124 request .setJsonEntity (
154125 String .format (
155126 Locale .ROOT ,
156- "{\" query\" : \" search source=%s \\ \" error \\ \" \" ,"
127+ "{\" query\" : \" search source=%s \\ \" Street \\ \" \" ,"
157128 + "\" highlight\" : {\" fields\" : {\" *\" : {}},"
158129 + "\" pre_tags\" : [\" <em>\" ], \" post_tags\" : [\" </em>\" ]}}" ,
159- TEST_INDEX ));
130+ TEST_INDEX_ACCOUNT ));
160131 RequestOptions .Builder restOptionsBuilder = RequestOptions .DEFAULT .toBuilder ();
161132 restOptionsBuilder .addHeader ("Content-Type" , "application/json" );
162133 request .setOptions (restOptionsBuilder );
163134 Response response = client ().performRequest (request );
164135 assertEquals (200 , response .getStatusLine ().getStatusCode ());
165136
166137 String body = org .opensearch .sql .legacy .TestUtils .getResponseBody (response , true );
167- // The explain output should contain the highlight clause
168138 assertTrue ("Explain should contain highlight" , body .contains ("highlight" ));
169139 }
170140
@@ -181,4 +151,16 @@ private JSONObject executeQueryWithHighlight(String query, String highlightJson)
181151 String body = org .opensearch .sql .legacy .TestUtils .getResponseBody (response , true );
182152 return new JSONObject (body );
183153 }
154+
155+ private JSONObject executeQueryNoHighlight (String query ) throws IOException {
156+ Request request = new Request ("POST" , "/_plugins/_ppl" );
157+ request .setJsonEntity (String .format (Locale .ROOT , "{\" query\" : \" %s\" }" , query ));
158+ RequestOptions .Builder restOptionsBuilder = RequestOptions .DEFAULT .toBuilder ();
159+ restOptionsBuilder .addHeader ("Content-Type" , "application/json" );
160+ request .setOptions (restOptionsBuilder );
161+ Response response = client ().performRequest (request );
162+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
163+ String body = org .opensearch .sql .legacy .TestUtils .getResponseBody (response , true );
164+ return new JSONObject (body );
165+ }
184166}
0 commit comments