How to replace multiple spaces for the field value before creating the document using pattern analyzers

Do you have control over the code that you use to ingest the data into Elasticsearch? If so, it's probably better to do normalization/trimming before ingesting data.

Additionally - have you seen this post: Keyword analyzer but allow redundant white spaces?