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

does ElasticSearch preserve disk storage when saving same value on same field
Escape reserved words in elastic search
Logstash MySQL combine multiple rows
Synonyms with less relevance possible?
How to get all field names in elasticsearch index
How to index numbers and floats in elastic search using node.js but not as strings
ElasticSearch Partial Mappings with Spaces
How to create a search with or clause using Elasticsearch
restore elasticsearch snapshot to multiple nodes at once
Getting results for multi_match cross_fields query in elasticsearch with custom analyzer
does logstash's elasticsearch output plugin retry individual bulk errors?
Able to access elasticsearch without credential on production after installing elasticsearch-http-user-auth plugin
Date_Histogram in elastic search
Adding an environment variable for a specific user in wix
ElasticSearch recovers slowly on node restart
Elasticsearch synonym list with multi word values

Categories

HOME
generics
typelite
kendo-dropdown
d3.js
systemd
activemq
adobe
semantic-web
jersey-2.0
amp
visualforce
share
facebook-android-sdk
sentry
gtk
siesta
nexus3
documentum
mahout-recommender
playframework-2.0
clr
coreldraw
fido-u2f
background-color
php-5.3
code-climate
social-networking
premake
lxml
quickcheck
android-collapsingtoolbar
cloudera-manager
buildforge
pdfminer
subclassing
pdfnet
mv
secure-coding
git-tfs
sonarqube5.2
sony-future-lab-n
ol3-google-maps
knockout-2.0
beeline
jgrapht
spring.net
gtk#
winmerge
sesame
rcharts
pecl
kendo-treeview
pbx
annotatorjs
portal
java-2d
data-management
android-4.2-jelly-bean
fragmentstatepageradapter
strpos
mathnet
jeditable
flipclock
wif4.5
webgrind
servicestack-bsd
json-spirit
decidable
hmisc
fasterxml
image-rotation
inequality
berkeley-db-xml
storing-data
category-theory
radius-protocol
lsa
spring-security-acl
mousemotionlistener
quickgraph
form-for
jacob
mongo-jackson-mapper
spiral
daap
word-frequency
cakeyframeanimation
java.lang.class
monocross
nsfont
acpi
motodev-studio
canonicalization
webresponse
dip
tessellation
fgetc
rawcontacts
ppc
parentid

Resources

Encrypt Message