首页 > 解决方案 > 如何在 elasticsearch 嵌套中添加条件属性以创建索引?

问题描述

我想创建具有某些条件的索引,例如使用 querycontainer 添加条件过滤器。

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)));
            }

但我收到以下错误,你能指导我如何实现这一点。

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

标签: elasticsearchnestelasticsearch-7

解决方案


您快到了,只需将Properties部分更改为m.Properties(p => ps).

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

希望有帮助。


推荐阅读