如何使用NEST客户端使用CreateMappingDescriptor为弹性搜索创建索引?

时间:2017-09-12 17:43:43

标签: .net elasticsearch nest

我已成功为我的索引创建了一个描述符。已经创建了索引。生成的响应是有效的响应。 我不知道如何使用描述符。

var defaultIndex = "mynestclient";
var node = new Uri("http://localhost:9200");
 var settings = new ConnectionSettings(node
            ).DefaultIndex(defaultIndex);
        var client = new ElasticClient(settings);
        var clientcreated= client.CreateIndex(defaultIndex);
        Console.WriteLine("Index Created");

var descriptor = new CreateIndexDescriptor(defaultIndex)
                    .Settings(s => s
                    .Analysis(a => a
                        .TokenFilters(t => t
                            .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1)))
                        .Analyzers(an => an
                            .Custom("my_analyzer", ca => ca
                                .Tokenizer("standard")
                                .Filters("my_filter", "lowercase")
                            )
                        )
                    )
                )
                .Mappings(ms => ms
                    .Map<Investor>(m => m
                        .Properties(props => props
                            .Text(t => t
                                .Name(p => p.Name)
                                .Analyzer("my_analyzer")
                            )
                            .Text(t => t
                                .Name(p => p.SomeOtherField)
                                .Analyzer("my_analyzer"))
                            .Boolean(b => b
                                .Name(p => p.IsActive))));

我已经通过了文档。使用描述符并不多。 此外,如果任何人可以指向我周围的文档,指定PutMappingDescriptors和CreateIndexDescriptors之间的差异,或同样,这将是有帮助的。 任何投入都非常赞赏。

1 个答案:

答案 0 :(得分:2)

描述符是使用NEST流畅地建立对Elasticsearch端点的请求的类型。然后将描述符传递给客户端上的相应方法。例如,

var descriptor = new CreateIndexDescriptor(defaultIndex)
                    .Settings(s => s
                    .Analysis(a => a
                        .TokenFilters(t => t
                            .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1)))
                        .Analyzers(an => an
                            .Custom("my_analyzer", ca => ca
                                .Tokenizer("standard")
                                .Filters("my_filter", "lowercase")
                            )
                        )
                    )
                )
                .Mappings(ms => ms
                    .Map<Investor>(m => m
                        .Properties(props => props
                            .Text(t => t
                                .Name(p => p.Name)
                                .Analyzer("my_analyzer")
                            )
                            .Text(t => t
                                .Name(p => p.SomeOtherField)
                                .Analyzer("my_analyzer"))
                            .Boolean(b => b
                                .Name(p => p.IsActive))));

var createIndexResponse = client.CreateIndex(defaultIndex, c => descriptor);

CreateIndex()方法中的第二个参数是Func<CreateIndexDescriptor, ICreateIndexRequest>,因此c参数是传递给委托的CreateIndexDescriptor,因此您可以简单地构建参数从而改为

var createIndexResponse = client.CreateIndex(defaultIndex, c => c
      .Settings(s => s
        .Analysis(a => a
            .TokenFilters(t => t
                .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1)))
            .Analyzers(an => an
                .Custom("my_analyzer", ca => ca
                    .Tokenizer("standard")
                    .Filters("my_filter", "lowercase")
                )
            )
        )
    )
    .Mappings(ms => ms
        .Map<Investor>(m => m
            .Properties(props => props
                .Text(t => t
                    .Name(p => p.Name)
                    .Analyzer("my_analyzer")
                )
                .Text(t => t
                    .Name(p => p.SomeOtherField)
                    .Analyzer("my_analyzer"))
                .Boolean(b => b
                    .Name(p => p.IsActive))))
    )
);

使用描述符的API设计是,不需要自己实例化描述符的新实例,方法需要Func<TRequestDescriptor, TRequestInterface>为您创建描述符,然后您只需要调用所需的方法,在请求中设置所需的属性,类似于您对.Settings().Mappings()等所做的操作。这称为Fluent Lambda语法。

NEST也为所有API提供Request类型(称为Object Initializer语法),如果Func委托和lambda表达式不是你的东西。请求类型在每种情况下也实现相关的TRequestInterface,并且NEST客户端上的每个方法都被重载以获取TRequestInterface。与上面示例等效的Object Initializer是

var createIndexRequest = new CreateIndexRequest(defaultIndex)
{
    Settings = new IndexSettings
    {
        Analysis = new Analysis
        {
            TokenFilters = new TokenFilters
            {
                { "my_filter", new NGramTokenFilter
                    {
                        MaxGram = 32,
                        MinGram = 1
                    } 
                }
            },
            Analyzers = new Analyzers
            {
                { "my_analyzer", new CustomAnalyzer
                    {
                        Tokenizer = "standard",
                        Filter = new[] {"my_filter", "lowercase" }
                    }
                }
            }
        }
    },
    Mappings = new Mappings
    {
        { typeof(Investor), new TypeMapping
            {
                Properties = new Properties<Investor>
                {
                    { i => i.Name, new TextProperty
                        {
                            Analyzer = "my_analyzer"
                        }
                    },
                    { i => i.SomeOtherField, new TextProperty
                        {
                            Analyzer = "my_analyzer"
                        }
                    },
                    { i => i.IsActive, new BooleanProperty() }
                }
            }
        }
    }
};

var createIndexResponse  = client.CreateIndex(createIndexRequest);

使用Fluent Lambda语法,Object Initializer语法或两者,它取决于您:)

  

此外,如果有人可以指向我关于NEST的文档,该文档指定了PutMappingDescriptors和CreateIndexDescriptors之间的区别,或者同样,那将会有所帮助。

PutMappingDescriptor将映射放入现有索引,而CreateIndexDescriptor创建索引,您可以选择同时创建映射。该主题在文档中的introduction to the mapping section中进行了讨论。