Skip to content

Instantly share code, notes, and snippets.

@Leaf-Lin
Last active August 20, 2023 23:01
Show Gist options
  • Save Leaf-Lin/4b5a3845f8fd4c4c200417978a4b47f3 to your computer and use it in GitHub Desktop.
Save Leaf-Lin/4b5a3845f8fd4c4c200417978a4b47f3 to your computer and use it in GitHub Desktop.
#/bin/bash
curl -H 'Content-Type: application/json' -XPUT "https://elastic:<password>@govhack.es.australia-southeast1.gcp.elastic-cloud.com/_index_template/esg_data_co2-year" -d '
{
"index_patterns": [
"esg_data_co2-*"
],
"template": {
"mappings": {
"properties": {
"Country Code": {
"type": "keyword"
},
"Country Name": {
"type": "keyword"
},
"Indicator Code": {
"type": "keyword"
},
"Indicator Name": {
"type": "text"
},
"timestamp": {
"type": "date"
},
"year": {
"type": "keyword"
},
"value": {
"type": "double"
}
}
}
}
}'
for var_year in `seq 1960 2022`; do
var_date="$var_year-01-01T00:00:00.000Z"
echo $var_year
curl -H 'Content-Type: application/json' -XPOST "https://elastic:<password>@govhack.es.australia-southeast1.gcp.elastic-cloud.com/_reindex" -d '
{
"source": {
"index": "esg_data",
"_source":[
"Country Name",
"Indicator Code",
"Indicator Name",
"Country Code",
"'"$var_year"'"
],
"query": {
"bool": {
"must": [
{
"term": {
"Indicator Code": {
"value": "EG.ELC.COAL.ZS"
}
}
}
]
}
}
},
"dest": {
"index": "esg_data_coal"
},
"script": {
"source": " def var_year = \"'"$var_year"'\"; ctx._source.year = var_year; ctx._source.value = ctx._source[var_year]; ctx._source.timestamp = \"'"$var_date"'\"; ctx._source.remove(var_year); ctx._index = \"esg_data_coal-\" + var_year; "
}
}'
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment