Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 567
ryanmcfall Avatar asked Oct 22 '13 13:10

ryanmcfall


1 Answers

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)
        )
    )
);
like image 146
Joe Waller Avatar answered Oct 17 '22 01:10

Joe Waller