How to exclude metadata from indexing

NOTE: This snippets are meant for versions older than ElasticPress 5.0 or if you are using the ep_meta_mode filter with auto as value.

Remove all metadata and choose individual fields to be indexed

PHP
add_filter(
	'ep_prepare_meta_data',
	function( $all_meta ) {
		// Change this array to match all meta keys you want to index.
		$allowed_meta = array( '_sku', 'shoe_size', 'in_stock' );
		$meta         = [];

		foreach ( $allowed_meta as $meta_key ) {
			if ( ! isset( $all_meta[ $meta_key ] ) ) {
				continue;
			}
			$meta[ $meta_key ] = $all_meta[ $meta_key ];
		}

		return $meta;
	}
);

Exclude certain fields or field patterns

The following code example hides all fields that contain an underscore followed by a number followed by an underscore ([…]_0_[…], […]_8_[…], etc) in the meta field name.  This pattern is used by the Advanced Custom Fields plugin to add repeater fields, which are rarely necessary for search or queries and can often be removed to reduce the number of distinct fields stored in Elasticsearch.

PHP
add_filter(
	'ep_prepare_meta_data',
	function( $meta ) {
		foreach ( $meta as $key => $value ) {
			if ( preg_match( '/.+_([0-9]+)_.+/', $key ) ) {
				unset( $meta[ $key ] );
			}
		}
		return $meta;
	}
);

You can create any regex matching expression within this function to remove metadata fields in a bulk fashion.