8
8
import static org .opensearch .ml .common .CommonValue .*;
9
9
10
10
import java .io .IOException ;
11
- import java .security .AccessController ;
12
- import java .security .PrivilegedExceptionAction ;
13
11
import java .util .HashMap ;
14
12
import java .util .Map ;
15
13
import java .util .Objects ;
@@ -96,7 +94,12 @@ public String getVersion() {
96
94
97
95
@ Override
98
96
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 ;
100
103
}
101
104
102
105
private SearchRequest getSearchRequest (String index , String query ) throws IOException {
@@ -120,8 +123,16 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
120
123
try {
121
124
String input = parameters .get (INPUT_FIELD );
122
125
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 ));
125
136
if (index == null || query == null ) {
126
137
listener .onFailure (new IllegalArgumentException ("SearchIndexTool's two parameter: index and query are required!" ));
127
138
return ;
@@ -134,10 +145,8 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
134
145
if (hits != null && hits .length > 0 ) {
135
146
StringBuilder contextBuilder = new StringBuilder ();
136
147
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 );
141
150
contextBuilder .append (doc ).append ("\n " );
142
151
}
143
152
listener .onResponse ((T ) contextBuilder .toString ());
0 commit comments