# Query get error when using serial\_diff with bucket\_selector

**URL:** <https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 9, 2021, 11:00am UTC](https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716 "2021-02-09T11:00:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablocp](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pablocp](https://discuss.elastic.co/u/pablocp)\
**Post date:** [February 9, 2021, 11:00am UTC](https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716/1 "2021-02-09T11:00:19Z")

</div>

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...

```auto
{
  "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:

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![pablocp](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pablocp](https://discuss.elastic.co/u/pablocp)\
**Post date:** [February 22, 2021, 10:20am UTC](https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716/2 "2021-02-22T10:20:34Z")

</div>

any ideas?

---

<div class="post-metadata">

**Author:** ![pablocp](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pablocp](https://discuss.elastic.co/u/pablocp)\
**Post date:** [March 8, 2021, 4:47pm UTC](https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716/3 "2021-03-08T16:47:35Z")

</div>

any ideas?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 5, 2021, 4:48pm UTC](https://discuss.elastic.co/t/query-get-error-when-using-serial-diff-with-bucket-selector/263716/4 "2021-04-05T16:48:03Z")

</div>

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