How Aggs use template like "template query"?


(Wanglifengwf) #1

How Aggs use template like "template query"?


(Colin Goodheart-Smithe) #2

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


(Wanglifengwf) #3

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();

(Colin Goodheart-Smithe) #4

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


(Wanglifengwf) #5

this happened error!

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

(Colin Goodheart-Smithe) #6

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.


(Wanglifengwf) #7

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


(system) #8