88import static org .opensearch .ml .common .CommonValue .*;
99
1010import java .io .IOException ;
11- import java .security .AccessController ;
12- import java .security .PrivilegedExceptionAction ;
1311import java .util .HashMap ;
1412import java .util .Map ;
1513import java .util .Objects ;
@@ -96,7 +94,12 @@ public String getVersion() {
9694
9795 @ Override
9896 public boolean validate (Map <String , String > parameters ) {
99- return parameters != null && parameters .containsKey (INPUT_FIELD ) && parameters .get (INPUT_FIELD ) != null ;
97+ if (parameters == null || parameters .isEmpty ()) {
98+ return false ;
99+ }
100+ boolean argumentsFromInput = parameters .containsKey (INPUT_FIELD ) && parameters .get (INPUT_FIELD ) != null ;
101+ boolean argumentsFromParameters = parameters .containsKey (INDEX_FIELD ) && parameters .containsKey (QUERY_FIELD );
102+ return argumentsFromInput || argumentsFromParameters ;
100103 }
101104
102105 private SearchRequest getSearchRequest (String index , String query ) throws IOException {
@@ -120,8 +123,16 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
120123 try {
121124 String input = parameters .get (INPUT_FIELD );
122125 JsonObject jsonObject = GSON .fromJson (input , JsonObject .class );
123- String index = Optional .ofNullable (jsonObject ).map (x -> x .get (INDEX_FIELD )).map (JsonElement ::getAsString ).orElse (null );
124- String query = Optional .ofNullable (jsonObject ).map (x -> x .get (QUERY_FIELD )).map (JsonElement ::toString ).orElse (null );
126+ String index = Optional
127+ .ofNullable (jsonObject )
128+ .map (x -> x .get (INDEX_FIELD ))
129+ .map (JsonElement ::getAsString )
130+ .orElse (parameters .getOrDefault (INDEX_FIELD , null ));
131+ String query = Optional
132+ .ofNullable (jsonObject )
133+ .map (x -> x .get (QUERY_FIELD ))
134+ .map (JsonElement ::toString )
135+ .orElse (parameters .getOrDefault (QUERY_FIELD , null ));
125136 if (index == null || query == null ) {
126137 listener .onFailure (new IllegalArgumentException ("SearchIndexTool's two parameter: index and query are required!" ));
127138 return ;
@@ -134,10 +145,8 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
134145 if (hits != null && hits .length > 0 ) {
135146 StringBuilder contextBuilder = new StringBuilder ();
136147 for (SearchHit hit : hits ) {
137- String doc = AccessController .doPrivileged ((PrivilegedExceptionAction <String >) () -> {
138- Map <String , Object > docContent = processResponse (hit );
139- return GSON .toJson (docContent );
140- });
148+ Map <String , Object > docContent = processResponse (hit );
149+ String doc = GSON .toJson (docContent );
141150 contextBuilder .append (doc ).append ("\n " );
142151 }
143152 listener .onResponse ((T ) contextBuilder .toString ());
0 commit comments