1. Cree dependencias con maven, pom.xml: basado en elasticsearch7.9.1
<dependencias>
<dependencia>
<groupId> junit </groupId>
<artifactId> junit </artifactId>
<version> 4.12 </version>
<scope> probar </scope>
</dependencia>
<dependencia>
<groupId> org.elasticsearch.client </groupId>
<artifactId> elasticsearch-rest-high-level-client </artifactId>
<version> 7.9.1 </version>
</dependencia>
<dependencia>
<groupId> org.apache.httpcomponents </groupId>
<artifactId> httpcore </artifactId>
<version> 4.4.13 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/commons-logging/commons-logging ->
<dependencia>
<groupId> commons-logging </groupId>
<artifactId> commons-logging </artifactId>
<version> 1.2 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core ->
<dependencia>
<groupId> com.fasterxml.jackson.core </groupId>
<artifactId> jackson-core </artifactId>
<version> 2.11.2 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind ->
<dependencia>
<groupId> com.fasterxml.jackson.core </groupId>
<artifactId> jackson-databind </artifactId>
<version> 2.11.2 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations ->
<dependencia>
<groupId> com.fasterxml.jackson.core </groupId>
<artifactId> anotaciones-jackson </artifactId>
<version> 2.11.2 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/joda-time/joda-time ->
<dependencia>
<groupId> joda-time </groupId>
<artifactId> joda-time </artifactId>
<version> 2.10.6 </version>
</dependencia>
<! - https://mvnrepository.com/artifact/com.carrotsearch/hppc ->
<dependencia>
<groupId> com.carrotsearch </groupId>
<artifactId> hppc </artifactId>
<version> 0.8.2 </version>
</dependencia>
</dependencias>
2.Crear índice, código principal:
importar java.util. *; import org.apache.http.HttpHost; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexRequest; importar org.elasticsearch.common.xcontent.XContentBuilder; prueba de clase pública { public static void main (String [] args) { HttpHost h1 = new HttpHost ("106.13.64.228", 9200, "http"); RestClientBuilder r2 = RestClient.builder (h1); // 创建 连接 Cliente RestHighLevelClient = new RestHighLevelClient (r2); // 创建 数据 Map <String, Object> doc1 = new HashMap <String, Object> (); doc1.put ("nombre_pantalla", "d_bharv1"); doc1.put ("cuenta_seguidores", 2000); doc1.put ("crear_en", "20-09-2015"); try { IndexRequest indexRequest = new IndexRequest ("testindex"). id ("1"). source (doc1); RequestOptions requestOptions = RequestOptions.DEFAULT; // 创建 index IndexResponse indexResponse = client.index (indexRequest, requestOptions); System.out.println (indexResponse); } captura (Excepción ex) { ex.printStackTrace (); } } }