Search code examples
elasticsearchnestelasticsearch-7

How to add conditional properties for index creation in elasticsearch nest?


I want to create index with some condition,like with querycontainer to add conditional filters.

PropertiesDescriptor<object> ps = new PropertiesDescriptor<object>();
            if (condition)
            {
                ps.Text(s => s.Name(name[1]));
            }
            if(condition)
            {
                ps.Number(s => s.Name(name[1]));
            }
            if (!_con.client.Indices.Exists(indexname).Exists)
            {
                var createIndexResponse = _con.client.Indices.Create(indexname, index => index.Settings(s => s.NumberOfShards(1).NumberOfReplicas(0))
                                                                                                .Map(m=>m.Properties(ps)));
            }

But i receive following error, can you guide me how to acheive this.

cannot convert from 'Nest.PropertiesDescriptor<object>' to 'System.Func<Nest.PropertiesDescriptor<object>, Nest.IPromise<Nest.IProperties>>'


Solution

  • You are almost there, just change Properties part to m.Properties(p => ps).

    _con.client.Indices.Create(indexname, 
        index => index.Settings(s => s.NumberOfShards(1).NumberOfReplicas(0)).Map(m=>m.Properties(p => ps)));
    

    Hope that helps.