I am new to elastic and I have a question about an action I want to perform on an index when creating it.
Is it possible that one of its fields is subject to only receiving a certain value and if it does not receive it, not paint anything?
I propose an analogy for this: I have a field called flag and I only want it to support yellow, green and red . If this information is not received, mark a Null or do not paint anything. Is this possible? Is there a property to achieve that?
This is at the elastic/kibana level
I hope to be clear. Thank you very much
CodePudding user response:
Building a custom analyser for your field that will only map a regex of values provided, would suffice your requirement. Custom Analysers ES Doclink : https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html
CodePudding user response:
This should be possible using a painless script. Here is an example
a) Created a sample index with the following mapping
Created an index
PUT /so73000363
Created a mapping
PUT /so73000363/_mapping
{
"properties": {
"color": {
"type": "text"
},
"name": {
"type": "text"
}
}
}
Inserted 3 rows
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "so73000363",
"_id": "TSr5BoIBCOifrGItOzcZ",
"_score": 1.0,
"_source": {
"name": "Watch",
"color": "yellow"
}
},
{
"_index": "so73000363",
"_id": "Tir5BoIBCOifrGIthDdi",
"_score": 1.0,
"_source": {
"name": "Go",
"color": "green"
}
},
{
"_index": "so73000363",
"_id": "TCr4BoIBCOifrGIt7zd-",
"_score": 1.0,
"_source": {
"name": "Stop",
"color": "red"
}
}
]
}
}
Now lets say color can take only the following values Blue , Green, Red , Yellow
Lets try and perform the following upsert
{
"scripted_upsert":true,
"script": {
"lang": "painless",
"inline" : "if (params.color.equals(\"blue\")||params.color.equals(\"green\")||params.color.equals(\"yellow\")||params.color.equals(\"red\")){ctx._source.color=params.color}",
"params": {
"color": "blue"
}
},
"upsert" : {
"name" : "Stop",
"color" : "blue"
}
}
if the value is blue this will get updated
GET /so73000363/_source/TCr4BoIBCOifrGIt7zd-
{
"name": "Stop",
"color": "blue"
}
Try to update now with orange
{
"scripted_upsert":true,
"script": {
"lang": "painless",
"inline" : "if (params.color.equals(\"blue\")||params.color.equals(\"green\")||params.color.equals(\"yellow\")||params.color.equals(\"red\")){ctx._source.color=params.color}",
"params": {
"color": "orange"
}
},
"upsert" : {
"name" : "Stop",
"color" : "orange"
}
}```
Does not get updated as orange is not permitted.