Skip to content

8.0 Feature request Add Fluent helper for DynamicTemplates #8465

Closed
@niemyjski

Description

@niemyjski
Contributor

Is your feature request related to a problem? Please describe.
The new list syntax leaves a lot ot be desired and a bit of a pain to upgrade to.

Describe the solution you'd like
.DynamicTemplates(t => t.DynamicTemplate("idx_text", t1 => t1.Match("text*").Mapping(m1 => m1.Text(mp => mp.AddKeywordAndSortFields()))))

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Activity

flobernd

flobernd commented on Mar 7, 2025

@flobernd
Member

Hi @niemyjski ,

I guess this is about this construct:

public ICollection<IDictionary<string, DynamicTemplate>>? DynamicTemplates { get; set; }

Specification

I recently made some changes to the related specification (elastic/elasticsearch-specification#3824). This alone does not help here, but I have a strong feeling that the actual type should be SingleKeyDictionary<string, DynamicTemplate>[] which would map to:

public ICollection<KeyValuePair<string, DynamicTemplate>>? DynamicTemplates { get; set; }

... which in theory is the same as a regular Dictionary, with the exception that this explicitly intends to preserve the order of items.

Will do

My plan is to expose this property as just IDictionary and take care about the ordering at serialization layer (e.g. by using OrderedDictionary<K,V>).

For descriptors, this would also be fully transparent to the user as you won't have to care about manually creating a dictionary.

The problem is with the regular object initializer syntax where the IDictionary could lead users to initialize the property using a regular dictionary (which might or might not preserve order of items; depending on the implementation). I have to think about this when I get to that point.

If my above assumtion is correct and we can get away with just IDictionary<string, DynamicTemplate>, the descriptor usage would look similar to:

.DynamicTemplates(templates => templates
    .Add("my_template", template => template.XXX()...)
    .Add("my_second_template", template => template.XXX().YYY()...)

Won't do

Other than that, the dictionary type will stay.

It's basically the same change that has been made for Aggregation where the name was previously pulled in to the object and now is external, in form of the dictionary key.

For the object representation, this means that the DynamicTemplate type itself won't have a property holding its name.

For the usages this means that we have to deal with IDictionary<string, DynamicTemplate> instead of ICollection<DynamicTemplate>.

niemyjski

niemyjski commented on Mar 7, 2025

@niemyjski
ContributorAuthor

That sounds good, just curious why ordering matters?

flobernd

flobernd commented on Mar 7, 2025

@flobernd
Member

@niemyjski I was surprised as well, but turns out Elasticsearch will always use the first matching template. That allows to add templates for very specific conditions first while still having more generic fallbacks later in the list.

And we already confirmed that my assumption was indeed correct and I can move forward with implementing this usability improvement 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @niemyjski@flobernd

      Issue actions

        8.0 Feature request Add Fluent helper for DynamicTemplates · Issue #8465 · elastic/elasticsearch-net