How Aggs use template like "template query"?

How Aggs use template like "template query"?

Could you provide some details on what you are actually trying to do here?

1 Like

this is my template query demo;

client.preparePutIndexedScript("mustache","first_template",
                "{\n" +
                "  \"template\":{\n" +
                "    \"query\": {\n" +
                "      \"term\": {\n" +
                "        \"event_type\": {\n" +
                "          \"value\": \"{{event_type}}\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}").get();

how can I do this:

        client.preparePutIndexedScript("mustache","first_template_aggs",
                "{\n" +
                        "  \"template\":{\n" +
                        "  \"aggs\": {\n" +
                        "    \"test\": {\n" +
                        "      \"terms\": {\n" +
                        "        \"field\": \"event_type\",\n" +
                        "        \"size\": 10\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }\n" +
                        "  }\n" +
                        "}").get();

Why are you wanting to template that request? It doesn't contain any placeholder values. In any case, you can template the whole of the search request using the Search Templates feature: https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-template.html

this happened error!

client.prepareSearch("index")
                .setQuery(QueryBuilders.templateQuery("first_template_aggs", ScriptService.ScriptType.INDEXED, map)).setSize(0);

That is the template query. You need to use the search template feature. You need to use the .setTemplate() method on the SearchRequestBuilderif you are using the Java API.

I'm unclear though why you are wanting to use search templates with the JAVA API. It would be much easier and more performant for you to build the full search request (not using templates) in Java code and execute that. The use case for search templates is really for the rest type clients.

1 Like

thanks ! I think it is more flexible,The future need only modify the template, do not need to modify the Java code