Code_Worm
Code_Worm

Reputation: 4470

pass type name to ElasticClient object Nest

I'm creating my index with custom mapping my problem is that methods corresponding to indexing such as index, createindex etc of ElasticClientonly get index name as input parameter and recognize type name from name of the class that being passed to them as generic parameter is there any way to pass type name to ElasticClient methods for example CreateIndex method and force it to accept that instead of using class name ???

here is part of my code

  var qq = Elasticclient.CreateIndex("testindex", a => a.Mappings(f => f.Map<BankPaymentLogModel>(
                          b => b.Properties(c => c.String(d => d.Name(e => e.testProperty))

                       ))));

any help would be appreciated

Upvotes: 0

Views: 915

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

There are a few options that you have for specifying a different type name to the one NEST will infer from the POCO name

1.Use the Map<T>(TypeName type, Func<TypeMappingDescriptor<T>, ITypeMapping>>) overload

var createIndexResponse = client.CreateIndex("testindex", a => a
    .Mappings(f => f
        .Map<BankPaymentLogModel>("my-type", b => b
            .Properties(c => c
                .String(d => d
                    .Name(e => e.testProperty)
                )
            )
        )
    )
);

Using this method however, would mean that you would need to call .Type("my-type") for every request where BankPaymentLogModel POCO is used, so that the same type name is sent in the request. So, the following options may be better

2.Use ElasticsearchTypeAttribute on BankPaymentLogModel type to specify the type name

[ElasticsearchType(Name = "my-type")]
public class BankPaymentLogModel
{
    public string testProperty { get; set; }
}

var createIndexResponse = client.CreateIndex("testindex", a => a
    .Mappings(f => f
        .Map<BankPaymentLogModel>(b => b
            .Properties(c => c
                .String(d => d
                    .Name(e => e.testProperty)
                )
            )
        )
    )
);

3.Or if you don't like attributes, a default type name can be configured on ConnectionSettings for BankPaymentLogModel

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
    .InferMappingFor<BankPaymentLogModel>(m => m
        .TypeName("my-type")
    );

var client = new ElasticClient(connectionSettings);

var createIndexResponse = client.CreateIndex("testindex", a => a
    .Mappings(f => f
        .Map<BankPaymentLogModel>(b => b
            .Properties(c => c
                .String(d => d
                    .Name(e => e.testProperty)
                )
            )
        )
    )
);

All 3 options above produce the following request json

PUT http://localhost:9200/testindex
{
  "mappings": {
    "my-type": {
      "properties": {
        "testProperty": {
          "type": "string"
        }
      }
    }
  }
}

Upvotes: 1

Related Questions