Andrzej
Andrzej

Reputation: 571

Mapping particular type to ElasticSearch index

I'm learning elasticsearch from this awesome tutorial. To create the 'nusearch' index sample code is given:

        Client.CreateIndex("nusearch", i => i
            .Settings(s => s
                .NumberOfShards(2)
                .NumberOfReplicas(0)
                .Analysis(Analysis)
            )
            .Mappings(m => m
                .Map<Package>(MapPackage)
            )
        );

There is a .Mappings section which tells to NEST that Package class will be stored in "nusearch" index. Why do I get Error like that:

Index name is null for the given type and no default index is set. Map an index name using ConnectionSettings.MapDefaultTypeIndices() or set a default index using ConnectionSettings.DefaultIndex().'

when trying to index Package in that way?:

        foreach (Package pack in packages)
        {
            Client.Index<Package>(pack);
        }

ConnectionSettings configuration is reduced to that:

        _connectionSettings = new ConnectionSettings(CreateUri(9200));

I'm wondering why is necessary to add to ConnectionSettings information again that Package type should be stored in particular index? Is it necessary to Map particular POCOs in ConnectionSettings? What if application is large and you have a lot of types to map? isn't that awkward? It seems to me that CreateIndex method is better place to provide that sort of information.

I've already checked that it is possible by code like that:

                    _connectionSettings = new ConnectionSettings(CreateUri(9200))
                        .MapDefaultTypeIndices(m => m.Add(typeof(Package), "nusearch"))

or:

                .InferMappingFor<Package>(i => i
                .TypeName("package")
                .IndexName("nusearch"))
            )

Method InferMappingFor is extended version of MapDefaultTypeIndices?

If you can additionallly let me know: What does .TypeName method exactly means for ElasticSearch? What is best practise to use it? It works when is omitted so I think that has meaning when searching?

thanks for your replies in advance.

Upvotes: 1

Views: 1244

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

Why do I get Error like that:

Index name is null for the given type and no default index is set. Map an index name using ConnectionSettings.MapDefaultTypeIndices() or set a default index using ConnectionSettings.DefaultIndex().'

when trying to index Package in that way?

The error message tells you why; there is no index name for the request. An index name must be provided to Elasticsearch in order for it to know in which index to index the document. NEST is able to infer an index name from a number of different places in order of precedence, so at least one of these must be set.

I'm wondering why is necessary to add to ConnectionSettings information again that Package type should be stored in particular index?

You don't have to map a particular type against a particular index, but it is pretty common to do so. If you decide not to then you either explicitly specify the index on every request which is cumbersome, or you use the default index, which may not be what you want to do if you have multiple indices each with a different type.

Is it necessary to Map particular POCOs in ConnectionSettings?

No, but it is an option.

What if application is large and you have a lot of types to map? isn't that awkward?

You can map all types, or decide not to, it's up to you. I'd say it's less awkward to define a convention of POCO type -> index once on ConnectionSettings than need to supply it on every request.

It seems to me that CreateIndex method is better place to provide that sort of information.

I disagree; every usage of the client may not be creating indices (they may already exist, perhaps even created by another process), so it makes sense to tie convention setup to singleton configuration options, which is what ConnectionSettings are and how they should be used.

Method InferMappingFor is extended version of MapDefaultTypeIndices?

Yes, InferMappingFor<T> can map

  • index name
  • type name
  • POCO property that should be used for the _id
  • properties to ignore
  • properties map to field names in Elasticsearch (if different from the default convention of simply camelcasing the POCO property name).

What does .TypeName method exactly means for ElasticSearch? What is best practise to use it?

Allows you to specify a different type name for the POCO than what will be inferred by default (lower casing POCO type name).

Upvotes: 1

Related Questions