Elasticsearch-net: Exception on mapping/index create with multiple childs to the same parent: An item with the same key has already been added. :

Created on 28 Dec 2017  路  2Comments  路  Source: elastic/elasticsearch-net

NEST/Elasticsearch.Net version: 6.0.0-beta1

Elasticsearch version: 6.1.1

CreateIndex throws an "An item with the same key has already been added." argument exception when trying to configure the mapping for multiple childs having the same parent.

Steps to reproduce:

  1. Define one parent and two child documents with a base document including the JoinField
  2. Use AutoMap for the parent and childs.
  3. Additionally configure the parent/child relation, following the documentation on https://github.com/elastic/elasticsearch-net/blob/master/src/Tests/ClientConcepts/HighLevel/Mapping/ParentChildJoins.doc.cs.

    1. CreateIndex throws "An item with the same key has already been added." argument exception.

Snippet:

var index = client.CreateIndex(
    indexName,
    s => s.Settings(x => x
        .NumberOfShards(1)
        .NumberOfReplicas(0)
    )
    .Mappings(ms => ms

        .Map<SampleBase>(mx => mx
            .AutoMap<SampleParent>()
            .AutoMap<SampleChildOne>()
            .AutoMap<SampleChildTwo>()
            .Properties(props => props
                .Join(j => j
                    .Name(p => p.MyJoinField)
                    .Relations(r => r
                        .Join<SampleParent, SampleChildOne>()
                        .Join<SampleParent, SampleChildTwo>()
                    )
                )
            )
        )
    )
);

See also source:
SameKeyIndexProblem.txt

Exception:

Error: An item with the same key has already been added. :    at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Nest.Relations.Add(RelationName type, Children children)
   at Nest.IsADictionaryDescriptorBase`4.<>c__DisplayClass1_0.<Assign>b__0(TInterface a)
   at Nest.DescriptorPromiseBase`2.Assign(Action`1 assigner)
   at Nest.IsADictionaryDescriptorBase`4.Assign(TKey key, TValue value)
   at Nest.RelationsDescriptor.Join[TParent,TChild]()
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_6(RelationsDescriptor r) in Program.cs:line 63
   at Nest.JoinPropertyDescriptor`1.<>c__DisplayClass5_0.<Relations>b__0(IJoinProperty a)
   at Nest.Fluent.Assign[TDescriptor,TInterface](TDescriptor self, Action`1 assign)
   at Nest.DescriptorBase`2.Assign(Action`1 assigner)
   at Nest.JoinPropertyDescriptor`1.Relations(Func`2 selector)
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_5(JoinPropertyDescriptor`1 j) in Program.cs:line 61
   at Nest.PropertiesDescriptor`1.SetProperty[TDescriptor,TInterface](Func`2 selector)
   at Nest.PropertiesDescriptor`1.Join(Func`2 selector)
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_4(PropertiesDescriptor`1 props) in 
Program.cs:line 60
   at Nest.TypeMappingDescriptor`1.<>c__DisplayClass86_0.<Properties>b__0(ITypeMapping a)
   at Nest.Fluent.Assign[TDescriptor,TInterface](TDescriptor self, Action`1 assign)
   at Nest.DescriptorBase`2.Assign(Action`1 assigner)
   at Nest.TypeMappingDescriptor`1.Properties(Func`2 propertiesSelector)
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_3(TypeMappingDescriptor`1 mx) in Program.cs:line 56
   at Nest.MappingsDescriptor.Map[T](Func`2 selector)
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_2(MappingsDescriptor ms) in Program.cs:line 54
   at Nest.CreateIndexDescriptor.<>c__DisplayClass14_0.<Mappings>b__0(ICreateIndexRequest a)
   at Nest.Fluent.Assign[TDescriptor,TInterface](TDescriptor self, Action`1 assign)
   at Nest.CreateIndexDescriptor.Mappings(Func`2 selector)
   at SameKeyErrorProblem.Program.<>c.<Main>b__0_0(CreateIndexDescriptor s) in Program.cs:line 50
   at Nest.Extensions.InvokeOrDefault[T,TReturn](Func`2 func, T default)
   at Nest.ElasticClient.CreateIndex(IndexName index, Func`2 selector)
   at SameKeyErrorProblem.Program.Main(String[] args) in Program.cs:line 48
v6.0.0-rc1

Most helpful comment

The problem here is the following:

.Join<SampleParent, SampleChildOne>()
.Join<SampleParent, SampleChildTwo>()

NEST fluent methods are never additive. If you want to map multiple children use:

.Join<SampleParent>(typeof(SampleChildOne), typeof(SampleChildTwo))

I will add a commit to improve the error message here though 馃憤

All 2 comments

The problem here is the following:

.Join<SampleParent, SampleChildOne>()
.Join<SampleParent, SampleChildTwo>()

NEST fluent methods are never additive. If you want to map multiple children use:

.Join<SampleParent>(typeof(SampleChildOne), typeof(SampleChildTwo))

I will add a commit to improve the error message here though 馃憤

I see, a better error message will do the trick. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markwalsh-liverpool picture markwalsh-liverpool  路  12Comments

anuragdewangan20 picture anuragdewangan20  路  27Comments

Mpdreamz picture Mpdreamz  路  21Comments

russcam picture russcam  路  16Comments

ffMathy picture ffMathy  路  16Comments