Update_by_query by Groovy file

Hi,

I am trying to update the field mileage using the groovy file script. All the samples I saw so far utilizes this kind of way to update. I can't seem to update it using the groovy file.

"script" : {"inline":"ctx._source.mileage += 1"},

Here is my sample data that I am trying to query.

{
"_index": "carmojodev2",
"_type": "inventory",
"_id": "62A646B5-A1F4-E611-8119-005056A41824",
"_version": 16,
"found": true,
"_source": {
"doors": 4,
"series": null,
"discountpercent": 0,
"model": "TRAILBLAZER",
"exteriorcolor": null,
"totalcost": 7000,
"modelidentifier": "348696",
"retailbook": 10900,
"location": null,
"state": "LA",
"drivetrain": "4WD",
"maxdiscount": 0,
"vehicleclass": 2,
"essource": "estool",
"buyprogramwholesale": 8350,
"companyid": "25C45D4E-3130-4DDB-90E0-BF256DEA2B62",
"lastsynctoes_date": "2017-03-22T00:42:52.1073969",
"distance": null,
"askingprice": 8000,
"description": null,
"bodytype": "SUV",
"vin": "1GNDT13S082242922",
"bookcomparisonvalue": 10600,
"iswestlakevalued": 0,
"highwaympg": 0,
"year": 2008,
"trim": "LT SPORT UTILITY 4D",
"mainexternaldocumentid": "YmlVQjBTUG5BdCtEbDdncEpQclB1cll5aDBzNGp1Szdqdmo2aWFIMVpEK2p3S3pvbmNhQTFsTlFNdzVRR2h4TFpwZ3JLc1QxQ08zOU43bUNXMzZvS3c9PQ%3d%3d",
"interiorcolor": null,
"inventoryid": "62A646B5-A1F4-E611-8119-005056A41824",
"advertisingprice": 10000,
"specialprice": 0,
"mileage": 57008,
"cylinders": 6,
"westlakeclasscode": 0,
"showonwebsites": "WIDGET,CARMOJO",
"autocheckscore": null,
"engine": "6-CYL, 4.2 LITER",
"specialpriceenddate": "2017-02-16T00:00:00",
"fueltype": "GASOLINE",
"esdbname": "dckiss12",
"vehicleinvoiceprice": 4,
"transmission": "AUTOMATIC, 4-SPD W/OVERDRIVE",
"stockno": "WHY WOULD YOU ADD A SPACE ",
"vehicleprice": 10000,
"transmissiontype": "AUTOMATIC",
"inventoryminfrontgross": null,
"bookvaluedefaultbystate": "NADA",
"specialpricestartdate": "2017-02-16T00:00:00",
"citympg": 0,
"make": "CHEVROLET",
"bodystyle": "SUV"
}
}

Here is my post address that I am using right now

https://10.209.0.40:2900/carmojodev2/inventory/_update_by_query

And it contains this json body that calls the groovy script file.

{
"from": 0,
"size": 50,
"script": {
"file": "mileage_add_by_one",
"lang": "groovy",
"params": {}
},
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"_id": "62A646B5-A1F4-E611-8119-005056A41824"
}
},
{
"range": {
"mileage": {
"gt": 0
}
}
}
]
}
}
]
}
}
}
}
}

On this json body, there is a mileage_add_by_one in the file inside the script.
And here is a simple implementation of the groovy file. It justs add a +1 mileage for now.

tempMileage = 0
if (doc.containsKey('mileage') == false || doc['mileage'].value == 0)
{
tempMileage = 0
}
else
{
tempMileage = doc['mileage'].value + 1
}
tempMileage

Here is the error that i got.
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "failed to run file script [mileage_add_by_one] using lang [groovy]"
}
],
"type": "script_exception",
"reason": "failed to run file script [mileage_add_by_one] using lang [groovy]",
"caused_by": {
"type": "missing_property_exception",
"reason": "No such property: doc for class: 44b47ca9838dd911303aab40815b4ed2e6eb6043"
}
},
"status": 500
}

Any thoughts? thanks a lot!

You need to use the ctx._source instead of doc[]. For example, ctx._source.mileage instead of doc['mileage']

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.