java elasticsearch

1.用maven创建依赖,pom.xml:基于elasticsearch7.9.1

    <dependencies>

        <dependency>

          <groupId>junit</groupId>

          <artifactId>junit</artifactId>

          <version>4.12</version>

          <scope>test</scope>

        </dependency>

    

        <dependency>

    <groupId>org.elasticsearch.client</groupId>

    <artifactId>elasticsearch-rest-high-level-client</artifactId>

    <version>7.9.1</version>

 </dependency>

 

 <dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpcore</artifactId>

    <version>4.4.13</version>

</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->

<dependency>

    <groupId>commons-logging</groupId>

    <artifactId>commons-logging</artifactId>

    <version>1.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

    <version>2.11.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.11.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-annotations</artifactId>

    <version>2.11.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->

<dependency>

    <groupId>joda-time</groupId>

    <artifactId>joda-time</artifactId>

    <version>2.10.6</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.carrotsearch/hppc -->

<dependency>

    <groupId>com.carrotsearch</groupId>

    <artifactId>hppc</artifactId>

    <version>0.8.2</version>

</dependency>

  </dependencies> 


2.创建index,主要代码:

import 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;
import org.elasticsearch.common.xcontent.XContentBuilder;  
public class test {  
  public static void main(String[] args) {
    HttpHost h1 = new HttpHost("106.13.64.228", 9200, "http");
      RestClientBuilder r2 = RestClient.builder(h1);
      //创建连接
      RestHighLevelClient client = new RestHighLevelClient(r2);  
      //创建数据
      Map<String, Object> doc1 = new HashMap<String, Object>();
      doc1.put("scree_name", "d_bharv1");
      doc1.put("follower_count", 2000);
      doc1.put("create_at", "2015-09-20");
      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);
      } catch (Exception ex) {
         ex.printStackTrace();
     }
  }
}


猜你喜欢

转载自blog.51cto.com/hjun169/2536420