### Elasticsearch Version

8.8

### Installed Plugins

_No response_

### Java Version

_bundled_

### OS Version

all

### Problem Description

`_terms_enum` queries against a `keyword` field build automata out of their prefix string, to be intersected with the terms dictionary on the relevant field.  We don't have a limit on the size of this prefix, meaning that if somebody (either accidentally or maliciously) passes a very large string here, we can run out of memory.

### Steps to Reproduce

Send a very large string as part of a `_terms_enum` request against a keyword field.

### Logs (if relevant)

_No response_
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumRequestTests.java
private NamedXContentRegistry xContentRegistry;
    private NamedWriteableRegistry namedWriteableRegistry;
    public void setUp() throws Exception {
        super.setUp();
        SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
        List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
        entries.addAll(IndicesModule.getNamedWriteables());
        entries.addAll(searchModule.getNamedWriteables());
        namedWriteableRegistry = new NamedWriteableRegistry(entries);
        xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
    }
    @Override
    protected TermsEnumRequest createTestInstance() {
        TermsEnumRequest request = new TermsEnumRequest();
        request.size(randomIntBetween(1, 20));
        request.field(randomAlphaOfLengthBetween(3, 10));
        request.caseInsensitive(randomBoolean());
        if (randomBoolean()) {