Skip to content

Added a Builder for KeywordFieldType, removed redundant constructors#136846

Closed
Kubik42 wants to merge 1 commit intoelastic:mainfrom
Kubik42:kubik-keyword-field-mapper-constructor-improvements
Closed

Added a Builder for KeywordFieldType, removed redundant constructors#136846
Kubik42 wants to merge 1 commit intoelastic:mainfrom
Kubik42:kubik-keyword-field-mapper-constructor-improvements

Conversation

@Kubik42
Copy link
Contributor

@Kubik42 Kubik42 commented Oct 21, 2025

This is a follow up for #132962, where one of the feedbacks I got was to clean up KeywordFieldType's constructors.

I agree with this feedback. The constructors are very long and nearly identical. This makes them quite error prone and difficult to extend.

This PR addresses that by removing all but one constructor and replacing them with a Builder.

Note, I left behind one of the constructors as it was too messy to remove. The two classes that rely on it (ValuesSourceReaderBenchmark and EsPhysicalOperationProviders) can be reworked, but the end result is quite messy. Frankly, this mess is a consequence of how we're using the constructor in the first place. That being, we're creating a "builder" and bypassing the actual building in favor of using the builder as a data carrier, and then overriding a bunch of things that builder would've done for us. The workaround is to actually call builder.build(), but there are so nuances with needing to create a MapperBuilderContext for the builder to use, which I don't think is a good idea given that this is production and non-test code.

@Kubik42 Kubik42 added >non-issue Team:StorageEngine :StorageEngine/Mapping The storage related side of mappings labels Oct 21, 2025
@Kubik42 Kubik42 force-pushed the kubik-keyword-field-mapper-constructor-improvements branch 3 times, most recently from 88c816b to 735cc1e Compare October 21, 2025 20:39
@Kubik42 Kubik42 force-pushed the kubik-keyword-field-mapper-constructor-improvements branch from 735cc1e to 7b63293 Compare October 21, 2025 21:52
@Kubik42 Kubik42 marked this pull request as ready for review October 21, 2025 23:22
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

Copy link
Contributor

@jordan-powers jordan-powers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Dmitry!

Copy link
Member

@martijnvg martijnvg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds more production code than it removes and top of that there is another builder but now just for keyword field type.

I think the right change here is to remove the number of constructors here. Looks like 3 out of 5 constructors are exclusively used in tests. Let's try to reduce the number of constructors to just one or two. At best the rest of the constructors can be factory methods in testing code, or just removed.

@Kubik42 Kubik42 closed this Oct 23, 2025
@Kubik42 Kubik42 deleted the kubik-keyword-field-mapper-constructor-improvements branch October 23, 2025 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants