0

I have elasticsearch document as below.

{
  "ProductID": 123,
  "SellingPrice": 1200.00,
  "DiscountAmount":0.00,
  "Discount": {
    "Type": "percentage",
    "Amount": 25,
    "StartDate": "2014-08-13T12:05:00",
    "EndDate": "2014-12-31T12:10:00"
  }
}

Whenever i retrieve this document, script should automatically check the Endate with Current system date, if discount is not expired, It should change "DiscountAmount" field based on discount percentage in the result. Please help me on this.

0

1 Answer 1

1

You can't change the actual fields in your document but you can add fields to your result with script fields. Something like this (untested) should get you started.

"script_fields" : {        
    "actual_cost" : {
        "script" : "if (doc['Discount.EndDate'].value > System.currentTimeMillis()) {return doc['SellingPrice'].value * doc['Discount.Amount]/100;}; return doc['SellingPrice'].value;"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to change value in result document whenever querying.. not in actual field.
The above adds a new field to the result when you use it in a search query. Nothing is changed (can't be). I am 99% sure this does what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.