elasticsearch


Elasticsearch Groovy Script syntax to set Nested Fields Value


We have below mapping defined for my index -
{
"mappings": {
"data": {
"_source": {
"enabled": true
},
"properties": {
"attributeNames": {
"type": "string"
},
"attributes": {
"type": "nested",
"include_in_parent": true,
"properties": {
"mc_name": {
"type":"string"
},
"mc_original_severity": {
"type": "string",
"index": "not_analyzed",
"fields": {
"converted": {
"type": "string",
"index": "not_analyzed"
}
}
},
"mc_score": {
"type": "integer"
}
}
}
}
}
}
}
We want that while indexing data into this index, based on the value of "mc_original_severity" we want to set the value of "mc_original_severity.converted". Previously we were using ES 1.7 and in that we have written groovy script and put that Groovy script inside Transform property of Mapping something like this -
"transform": {
"script": " if (ctx._source.attributes.mc_original_severity?.equals('CRITICAL')) {ctx._source['mc_original_severity.converted'] = '0';} ;",
"lang": "groovy"
}
Currently, we are using ES 5.2 and we are using Pipeline and script processor. We want to do the same thing using script processor either using Painless scripting or Groovy scripting. Below is the code which we are using right now -
String completeScript = "if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx.doc['mc_original_severity.converted'] = '0';}"
+ "else if(ctx.mc_original_severity.equals('CRITICAL')){ctx.doc['mc_original_severity.converted'] = '1';} };";
PutPipelineRequestBuilder reqBuilder = getPipelineRequestBuilder(localClient, action, "pipe1", completeScript);
reqBuilder.execute().get();
private static PutPipelineRequestBuilder getPipelineRequestBuilder(Client client, PutPipelineAction putPipelineAction,
String pipeName, String completeScript){
PutPipelineRequestBuilder reqBuilder = new PutPipelineRequestBuilder(localClient, putPipelineAction, "pipe1",
new BytesArray(
"{\n"+
" \"processors\": [\n"+
" {\n"+
" \"script\": {\n"+
" \"lang\": \"groovy\",\n"+
" \"inline\": \""+completeScript+"\"\n"+
" }\n"+
" }\n"+
" ]\n"+
"}\n"));
return reqBuilder;
But the script which we are using is not working right now while we ingest data after applying this pipeline. We have tried different combinations of Groovy as well as Painless script. Can somebody guide us how to achieve the use case described above. We are getting below exception for above used script.
"error": {
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[error evaluating if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx.doc['mc_original_severity.converted'] = '0';}else if(ctx.mc_original_severity.equals('CRITICAL')){ctx.doc['mc_original_severity.converted'] = '1';} }]; nested: NullPointerException[Cannot set property 'mc_original_severity.converted' on null object];",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "ScriptException[error evaluating if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx.doc['mc_original_severity.converted'] = '0';}else if(ctx.mc_original_severity.equals('CRITICAL')){ctx.doc['mc_original_severity.converted'] = '1';} }]; nested: NullPointerException[Cannot set property 'mc_original_severity.converted' on null object];",
"caused_by": {
"type": "script_exception",
"reason": "error evaluating if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx.doc['mc_original_severity.converted'] = '0';}else if(ctx.mc_original_severity.equals('CRITICAL')){ctx.doc['mc_original_severity.converted'] = '1';} }",
"caused_by": {
"type": "null_pointer_exception",
"reason": "Cannot set property 'mc_original_severity.converted' on null object"
},
"script_stack": [],
"script": "",
"lang": "groovy"
}
},
"header": {
"processor_type": "script"
}
}
}

Related Links

Can _score from different queries be compared?
Difference between a “plain” terms query and a terms query using a filter
Elasticsearch: Updating a field that has been set as a document _id via mapping with a path
Return parent data with child document from Elasticsearch
Elasticsearch Giving Incorrect Result When Using “must_not” operator alongwith “must” Operator
ElasticSearch _suggest queries are case sensitive. Want them to be case insensitive
How to normalize periods in elastic search query (such that JJ Abrams == J.J Abrams)?
Elastic Search - Sort By Doc Type
ElasticSearch Filtering aggregations from array field
Full text search for exact match_phrase (with leading and trailing whitespace) in elasticsearch
Do two equal documents in elasticsearch double the needed disc space
In logstash/ElasticSearch/Kibana, how do I augment records following a particular stateful request?
How to know which shard is overloaded in Elasticsearch?
ElasticSearch - searching different doc_types with the same field name but different analyzers
ELK Type Conversion - Not a number but a string
How to get the definitiion of a search analyzer of an index in elasticsearch

Categories

HOME
spring-data
blast
bokeh
adobe
mathematical-optimization
clone
stored-procedures
firebase-database
joomla3.2
mutation-testing
erd
clish
pm2
sql-injection
complexity-theory
google-cloud-logging
asciimath
scala-ide
susy-sass
clr
android-custom-view
azure-machine-learning
data-type-conversion
websphere-portal
animated-gif
ratio
introspection
python-appium
ccavenue
splunk-query
objectmapper
argv
gsp
xerces-c
uivisualeffectview
classnotfoundexception
pyttsx
permission-denied
webalizer
phppgadmin
renderman
skip-lists
sequence-diagram
patternlab.io
glassfish-4.1
hmmlearn
google-shopping-api
boost-compute
msbuild-4.0
wicked-pdf
node-glob
strpos
timestamping
flush
enunciate
openshift-cartridge
dnx50
blank-line
google-gdk
org-babel
iplimage
oracle-adf-mobile
webmethod
aho-corasick
box2dweb
webautomation
pageviews
jack
pre
windows-taskbar
codahale-metrics
google-code-prettify
days
oembed
cmath
coordinate
uipangesturerecognizer
exponent
mkannotation
git-log
explicit
self-tracking-entities
eventaggregator
source-code-protection
downloadfile

Resources

Encrypt Message