Skip to content

Instantly share code, notes, and snippets.

@wlalele
Last active November 18, 2020 13:38
Show Gist options
  • Save wlalele/44d766be0fba9c25b389928acd2187f7 to your computer and use it in GitHub Desktop.
Save wlalele/44d766be0fba9c25b389928acd2187f7 to your computer and use it in GitHub Desktop.
working use-case activities
// find timeslot by sku in a collection
def findById(def timeslots, def timeslotId) {
for (def timeslot: timeslots) {
if (timeslot.timeslotId == timeslotId) {
return timeslot;
}
}
return null;
}
// params contains ctx, which cause a infinite loop if you try to set it inside ctx._source
// https://github.com/elastic/elasticsearch/issues/19475#issuecomment-235119405
params.remove('ctx');
def params_timeslots = params.timeslots;
def is_update = ctx.op != 'create';
def has_timeslots = ctx._source.timeslots !== null && ctx._source.timeslots.length > 0;
// put all params inside source (except update case for timeslot)
if (is_update && has_timeslots) {
// in case of update remove timeslots from params
params.remove('timeslots');
}
ctx._source.putAll(params);
// do specific process on timeslots in case of update
if (is_update && has_timeslots) {
// foreach timeslot in timeslots given
for (def _paramsTimeslot: params_timeslots) {
def _sourceTimeslot = findById(ctx._source.timeslots, _paramsTimeslot.timeslotId);
if (_sourceTimeslot == null) {
ctx._source.timeslots.add(_paramsTimeslot);
continue;
}
if (_sourceTimeslot.availability != _sourceTimeslot.subscribersLimit) {
_paramsTimeslot.remove('availability')
}
_sourceTimeslot.putAll(_paramsTimeslot);
}
}
## setup
DELETE /activities_fra/_doc/99999998
POST /activities_fra/_doc/99999998
{
"name": "Test",
"timeslots": [
{
"timeslotId": "123-125",
"availability": 99,
"subscribersLimit": 100,
"groups": []
},
{
"timeslotId": "123-124",
"availability": 12,
"subscribersLimit": 100,
"groups": []
}
]
}
## verification
GET /activities_fra/_doc/99999998
GET /activities_fra/_doc/124
## bulk upsert
POST _bulk
{
"update": {
"_index": "activities_fra",
"_type": "_doc",
"_id": "99999998"
}
}
{
"scripted_upsert": true,
"script": {
"source": """
// find timeslot by sku in a collection
def findById(def timeslots, def timeslotId) {
for (def timeslot: timeslots) {
if (timeslot.timeslotId == timeslotId) {
return timeslot;
}
}
return null;
}
// params contains ctx, which cause a infinite loop if you try to set it inside ctx._source
// https://github.com/elastic/elasticsearch/issues/19475#issuecomment-235119405
params.remove('ctx');
def params_timeslots = params.timeslots;
def is_update = ctx.op != 'create';
def has_timeslots = ctx._source.timeslots !== null && ctx._source.timeslots.length > 0;
// put all params inside source (except update case for timeslot)
if (is_update && has_timeslots) {
// in case of update remove timeslots from params
params.remove('timeslots');
}
ctx._source.putAll(params);
// do specific process on timeslots in case of update
if (is_update && has_timeslots) {
// foreach timeslot in timeslots given
for (def _paramsTimeslot: params_timeslots) {
def _sourceTimeslot = findById(ctx._source.timeslots, _paramsTimeslot.timeslotId);
if (_sourceTimeslot == null) {
ctx._source.timeslots.add(_paramsTimeslot);
continue;
}
if (_sourceTimeslot.availability != _sourceTimeslot.subscribersLimit) {
_paramsTimeslot.remove('availability')
}
_sourceTimeslot.putAll(_paramsTimeslot);
}
}
""",
"lang": "painless",
"params": {
"name": "jean",
"email": "[email protected]",
"timeslots": [
{
"timeslotId": "123-126",
"availability": 125,
"subscribersLimit": 125
},
{
"timeslotId": "123-127",
"availability": 125,
"subscribersLimit": 125
},
{
"timeslotId": "123-128",
"availability": 50,
"subscribersLimit": 50
},
{
"timeslotId": "123-125",
"availability": 100,
"subscribersLimit": 100,
"groups": [1, 2, 3]
},
]
}
},
"upsert": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment