-
Notifications
You must be signed in to change notification settings - Fork 113
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…
…) (#444) * added support for mustache scripting of rollup.target_index field Signed-off-by: Petar Dzepina <[email protected]> * defekt fixes Signed-off-by: Petar Dzepina <[email protected]> * tests Signed-off-by: Petar Dzepina <[email protected]> * small refactor/improvements Signed-off-by: Petar Dzepina <[email protected]> * added wildcard check when creating rollup job; removed resolving targetIndex on Rollup init; added test for wildcards Signed-off-by: Petar Dzepina <[email protected]> * lint fixes Signed-off-by: Petar Dzepina <[email protected]> * moved target_index validation in getRollup resp handler Signed-off-by: Petar Dzepina <[email protected]> * added using toMap() Signed-off-by: Petar Dzepina <[email protected]> * removed catch block Signed-off-by: Petar Dzepina <[email protected]> * exception fix Signed-off-by: Petar Dzepina <[email protected]> * linter fix Signed-off-by: Petar Dzepina <[email protected]> * fixed IT fail Signed-off-by: Petar Dzepina <[email protected]> * added Exception catch block Signed-off-by: Petar Dzepina <[email protected]> (cherry picked from commit e858ab2) Co-authored-by: Petar Dzepina <[email protected]>
- Loading branch information
1 parent
4ab5ec2
commit 390863f
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.