Elastic Search Java REST Advanced Client Inline Template
ElasticSearch 7.2.0
1. Inline template
2. Create registration template
3. Execute registration template
4. Create Client
5.maven configuration - pom file
1. Inline template
/** * Inline template */ public static void inlineTemplate() { //Get Client var client = getClient(); SearchTemplateRequest request = new SearchTemplateRequest(); //Specified index request.setRequest(new SearchRequest("movies")); //Set to Inline request.setScriptType(ScriptType.INLINE); //Setup script request.setScript("{" + " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + "}"); Map<String, Object> scriptParams = new HashMap<>(); scriptParams.put("field", "title"); scriptParams.put("value", "life"); scriptParams.put("size", 5); request.setScriptParams(scriptParams); try { SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT); //Output query results response.getResponse().getHits().forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } finally { //Close the client close(client); } }
2. Create registration templates
If the client of the registration template uses the low-level client, the high-level client does not provide the registration template.
Registered Template recommends executing commands directly instead of in java. Registered Template in Java needs to be redistributed when changes are made.
/** * Registration template < br > * If the client of registration template uses low-level client, the high-level client does not provide registration template < br>. * Registered Template recommends executing commands directly instead of in java. Registered Template in Java needs to be republished < br> when changing. * @return Returns whether the registration template was successfully registered */ private static int registerTemplate() { var client = getLowClient(); Request scriptRequest = new Request("POST", "_scripts/movies_script"); String json = "{" + " \"script\": {" + " \"lang\": \"mustache\"," + " \"source\": {" + " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + " }" + " }" + "}"; scriptRequest.setJsonEntity(json); int statusCode = -1; try { Response response = client.performRequest(scriptRequest); // Response status lines from which status codes can be obtained statusCode = response.getStatusLine().getStatusCode(); } catch (IOException e) { e.printStackTrace(); } finally { close(client); } return statusCode; }
3. Execute registration template
/** * Execute registration template */ public static void runRegisterTemplate() { int statusCode = registerTemplate(); // Search templates can be pre-registered through stored script API s. // Note that the stored script API is not yet available in the advanced REST client, so in this case, we use the low-level REST client. if (statusCode == 200) { SearchTemplateRequest request = new SearchTemplateRequest(); request.setRequest(new SearchRequest("movies")); request.setScriptType(ScriptType.STORED); request.setScript("movies_script"); Map<String, Object> params = new HashMap<>(); params.put("field", "title"); params.put("value", "life"); params.put("size", 5); request.setScriptParams(params); // Given the parameter values, the template can be rendered without performing the search: request.setSimulate(true); request.setExplain(true); request.setProfile(true); RestHighLevelClient rHClient = getClient(); try { SearchTemplateResponse response = rHClient.searchTemplate(request, RequestOptions.DEFAULT); SearchTemplateRequest request2 = new SearchTemplateRequest(); request2.setRequest(new SearchRequest("movies")); request2.setScriptType(ScriptType.INLINE); request2.setScript(response.getSource().utf8ToString()); request2.setScriptParams(new HashMap<String, Object>()); response = rHClient.searchTemplate(request2, RequestOptions.DEFAULT); SearchResponse sResponse = response.getResponse(); SearchHits hits = sResponse.getHits(); if(hits != null) { for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); } } } catch (IOException e) { // TODO Automatically Generated catch Block e.printStackTrace(); }finally { try { rHClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
4. Create Client
Creating Advanced Client
Creating low-level clients
Close the client
/** * * Close the client * * @param client */ private static void close(Closeable client) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Get Client * * @return */ private static RestHighLevelClient getClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("elk-node01", 9200, "http"), new HttpHost("elk-node02", 9200, "http"), new HttpHost("elk-node03", 9200, "http"))); return client; } /** * Getting low-level clients * * @return */ private static RestClient getLowClient() { RestClient client = RestClient.builder(new HttpHost("elk-node01", 9200, "http"), new HttpHost("elk-node02", 9200, "http"), new HttpHost("elk-node03", 9200, "http")).build(); return client; }
5.maven configuration - pom
Elastic search Java REST advanced client maven configuration