0

What is the right way to create index by using NEST v.5? I saw the similar post here: Specifying and using a NGramTokenizer with the C# NEST client for Elastic Search. But it seems that API was changed. I can do it the following way:

ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));

IndexSettings indexSettings = new IndexSettings();
            CustomAnalyzer customAnalyzer = new CustomAnalyzer();

customAnalyzer.Tokenizer = "mynGram";
            customAnalyzer.Filter = new List<string> { "lowercase" };

            indexSettings.Analysis.Analyzers.Add("mynGram", customAnalyzer);
            indexSettings.Analysis.Tokenizers.Add("mynGram", 
                                                  new NGramTokenizer
                                                  {
                                                      MaxGram = 10,
                                                      MinGram = 2
                                                  });

elasticClient = new ElasticClient(settings);

            elasticClient.CreateIndex("taskmanager", s => s
                .Settings(sett => sett
                    .Analysis(a => a
                        .Analyzers(anl => anl
                            .Custom("customAnalyzer", c => c
                                // how to set my custom analyzer?
                                .Tokenizer("mynGram")

                            )
                        )
                    )
                )
            );

The problem is that I don't know how to set my settings by using fluent API.

1 Answer 1

1

I found the answer on blog: ELASTIC SEARCH : CREATE INDEX USING NEST IN .NET

The first I need to create an index settings and custom analyzer:

IndexSettings indexSettings = new IndexSettings();
CustomAnalyzer customAnalyzer = new CustomAnalyzer();

Then we need to set our tokenizer and filter to the custom analyzer.

customAnalyzer.Tokenizer = "mynGram";
            customAnalyzer.Filter = new List<string> { "lowercase" };

            indexSettings.Analysis.Analyzers.Add("mynGram", customAnalyzer);
            indexSettings.Analysis.Tokenizers.Add("mynGram",
                                                  new NGramTokenizer
                                                  {
                                                      MaxGram = 10,
                                                      MinGram = 2
                                                  });

My mistake was I didn't use IndexState which contains our settings.

IndexState indexConfig = new IndexState
{
   Settings = indexSettings
};

elasticClient.CreateIndex("mycustomname", i => i .InitializeUsing(indexConfig) );

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.