{"id":22503181,"url":"https://github.com/afa-farkhod/elasticsearch-search-template-invoking-java-client","last_synced_at":"2026-05-02T13:34:14.840Z","repository":{"id":177743720,"uuid":"658521474","full_name":"afa-farkhod/Elasticsearch-Search-Template-Invoking-Java-Client","owner":"afa-farkhod","description":"Elasticsearch Search Template creation on dev tool \u0026 Invoke search template in Java client API","archived":false,"fork":false,"pushed_at":"2023-11-20T04:46:28.000Z","size":97,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-30T05:41:07.174Z","etag":null,"topics":["elasticsearch","java","resthighlevelclient","search-template"],"latest_commit_sha":null,"homepage":"https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/afa-farkhod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-26T01:14:40.000Z","updated_at":"2023-07-08T11:58:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"a791b59b-c024-4908-a0ef-2e385d3fe65e","html_url":"https://github.com/afa-farkhod/Elasticsearch-Search-Template-Invoking-Java-Client","commit_stats":null,"previous_names":["af4092/elasticsearch-search-template-invoking-java-client","afa-farkhod/elasticsearch-search-template-invoking-java-client"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/afa-farkhod/Elasticsearch-Search-Template-Invoking-Java-Client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afa-farkhod%2FElasticsearch-Search-Template-Invoking-Java-Client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afa-farkhod%2FElasticsearch-Search-Template-Invoking-Java-Client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afa-farkhod%2FElasticsearch-Search-Template-Invoking-Java-Client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afa-farkhod%2FElasticsearch-Search-Template-Invoking-Java-Client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afa-farkhod","download_url":"https://codeload.github.com/afa-farkhod/Elasticsearch-Search-Template-Invoking-Java-Client/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afa-farkhod%2FElasticsearch-Search-Template-Invoking-Java-Client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32536578,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T12:25:33.646Z","status":"ssl_error","status_checked_at":"2026-05-02T12:24:51.733Z","response_time":132,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elasticsearch","java","resthighlevelclient","search-template"],"created_at":"2024-12-06T23:29:59.993Z","updated_at":"2026-05-02T13:34:14.805Z","avatar_url":"https://github.com/afa-farkhod.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elasticsearch-Search-Template-Invoking-Java-Client\nElasticsearch Search Template creation on dev tool \u0026amp; Invoke search template in Java client API\n\n### [Search template](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html) \n- A search template is a stored search you can run with different variables. If you use Elasticsearch as a search backend, you can pass user input from a search bar as parameters for a search template. This lets you run searches without exposing Elasticsearch’s query syntax to your users. If you use Elasticsearch for a custom application, search templates let you change your searches without modifying your app’s code.\n\n### [Create a search template](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#create-search-template)\n- The request’s source supports the same parameters as the search API's request body. Source also supports Mustache variables which are typically enclosed in double curly brackets: `{{my-var}}`. When you run a templated search, Elasticsearch replaces these variables with values from params. Search templates must use a lang of `mustache`. Elasticsearch stores search templates as Mustache scripts in the cluster state. Elasticsearch compiles search templates in the template script context. Settings that limit or disable scripts also affect search templates.\n- The following request creates a search template with an id of `test-search-template`.\n```\nPUT _scripts/test-search-template\n{\n  \"script\": {\n    \"lang\": \"mustache\",\n    \"source\": {\n      \"query\": {\n        \"match\": {\n          \"message\": \"{{query_string}}\"\n        }\n      },\n      \"from\": \"{{from}}\",\n      \"size\": \"{{size}}\"\n    },\n    \"params\": {\n      \"query_string\": \"Hello Test\"\n    }\n  }\n}\n```\n\n### [Validate a search template](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#validate-search-template)\n- To test the template with different parameters, use the render search template API.\n```\nPOST _render/template\n{\n  \"id\": \"test-search-template\",\n  \"params\": {\n    \"query_string\": \"Test The Template\",\n    \"from\": 20,\n    \"size\": 10\n  }\n}\n```\n\n### [Run a templated search](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#run-templated-search)\n- To run a search with a search template, use the search template API. You can specify different parameters with each request.\n```\nGET sample-index/_search/template\n{\n  \"id\": \"test-search-template\",\n  \"params\": {\n    \"query_string\": \"Test The Template\",\n    \"from\": 0,\n    \"size\": 10\n  }\n}\n```\n### [Source Code Explanation](https://github.com/af4092/Elasticsearch-Search-Template-Invoking-Java-Client/blob/main/src/src/main/java/org/example/src/TextSearchTemplate.java)\n\n- Source code is located in the following path: `src\\main\\java\\org\\example\\src\\TextSearchTemplate.java`\n  - The code demonstrates the usage of the Elasticsearch `High-Level REST Client` to perform a search using a search template.\n  - The code sets up the Elasticsearch client connection by creating a `RestClientBuilder` instance and specifying the remote server's IP address, port and protocol (HTTP).\n  - Credentials are configured using the `BasicCredentialsProvider` and the provided username and password.\n  - The `RestHighLevelClient` is created using the configured `RestClientBuilder`.\n  - A `SearchTemplateRequest` object is created to define the search template request.\n  - The `SearchRequest` is set on the SearchTemplateRequest, specifying the index name where the search will be performed.\n  - The script type is set to `ScriptType.STORED`, indicating that the search template is stored on the Elasticsearch server.\n  - The name of the search template is set using the `setScript` method. In our case, it is set to \"test-search-template\".\n  - Template parameters are defined using a `HashMap`. In the example, the field name and value are specified as parameters.\n  - The template parameters are set on the SearchTemplateRequest using the `setScriptParams` method.\n  - The search is executed by calling the `client.searchTemplate` method, passing the `SearchTemplateRequest` and default request options.\n  - The response is obtained as a `SearchTemplateResponse`.\n  - The search response is extracted from the template response using the `getResponse` method.\n  - The hits (search results) are retrieved from the search response using the `getHits` method.\n  - The code iterates over the hits using a for loop and prints the source of each hit using `getSourceAsString`.\n  - The program handles `IOException` exceptions that may occur during the execution of the Elasticsearch operations and prints the stack trace if an exception occurs.\n  - Finally, the `close method` is called on the `RestHighLevelClient` to release resources.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafa-farkhod%2Felasticsearch-search-template-invoking-java-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafa-farkhod%2Felasticsearch-search-template-invoking-java-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafa-farkhod%2Felasticsearch-search-template-invoking-java-client/lists"}