### Elasticsearch Version

8.8.0-SNAPSHOT

### Installed Plugins

_No response_

### Java Version

_bundled_

### OS Version

MacOS

### Problem Description

API when called is returning invalid response, on the same data terms aggs query runs fine 

![image](https://user-images.githubusercontent.com/3505601/220749805-ddf0d735-60d5-4e45-85bf-941ea3076a3f.png)


When run as aggs query 

![image](https://user-images.githubusercontent.com/3505601/220764360-49f7b648-8b15-43cf-afe2-01bd62e8ebc5.png)


### Steps to Reproduce

Queries are mentioned above ^^

### Logs (if relevant)

_No response_
server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java
@Override
        public long totalTermFreq() throws IOException {
            throw new UnsupportedOperationException();
        }
        @Override
        public PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {
            throw new UnsupportedOperationException();
        }
        @Override
        public ImpactsEnum impacts(int flags) throws IOException {
            throw new UnsupportedOperationException();
        }
        @Override
        public TermState termState() throws IOException {
            throw new UnsupportedOperationException();
        }
    }
    /**
     * A field data implementation that gives access to the values associated with
     * a particular JSON key.
     *
     * This class wraps the field data that is built directly on the keyed flattened field,
     * and filters out values whose prefix doesn't match the requested key. Loading and caching
     * is fully delegated to the wrapped field data, so that different {@link KeyedFlattenedFieldData}
     * for the same flattened field share the same global ordinals.
     *
     * Because of the code-level complexity it would introduce, it is currently not possible
     * to retrieve the underlying global ordinals map through {@link #getOrdinalMap()}.
     */
    public static class KeyedFlattenedFieldData implements IndexOrdinalsFieldData {