# Bug in ElasticSearch (Aggs and moving\_fn) v6.6.2

**URL:** <https://discuss.elastic.co/t/bug-in-elasticsearch-aggs-and-moving-fn-v6-6-2/173393>\
**Category:** Elasticsearch\
**Created:** [March 21, 2019, 7:59pm UTC](https://discuss.elastic.co/t/bug-in-elasticsearch-aggs-and-moving-fn-v6-6-2/173393 "2019-03-21T19:59:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bartos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_bartos/32/42439_2.png) [@Chris\_Bartos](https://discuss.elastic.co/u/Chris_Bartos)\
**Post date:** [March 21, 2019, 7:59pm UTC](https://discuss.elastic.co/t/bug-in-elasticsearch-aggs-and-moving-fn-v6-6-2/173393/1 "2019-03-21T19:59:25Z")

</div>

This is my query:

```
 {
    	"sort": [{ "start_time": { "order": "asc" } }],
    	"size": 4,
    	"query": {
    		"bool": {
    			"must": [
    				{ "match": { "start_date": "03-22-2019" } },
    				{ "match": { "open": true } }
    			]
    		}
    	},
    	"aggs": {
        	"by-room": {
        		"terms": {
            		"field": "name",
            		"size": 1
        		},
        		"aggs": {
            		"every-interval": {
            			"date_histogram": {
                			"field": "start_datetime",
                			"interval": "30m",
                			"format": "YYYY-MM-DD HH:mm:ss"
            			},
            			"aggs": {
            				"num-taken": {
            					"sum": {
            						"field": "taken"
            					}
            				},
            				"is-available": {
            					"moving_fn": {
    	    						"buckets_path": "num-taken",
    	    						"window": 2,
    	    						"script": "values.length"
            					}
            				}
            			}
            		}
        		}
        	}
        }
    }

```

I'm Getting back:

```
{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 29,
        "max_score": null,
        "hits": [
{
                "_index": "room1",
                "_type": "timeslots",
                "_id": "63",
                "_score": null,
                "_source": {
                    "name": "room1",
                    "room_id": 1,
                    "date": "2019-03-22",
                    "start_time": "07:30:00",
                    "start_datetime": "2019-03-22 07:30:00",
                    "taken": false,
                    "open": true,
                },
                "sort": [
                    27000000
                ]
            },
            {
                "_index": "room1",
                "_type": "timeslots",
                "_id": "64",
                "_score": null,
                "_source": {
                    "name": "room1",
                    "room_id": 1,
                    "date": "2019-03-22",
                    "start_time": "08:00:00",
                    "start_datetime": "2019-03-22 08:00:00",
                    "taken": false,
                    "open": true,
                },
                "sort": [
                    28800000
                ]
            },
            {
                "_index": "tho_045a",
                "_type": "timeslots",
                "_id": "65",
                "_score": null,
                "_source": {
                    "name": "room1",
                    "room_id": 1,
                    "date": "2019-03-22",
                    "start_time": "08:30:00",
                    "start_datetime": "2019-03-22 08:30:00",
                    "taken": true,
                    "open": true,
                },
                "sort": [
                    30600000
                ]
            },
            {
                "_index": "room1",
                "_type": "timeslots",
                "_id": "66",
                "_score": null,
                "_source": {
                    "name": "room1",
                    "room_id": 1,
                    "date": "2019-03-22",
                    "start_time": "09:00:00",
                    "start_datetime": "2019-03-22 09:00:00",
                    "taken": true,
                    "open": true,
                },
                "sort": [
                    32400000
                ]
            } ]
    },
    "aggregations": {
        "by-room": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "room1",
                    "doc_count": 29,
                    "every-interval": {
                        "buckets": [
                            {
                            "key_as_string": "2019-01-22 07:30:00",
                            "key": 1548142200000,
                            "doc_count": 1,
                            "num-taken": {
                                "value": 0,
                                "value_as_string": "false"
                            },
                            "is-available": {
                                "value": 0
                            }
                        },
                        {
                            "key_as_string": "2019-01-22 08:00:00",
                            "key": 1548144000000,
                            "doc_count": 1,
                            "num-taken": {
                                "value": 0,
                                "value_as_string": "false"
                            },
                            "is-available": {
                                "value": 1
                            }
                        },
                        {
                            "key_as_string": "2019-01-22 08:30:00",
                            "key": 1548145800000,
                            "doc_count": 1,
                            "num-taken": {
                                "value": 1,
                                "value_as_string": "true"
                            },
                            "is-available": {
                                "value": 2
                            }
                        }
            },
     ...
}

```

I used "size 4" to demonstrate what is indexed.

1. In the buckets the dates are wrong. Should be 2019-03-22 but it's coming back 2019-01-22
2. The first two buckets in the response has "is-available": { "value": 0 } and "is-available: { "value": 1 } respectively. I expect back (the way I'm making the query -- "script": "values.length" -- along with what exactly is indexed): "is-available": { "value": 2 } and "is-available: { "value": 2 }.

Correct?

---

<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 18, 2019, 7:59pm UTC](https://discuss.elastic.co/t/bug-in-elasticsearch-aggs-and-moving-fn-v6-6-2/173393/2 "2019-04-18T19:59:35Z")

</div>

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