1

I'm trying to have a dynamic param based on a value in the document.

I tried so far this here

 body: {
            "script_fields": {
                "potentialIncome": {
                    "script": {
                        "lang": "painless",
                        "source": "doc.rentPrice.value - params['doc.buyingPrice.value']",
                        "params": {
                            120000: 1200,
                            150000: 1500
                        }
                    }
                }

            }
        }

This throws me following error:

type: 'script_exception',
    reason: 'runtime error',
    script_stack: 
     [ 'doc.rentPrice.value - params[\'doc.buyingPrice.value\']',
       '                            ^---- HERE' ],
    script: 'doc.rentPrice.value - params[\'doc.buyingPrice.value\']',
    lang: 'painless' 

I would like to have params dynamic in a way that the doc value buyingPrice decides which value to deduct.

Using ElasticSearch 7.2


A complicated and bad way is to use following script

if(doc['buyingPrice'].value==120000){return doc['rentPrice'].value-params['120000']}

else if(doc['buyingPrice'].value==150000){return doc['rentPrice'].value-params['150000']}


The Es object:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [{
            "_index": "immo",
            "_type": "objects",
            "_id": "1",
            "_score": 1.0,
            "_source": {
                "buyingPrice": 120000,
                "rentPrice": 500
            }
        }, {
            "_index": "immo",
            "_type": "objects",
            "_id": "2",
            "_score": 1.0,
            "_source": {
                "buyingPrice": 150000,
                "rentPrice": 500
            }
        }]
    }
}

1 Answer 1

2

You need to try without the single quotes.

"source": "return (params[String.valueOf(doc.buyingPrice.value)] != null) ? doc.rentPrice.value - params[String.valueOf(doc.buyingPrice.value)] :  0",
                         ^                     ^
                         |                     |
Sign up to request clarification or add additional context in comments.

4 Comments

This works in the sense it does not break anymore - but potential income is now always 0. I edited and add the objects
There you go I fixed the query.
Hey - can you do me a favor? ;) I have the same issue - now it is a text field. The text field is doc.region.value and I have params where the key is a string. How do I do that?
Feel free to ask a new question.

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.