Is there a way to call Template Query using NEST? Is there any examples?
Calling Elasticsearch Template Query using NEST?
973 views Asked by M Kim At
3
There are 3 answers
0
On
The search template endpoint has been mapped in NEST 2.x.
There is a general example about templating here: https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/template-query-usage.html
Here is some information about how inline templates can be used in a phrase suggestion with the collate option: https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/suggest-usage.html
Here is an issue on GitHub I posted with some information on how to save templates to Elastic: https://github.com/elastic/elasticsearch-net/issues/2176
Here's a general example of how to use NEST:
var templateRequest= new PutSearchTemplateDescriptor(new Id("my_template"));
templateRequest.Template("{\"multi_match\":{\"query\":{\"query\":\"{{suggestion}}\",\"fields\":[\"field1\",\"field2\"]}}}");
var response = ElasticClient.PutSearchTemplate(templateRequest);
When using the template in a suggest collate:
.Collate(c => c
.Query(q => q
.Indexed("my_template")
)
.Prune()
)
The search template endpoint isn't mapped in NEST yet, and poses a bit of a challenge since it's very different to how queries are normally constructed. We're actually working on this now (in this branch) and are hoping to get this functionality in the upcoming 1.1 release. Here's a link to the original issue for tracking purposes.
EDIT: Forgot to mention, the endpoint is available on the low-level Elasticsearch.Net client, which you can access via
ElasticClient
: