Search code examples
c#asp.net-web-apielasticsearchnestnest2

How to set document name in NEST 2.0


I have a Dictionary<string, object> which is being inserted into ElasticSearch 2.0 using NEST 2.0 library.

The default document name is Dictionary'2. I don't know why NEST choose this name but I'd like to have something more sensible like "DataRecord"

How can I set the document name?

Second option would be to create an empty wrapper class but it seems a little bit overkill


Solution

  • I think the easiest way is to define default type names on ConnectionSettings.

    var settings = new ConnectionSettings()
        .DefaultIndex(indexName)
        .MapDefaultTypeNames(dictionary => dictionary.Add(typeof(Dictionary<string,object>), "yourTypeName"))
        .DisableDirectStreaming()
        .PrettyJson();
    
    var client = new ElasticClient(settings);
    

    With this approach you don't have to bother yourself with putting type name whenever you are indexing documents or searching.

    In case you need more "control" over the process you can just put type parameter when calling elasticsearch, but you have to remember about this parameter in all places.

    var indexResponse = client.Index(new Dictionary<string, object>
    {
        {"asd", 1}
    }, descriptor => descriptor.Type("yourTypeName"));