Prashant Mohite
Prashant Mohite

Reputation: 798

Add the custom analyzer universally to all the fields in Nest Elastic search 6.2

I have an elastic search index that has more than 100 properties. I want to add the custom analyzer for most of the fields. I want to avoid typical nest syntax to specify the analyzer per field.
writing Analyzers

Let me know if there is any other alternative to set the analyzer as a universal setting. Please let me know your thoughts on the same. Thanks in advance.

Upvotes: 0

Views: 940

Answers (1)

Russ Cam
Russ Cam

Reputation: 125498

You can change the default analyzer for all text fields within an index by adding an analyzer with the name "default" to the index settings when creating the index

var defaultIndex = "my_index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

var createIndexResponse = client.CreateIndex(defaultIndex, c => c
    .Settings(s => s
        .Analysis(a => a
            .Analyzers(an => an
                .Standard("default", st => st
                    .StopWords("_english_")
                )
            )
        )
    )
);

which will send a create index request with the following body

{
  "settings": {
    "analysis": {
      "analyzer": {
        "default": {
          "type": "standard",
          "stopwords": [
            "_english_"
          ]
        }
      }
    }
  }
}

If you wanted this to apply to all indices created, you can use an index template to apply this convention to all automatically created indices

var putIndexTemplateResponse = client.PutIndexTemplate("default_analyzer", t => t
    .IndexPatterns("*")
    .Order(0)
    .Settings(s => s
        .Analysis(a => a
            .Analyzers(an => an
                .Standard("default", st => st
                    .StopWords("_english_")
                )
            )
        )
    )
);

Upvotes: 1

Related Questions