Need concrete documentation / examples of building complex index using NEST ElasticSearch library

独自空忆成欢 提交于 2019-11-30 14:33:00
Joe Waller

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)
        )
    )
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!