Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo script string query #1

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
resolve early feedback from @msfroh
  • Loading branch information
noCharger committed Apr 14, 2023
commit c5c2e3926c50048c4f5c05512ecc2c5c3b83f0cf
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ public SearchRequest processRequest(SearchRequest request) throws Exception {
searchScript = precompiledSearchScript;
}
// convert the search request to a map
Map<String, Object> cxt = Map.of("source", request.source().toMap());
Map<String, Object> ctx = Map.of("source", request.source().toMap());
// execute the script with the search request in context
searchScript.execute(cxt);
CollectionUtils.ensureNoSelfReferences(cxt, "search script");
// assert cxt has at least one key and it contains source key
if (cxt.isEmpty() || cxt.get("source") == null) {
searchScript.execute(ctx);
CollectionUtils.ensureNoSelfReferences(ctx, "search script");
// assert ctx has at least one key and it contains source key
if (ctx.isEmpty() || ctx.get("source") == null) {
throw new IllegalArgumentException("script must have at least one key");
}

Object obj = cxt.get("source");
Object obj = ctx.get("source");
if (obj instanceof Map<?, ?>) {
Map<?, ?> rawMap = (Map<?, ?>) obj;
Map<String, Object> resultMap = new LinkedHashMap<>();
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/org/opensearch/script/SearchScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public abstract class SearchScript {

public static final String[] PARAMETERS = { "cxt" };
public static final String[] PARAMETERS = { "ctx" };

/** The context used to compile {@link SearchScript} factories. */
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>(
Expand All @@ -42,7 +42,7 @@ public Map<String, Object> getParams() {
return params;
}

public abstract void execute(Map<String, Object> cxt);
public abstract void execute(Map<String, Object> ctx);

/**
* Factory for ingest script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1560,9 +1560,7 @@ public Map<String, Object> toMap() {
}

if (queryBuilder instanceof QueryStringQueryBuilder) {
String queryString = ((QueryStringQueryBuilder) queryBuilder).queryString();
// deep copy of original query string
attributes.put("query", queryString.substring(0));
attributes.put("query", ((QueryStringQueryBuilder) queryBuilder).queryString());
}

attributes.put("postQuery", postQueryBuilder);
Expand Down