Using SearchTemplates in Elasticsearch 6.6

All,
I've been trying to parametrize size and from in the searchtemplate and invoke the template from NEST 6.6.
I can't get the following searchtemplate to work(It is returning all results) while invoking the template. We are trying to limit the NEST queries we are writing and moving all the queries into searchtemplates. Someone please let me know whats wrong with this or is it even possible to parametrize the size and from in the template. I saw a simlar question C# NEST 5 Search with template for NEST 5.x, but the same NEST api seem slike is discontinued in 6.6. I would like to do this through SearchTemplate though. Please see the details below

Template definition

PUT _scripts/customers_by_email
{
  "script": {
    "lang": "mustache",
    "source": """
    {
      "from":"{{from}}{{^from}}0{{/from}}",
      "size":"{{size}}{{^size}}20{{/size}}"
    },
    {
      "query":{
        "bool":{
          "filter":{
            "terms":{
              "email":
              {{#toJson}}emails{{/toJson}}
            }
          }
        }
      }
    }"""
  }
}

GET _render/template
{
  "id":"customers_by_email",
  "params": {
    "emails":["rennishj@test.com"]
  }
}

GET customer/_search/template
{
  "id":"customers_by_email",
  "params": {
    "emails":["rennishj@test.com"]
  }
}

customer mapping:

email is keyword

sample customer document:

PUT customer/_doc/4
{
  "firstName":"Niah",
  "lastName":"Joseph",
  "email":["niah@test.com"]
}

customer mapping details:

PUT _template/customer_template
{
  "order": 0,
  "index_patterns": ["customer*"],
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "autoComplete_filter": {
            "type": "edge_ngram",
            "min_gram": "2",
            "max_gram": "20"
          }
        },
        "analyzer": {
          "autoComplete": {
            "filter": ["lowercase", "asciifolding","autoComplete_filter"],
            "type": "custom",
            "tokenizer": "whitespace"
          },
          "default": {
            "filter": ["lowercase", "asciifolding"],
            "type": "custom",
            "tokenizer": "whitespace"
          }
        }
      },
      "number_of_shards": "3",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "_doc": {
      "dynamic": false,
      "properties": {
        "firstName": {
          "type": "keyword",
          "fields": {
            "search": {
              "type": "text",
              "analyzer": "autoComplete",
              "search_analyzer": "default"
            }
          }
        },
        "lastName": {
          "type": "keyword",
          "fields": {
            "search": {
              "type": "text",
              "analyzer": "autoComplete",
              "search_analyzer": "default"
            }
          }
        },
        "email": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT customer/_doc/4
{
  "firstName":"Niah",
  "lastName":"Joseph",
  "email":["niah@test.com"]
}

Invoking the template from NEST

List<string> emails = new List<string>(){"rennishj@test.com"};
var searchResponse = client.SearchTemplate<object>(st => st
    .Index("customer")
    .Id("customers_by_email")
    .Params(p => p
        .Add("emails", emails.ToArray())
        .Add("from", 0)		
        .Add("size", 50)
    )
);

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.