Search code examples
elasticsearchnest

How to define custom Filter in index mapping using nest 7


How would I define a customer filter using nest 7 in c#. Given the curl example below....

curl -X POST http://127.0.0.1:9200/tryoindex/ -d'
{
  "settings": {
    "analysis": {
      "filter": {
        "custom_english_stemmer": {
          "type": "stemmer",
          "name": "english"
        }
      },

I don't have the Filter property available in the Analysis object!!. What is the syntax to convert the curl example to nest 7 code.

var inxResp = client.Indices.Create(indexName, c => c          
                .Index(indexName)
                .Settings(s => s
                    .NumberOfShards(1)
                    .NumberOfReplicas(0)
                    .Analysis(a => a
                    )

Solution

  • fluent API/syntax

    var createIndexResponse = client.Indices.Create("tryoindex", c => c
        .Settings(s => s
            .Analysis(a => a
                .TokenFilters(tf => tf
                    .Stemmer("custom_english_stemmer", st => st
                        .Language("english")
                    )
                )
            )
        )
    );
    

    or object initializer API/syntax

    var createIndexResponse = client.Indices.Create(new CreateIndexRequest("tryoindex")
    {
        Settings = new IndexSettings
        {
            Analysis = new Analysis
            {
                TokenFilters = new TokenFilters
                {
                    { "custom_english_stemmer", new StemmerTokenFilter 
                        {
                            Language = "english"
                        }
                    }
                }
            }
        }
    });