-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for mustache scripting of rollup.target_index field (#435)
* added support for mustache scripting of rollup.target_index field Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * defekt fixes Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * tests Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * small refactor/improvements Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * added wildcard check when creating rollup job; removed resolving targetIndex on Rollup init; added test for wildcards Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * lint fixes Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * moved target_index validation in getRollup resp handler Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * added using toMap() Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * removed catch block Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * exception fix Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * linter fix Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * fixed IT fail Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> * added Exception catch block Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com> (cherry picked from commit e858ab2)
- Loading branch information
1 parent
a1526f8
commit 78663e7
Showing
9 changed files
with
266 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...n/kotlin/org/opensearch/indexmanagement/rollup/util/RollupFieldValueExpressionResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.rollup.util | ||
|
||
import org.opensearch.common.xcontent.XContentFactory | ||
import org.opensearch.indexmanagement.indexstatemanagement.util.XCONTENT_WITHOUT_TYPE | ||
import org.opensearch.indexmanagement.opensearchapi.toMap | ||
import org.opensearch.indexmanagement.rollup.model.Rollup | ||
import org.opensearch.script.Script | ||
import org.opensearch.script.ScriptService | ||
import org.opensearch.script.ScriptType | ||
import org.opensearch.script.TemplateScript | ||
|
||
object RollupFieldValueExpressionResolver { | ||
|
||
private val validTopContextFields = setOf(Rollup.SOURCE_INDEX_FIELD) | ||
|
||
private lateinit var scriptService: ScriptService | ||
|
||
fun resolve(rollup: Rollup, fieldValue: String): String { | ||
val script = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, fieldValue, mapOf()) | ||
|
||
val contextMap = rollup.toXContent(XContentFactory.jsonBuilder(), XCONTENT_WITHOUT_TYPE) | ||
.toMap() | ||
.filterKeys { key -> key in validTopContextFields } | ||
|
||
val compiledValue = scriptService.compile(script, TemplateScript.CONTEXT) | ||
.newInstance(script.params + mapOf("ctx" to contextMap)) | ||
.execute() | ||
|
||
return if (compiledValue.isBlank()) fieldValue else compiledValue | ||
} | ||
|
||
fun registerScriptService(scriptService: ScriptService) { | ||
this.scriptService = scriptService | ||
} | ||
} |
Oops, something went wrong.