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

巧了我就是萌 提交于 2019-11-29 20:35:34

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/19519449/need-concrete-documentation-examples-of-building-complex-index-using-nest-elas

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