1

I am trying to filter results with script query. I can access parameter values with hardcoded field value like this:

"script": "doc['price'].value * params.1000 > 4000",
      "params": {"1000": "1.75"}

But if I try to get field value with doc there is no filtering and I can see all results.

    "script": "doc['price'].value * params.doc['rate'] > 4000",
      "params": {"1000": "1.75"}

Is there any solution to dynamically get parameter value?

EDIT:

The Field 'rate' is just the ID. It is Integer but it is not value I need. The idea is to pass every time different value, via parameter, instead updating field 'rate' frequently. Hope This is better explanation...

example:

"script": "doc['price'].value * params.doc['rate'] > 4000",
  "params": {
              "1000": "1.75",
              "1001": "3.75",
              "1002": "5"
            }

if 
'price' == 10 &&
'rate' == 1002
result should be: 10 * 5 > 4000  

if 
'price' == 10 &&
'rate' == 1001
result should be: 10 * 3.75 > 4000

if 
'price' == 7 &&
'rate' == 1000
result should be: 7 * 1.75 > 4000   
Pusic
  • 41
  • 1
  • 7

2 Answers2

0

I'm assuming rate as an integer, if so shouldn't the script look like such. If I've understood your filtering correctly, if not please correct me:

 "script": "(doc['price'].value * doc['rate'].value) > 4000", <--replace params with doc
      "params": {"1000": "1.75"}
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
  • doc['rate'].value is integer but it is not value I need. It will change frequently so idea was to pass it as parameter. In this context 'rate' is just ID. I hope this is better explanation. – Pusic Dec 18 '16 at 17:08
0

You can try with something like this:

"script": {
    "lang": "painless",
    "inline": "doc['price'].value * params.get(doc['rate'].toString()) > 4000",
    "params": {
        "[1000]": 1.75,
        "[1001]": 3.75,
        "[1002]": 5
    }
}

Why parameters need square brackets I don't know, but using a script with Debug.explain(doc['rate'].toString()) shows that the rate field converted to string is like that (perhaps you can use a keyword field for rate, in that case I suppose parameters can have the exact same name as the field value):

"error": {
    "caused_by": {
        "reason": null,
        "type": "painless_explain_error"
    },
    "class": "java.lang.String",
    "lang": "painless",
    ...
    "script": "Debug.explain(doc['rate'].toString())",
    ...
    "to_string": "[1000]",
},
...

I have used params.get is because params is an HashMap (again, found using Debug.explain(params)). Also parameters values are not strings.