需要具体的文档/使用NEST ElasticSearch库构建复杂索引的示例 [英] Need concrete documentation / examples of building complex index using NEST ElasticSearch library

查看:564
本文介绍了需要具体的文档/使用NEST ElasticSearch库构建复杂索引的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NEST库的Fluent界面创建一个索引,其中包括设置自定义过滤器,分析器和类型映射。我想避免使用NEST特定注释来装饰我的课程。



我已经在 http://nest.azurewebsites.net/indices/create-indices.html http://nest.azurewebsites.net/indices/put-mapping.html 。此文档虽然显示了一些示例,但还不够完整,可以帮助我了解如何使用Fluent API构建一些复杂的索引方案。



我已经找到了教程在 http://euphonious-intuition.com/2012/08/more-complicated- map-in-elasticsearch / 非常有帮助;一些代码显示了如何通过NEST Fluent界面代替直接JSON来构建本教程中的过滤器,分析器和映射,这个问题将是一个很好的答案。

解决方案

你的问题越具体,你收到的答案就越好。然而,这里是一个索引,它设置一个分析器(带过滤器)和tokenizer(EdgeNGram),然后使用它们在Tag类的Name字段上创建一个自动完成索引。



public class Tag
{
public string Name {get;组; }
}

Nest.IElasticClient client = null; //连接到ElasticSearch

var createResult = client.CreateIndex(indexName,index => index
.Analysis(analysis => analysis
.Analyzers(a => a
.Add(
autocomplete,
new Nest.CustomAnalyzer()
{
Tokenizer =edgeNGram,
Filter = new string [ ] {小写}
}


.Tokenizers(t => t
.Add(
edgeNGram,
new Nest.EdgeNGramTokenizer()
{
MinGram = 1,
MaxGram = 20
}



.AddMapping< Tag>(tmd => tmd
.Properties(props => props
.MultiField(p => p
.Name(t =>名称)
.Fields(tf => ; tf
.String(s => s
.Name(t => t.Name)
.Index(Nest.FieldIndexOption.not_analyzed)

.String(s => s
.Name(t => t.Name.Suffix(autocomplete))
.Index(Nest.FieldIndexOption.analyzed)
.IndexAnalyzer 自动完成)





);

NEST在github上的单元测试项目中也有一个相当完整的映射示例。
https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs



编辑:



要查询索引,请执行以下操作:

  string queryString =; // search string 
var results = client.Search< Tag>(s => s
.Query(q => q
.Text(tq => tq
.OnField(t => t.Name.Suffix(autocomplete))
.QueryString(queryString)


);


I would like to use the NEST library's Fluent interface to create an index, which involves setting up custom filters, analyzers, and type mappings. I would like to avoid decorating my classes with NEST-specific annotations.

I have seen the documentation at http://nest.azurewebsites.net/indices/create-indices.html and http://nest.azurewebsites.net/indices/put-mapping.html. This documentation, while showing some examples, is not complete enough to help me figure out how to use the Fluent API to build some complex indexing scenarios.

I have found the tutorial at http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/ to be quite helpful; some code showing how to build the filters, analyzers and mappings in this tutorial via the NEST Fluent interface in place of the straight JSON would be a great answer to this question.

解决方案

The more specific you can be with your question the better the answers you receive will be. Nevertheless, here is an index that sets up an analyzer (with filter) and tokenizer (EdgeNGram) and then uses them to create an autocomplete index on the Name field of a Tag class.

public class Tag
{
    public string Name { get; set; }
}

Nest.IElasticClient client = null; // Connect to ElasticSearch

var createResult = client.CreateIndex(indexName, index => index
    .Analysis(analysis => analysis
        .Analyzers(a => a
            .Add(
                "autocomplete",
                new Nest.CustomAnalyzer()
                {
                    Tokenizer = "edgeNGram",
                    Filter = new string[] { "lowercase" }
                }
            )
        )
        .Tokenizers(t => t
            .Add(
                "edgeNGram",
                new Nest.EdgeNGramTokenizer()
                {
                    MinGram = 1,
                    MaxGram = 20
                }
            )
        )
    )
    .AddMapping<Tag>(tmd => tmd
        .Properties(props => props
            .MultiField(p => p
                .Name(t => t.Name)
                .Fields(tf => tf
                    .String(s => s
                        .Name(t => t.Name)
                        .Index(Nest.FieldIndexOption.not_analyzed)
                    )
                    .String(s => s
                        .Name(t => t.Name.Suffix("autocomplete"))
                        .Index(Nest.FieldIndexOption.analyzed)
                        .IndexAnalyzer("autocomplete")
                    )
                )
            )
        )
    )
);

There is also a fairly complete mapping example in NEST's unit test project on github. https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs

Edit:

To query the index, do something like the following:

string queryString = ""; // search string
var results = client.Search<Tag>(s => s
    .Query(q => q
        .Text(tq => tq
            .OnField(t => t.Name.Suffix("autocomplete"))
            .QueryString(queryString)
        )
    )
);

这篇关于需要具体的文档/使用NEST ElasticSearch库构建复杂索引的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆