use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.
the class BucketScriptTests method createTestAggregatorFactory.
@Override
protected BucketScriptPipelineAggregationBuilder createTestAggregatorFactory() {
String name = randomAlphaOfLengthBetween(3, 20);
Map<String, String> bucketsPaths = new HashMap<>();
int numBucketPaths = randomIntBetween(1, 10);
for (int i = 0; i < numBucketPaths; i++) {
bucketsPaths.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 40));
}
Script script;
if (randomBoolean()) {
script = mockScript("script");
} else {
Map<String, Object> params = new HashMap<>();
if (randomBoolean()) {
params.put("foo", "bar");
}
ScriptType type = randomFrom(ScriptType.values());
script = new Script(type, type == ScriptType.STORED ? null : randomFrom("my_lang", Script.DEFAULT_SCRIPT_LANG), "script", params);
}
BucketScriptPipelineAggregationBuilder factory = new BucketScriptPipelineAggregationBuilder(name, bucketsPaths, script);
if (randomBoolean()) {
factory.format(randomAlphaOfLengthBetween(1, 10));
}
if (randomBoolean()) {
factory.gapPolicy(randomFrom(GapPolicy.values()));
}
return factory;
}
use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.
the class UpdateRequest method updateOrCreateScript.
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) {
Script script = script();
if (script == null) {
script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params);
} else {
String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent;
ScriptType newScriptType = type == null ? script.getType() : type;
String newScriptLang = lang == null ? script.getLang() : lang;
Map<String, Object> newScriptParams = params == null ? script.getParams() : params;
script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams);
}
script(script);
}
use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.
the class RoundTripTests method randomScript.
private Script randomScript() {
ScriptType type = randomFrom(ScriptType.values());
String lang = random().nextBoolean() ? Script.DEFAULT_SCRIPT_LANG : randomSimpleString(random());
String idOrCode = randomSimpleString(random());
Map<String, Object> params = Collections.emptyMap();
type = ScriptType.STORED;
return new Script(type, type == ScriptType.STORED ? null : lang, idOrCode, params);
}
use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.
the class UpdateRequestTests method testToAndFromXContent.
public void testToAndFromXContent() throws IOException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.detectNoop(randomBoolean());
if (randomBoolean()) {
XContentType xContentType = randomFrom(XContentType.values());
BytesReference source = RandomObjects.randomSource(random(), xContentType);
updateRequest.doc(new IndexRequest().source(source, xContentType));
updateRequest.docAsUpsert(randomBoolean());
} else {
ScriptType scriptType = randomFrom(ScriptType.values());
String scriptLang = (scriptType != ScriptType.STORED) ? randomAlphaOfLength(10) : null;
String scriptIdOrCode = randomAlphaOfLength(10);
int nbScriptParams = randomIntBetween(0, 5);
Map<String, Object> scriptParams = new HashMap<>(nbScriptParams);
for (int i = 0; i < nbScriptParams; i++) {
scriptParams.put(randomAlphaOfLength(5), randomAlphaOfLength(5));
}
updateRequest.script(new Script(scriptType, scriptLang, scriptIdOrCode, scriptParams));
updateRequest.scriptedUpsert(randomBoolean());
}
if (randomBoolean()) {
XContentType xContentType = randomFrom(XContentType.values());
BytesReference source = RandomObjects.randomSource(random(), xContentType);
updateRequest.upsert(new IndexRequest().source(source, xContentType));
}
if (randomBoolean()) {
if (randomBoolean()) {
updateRequest.fetchSource(randomBoolean());
} else {
String[] includes = new String[randomIntBetween(0, 5)];
for (int i = 0; i < includes.length; i++) {
includes[i] = randomAlphaOfLength(5);
}
String[] excludes = new String[randomIntBetween(0, 5)];
for (int i = 0; i < excludes.length; i++) {
excludes[i] = randomAlphaOfLength(5);
}
if (randomBoolean()) {
updateRequest.fetchSource(includes, excludes);
}
}
}
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toShuffledXContent(updateRequest, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = BytesReference.bytes(shuffleXContent(parser, randomBoolean()));
}
}
UpdateRequest parsedUpdateRequest = new UpdateRequest();
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsedUpdateRequest.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
assertEquals(updateRequest.script(), parsedUpdateRequest.script());
assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
BytesReference finalBytes = toXContent(parsedUpdateRequest, xContentType, humanReadable);
assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.
the class BucketSelectorTests method createTestAggregatorFactory.
@Override
protected BucketSelectorPipelineAggregationBuilder createTestAggregatorFactory() {
String name = randomAlphaOfLengthBetween(3, 20);
Map<String, String> bucketsPaths = new HashMap<>();
int numBucketPaths = randomIntBetween(1, 10);
for (int i = 0; i < numBucketPaths; i++) {
bucketsPaths.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 40));
}
Script script;
if (randomBoolean()) {
script = mockScript("script");
} else {
Map<String, Object> params = new HashMap<>();
if (randomBoolean()) {
params.put("foo", "bar");
}
ScriptType type = randomFrom(ScriptType.values());
script = new Script(type, type == ScriptType.STORED ? null : randomFrom("my_lang", Script.DEFAULT_SCRIPT_LANG), "script", params);
}
BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder(name, bucketsPaths, script);
if (randomBoolean()) {
factory.gapPolicy(randomFrom(GapPolicy.values()));
}
return factory;
}
Aggregations