Query get error when using serial_diff with bucket_selector

I have a set of products, and each product has a price that varies in time. I want to find those products that comparing to week -1 have decreased its average value a %. I thought it would be fine to make a query and to use serial_diff with a bucket selector (in a date_histogram), but I have a query error due to the fact that the first register is used to calculate he serial_diff and has no value. If in the script condition I don't use var serial_diff_PRICE, it works OK, but not with it.

Is any way to avoid this error or another way to query the data I want? The option "gap_policy":"insert_zeros" doesn't work, and can't give serial_diff_PRICE an initial value...

{
  "size": 0,
  "aggs": {
    "product_name": {"terms": {"field": "prod_name","size": 5},
      "aggs": {
        "Histogram": {"date_histogram": {"field": "fecha","fixed_interval": "7d","time_zone": "Europe/Madrid"},
          "aggs": {
            "PRICE": {"avg": {"field": "price"}},
            "serial_diff_PRICE" : {"serial_diff": {"buckets_path": "PRICE","lag": 1}},
			"FILTER_PRICE": {
				"bucket_selector": {
					"buckets_path": {
						"PRICE": "PRICE",
						"serial_diff_PRICE": "serial_diff_PRICE"
					},
					"script": "params.serial_diff_PRICE <= (-0.4*params.PRICE)"
				}	
			}	
          }
		}
  	  }	
  	}
  },
  "query": {
    "bool": {
      "must": [
        { "range" : {"fecha" : { "gte" : "2021-01-15T00:00:00+0100", "lte": "2021-01-25T23:55:00+0100"}}},
      ],
      "filter": [
	  ],              
      "should": [],
      "minimum_should_match":0
    }
  }
}

And the error produced:

{
  "error" : {
    "root_cause" : [ ],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [ ],
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "runtime error",
      "script_stack" : [
        "params.serial_diff_PRICE <= (-0.4*params.PRICE)",
        "                                        ^---- HERE"
      ],
      "script" : "params.serial_diff_PRICE <= (-0.4*params.PRICE)",
      "lang" : "painless",
      "position" : {
        "offset" : 40,
        "start" : 0,
        "end" : 47
      },
      "caused_by" : {
        "type" : "null_pointer_exception",
        "reason" : "Cannot invoke \"Object.getClass()\" because \"leftObject\" is null"
      }
    }
  },
  "status" : 400
}

Kind regards,
Pablo

any ideas?

any ideas?

1 Like

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