### Elasticsearch Version

7.16.2

### Installed Plugins

_No response_

### Java Version

openjdk 11.0.13

### OS Version

Ubuntu 18.04.1

### Problem Description

Using `auto_date_histogram` with a `ip_range` sub-aggregation with `<n>` ranges fails with an `illegal_state_exception` with reason `Expected [<n>] buckets, but got [0]`.




### Steps to Reproduce

Create index with `date` field and `ip` field and put in test data
```
PUT /testing_bug
{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "ip": {
        "type": "ip"
      }
    }
  }
}
POST /testing_bug/_doc
{
  "@timestamp": "2022-08-01T07:00:00.000Z",
  "ip": "192.168.1.1"
}
POST /testing_bug/_doc
{
  "@timestamp": "2022-08-02T07:00:00.000Z",
  "ip": "192.168.1.2"
}
```

Do `auto_date_histogram` aggregation with `ip_ranges` sub-aggregation
```
GET /testing_bug/_search
{
  "aggs": {
    "testing": {
      "auto_date_histogram": {
        "field": "@timestamp",
        "buckets": 10
      },
      "aggs": {
        "ip_ranges": {
          "ip_range": {
            "field": "ip",
            "ranges": [
              {
                "from": "10.0.0.5",
                "to": "10.0.0.10"
              }
            ]
          }
        }
      }
    }
  }
}
```

Error response:
```
{
  "error" : {
    "root_cause" : [ ],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [ ],
    "caused_by" : {
      "type" : "illegal_state_exception",
      "reason" : "Expected [1] buckets, but got [0]"
    }
  },
  "status" : 500
}
```

### Logs (if relevant)

```
[2022-09-17T01:00:53,509][WARN ][r.suppressed             ] [ip-*********] path: /********/_search, params: {pretty=true, index=*******}
org.elasticsearch.action.search.SearchPhaseExecutionException:
        at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:725) [elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.action.search.FetchSearchPhase$1.onFailure(FetchSearchPhase.java:103) [elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:28) [elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33) [elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:777) [elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26) [elasticsearch-7.16.2.jar:7.16.2]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
        at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: java.lang.IllegalStateException: Expected [5] buckets, but got [0]
        at org.elasticsearch.search.aggregations.bucket.range.InternalBinaryRange.reduce(InternalBinaryRange.java:242) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:253) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:267) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.bucket.histogram.InternalAutoDateHistogram.reduceBucket(InternalAutoDateHistogram.java:395) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.bucket.histogram.InternalAutoDateHistogram.mergeConsecutiveBuckets(InternalAutoDateHistogram.java:562) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.bucket.histogram.InternalAutoDateHistogram.maybeMergeConsecutiveBuckets(InternalAutoDateHistogram.java:547) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.bucket.histogram.InternalAutoDateHistogram.reduce(InternalAutoDateHistogram.java:517) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:253) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.search.aggregations.InternalAggregations.topLevelReduce(InternalAggregations.java:188) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.action.search.SearchPhaseController.reduceAggs(SearchPhaseController.java:538) ~[elasticsearch-7.16.2.jar:7.16.2]
        at org.elasticsearch.action.search.SearchPhaseController.reducedQueryPhase(SearchPhaseController.java:507) ~[elasticsearch
```
server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/BinaryRangeAggregator.java
for (long ord = values.nextOrd(); ord != SortedSetDocValues.NO_MORE_ORDS; ord = values.nextOrd()) {
                    lo = collect(doc, ord, bucket, lo);
                }
            }
        }
        private int collect(int doc, long ord, long bucket, int lowBound) throws IOException {
            int lo = lowBound, hi = froms.length - 1; 
            int mid = (lo + hi) >>> 1;
            while (lo <= hi) {
                if (ord < froms[mid]) {
                    hi = mid - 1;
                } else if (ord > maxTos[mid]) {
                    lo = mid + 1;
                } else {
                    break;
                }
                mid = (lo + hi) >>> 1;
            }
            if (lo > hi) return lo; 
            
            int startLo = lo, startHi = mid;
            while (startLo <= startHi) {
                final int startMid = (startLo + startHi) >>> 1;
                if (ord > maxTos[startMid]) {
                    startLo = startMid + 1;
                } else {
                    startHi = startMid - 1;
                }
            }