Calling Elasticsearch Template Query using NEST?

973 views Asked by At

Is there a way to call Template Query using NEST? Is there any examples?

3

There are 3 answers

1
Greg Marzouka On BEST ANSWER

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:

var client = new ElasticClient(...);
client.Raw.SearchTemplate(...);
0
harvzor 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()
)
0
user2297083 On

Another question on similiar lines, Is PutSearchTemplateDescriptor the write method to call a pre-regsitered template?

I have registered the template to the .scripts but unable to find the right method to call the template from NEST client