您可以使用RestTemplate。
这是我尝试过的方法,为了测试,我制作了简单的 get api,它创建一个索引并在之后更新它。
@GetMapping
public Map<String, Object> updateEsMapping() throws JsonProcessingException {
//Create mapping index
final Map<String, Object> request =
Map.of("properties", Map.of(
"id", Map.of("type", "keyword"),
"name", Map.of("type", "keyword"),
"date", Map.of("type", "date")
));
//Compose json header
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//Create entity
final HttpEntity<String> entity = new HttpEntity<String>(objectMapper.writeValueAsString(request), headers);
//es constants
final String host = "http://localhost:9200/";
final String indexName = "raw-sample-document-creation/";
final String mapping = "_mapping/";
//First create index
restTemplate.put(host + indexName, null);
//Update mapping
ResponseEntity<String> exchange = restTemplate.exchange(host + indexName + mapping, HttpMethod.PUT, entity, String.class);
return Map.of("status", "ok", "request", exchange.getBody()
.replace("\n", "")
.replace("\r", "")
);
}
您可以在此处探索更多标准方法
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients