java elasticsearch

1. maven、pom.xmlで依存関係を作成します:elasticsearch7.9.1に基づく

    <依存関係>

        <依存関係>

          <groupId> junit </ groupId>

          <artifactId> junit </ artifactId>

          <version> 4.12 </ version>

          <scope> test </ scope>

        </依存関係>

    

        <依存関係>

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

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

    <version> 7.9.1 </ version>

 </依存関係>

 

 <依存関係>

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

    <artifactId> httpcore </ artifactId>

    <version> 4.4.13 </ version>

</依存関係>

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

<依存関係>

    <groupId> commons-logging </ groupId>

    <artifactId> commons-logging </ artifactId>

    <version> 1.2 </ version>

</依存関係>

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

<依存関係>

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

    <artifactId> jackson-core </ artifactId>

    <version> 2.11.2 </ version>

</依存関係>

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

<依存関係>

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

    <artifactId> jackson-databind </ artifactId>

    <version> 2.11.2 </ version>

</依存関係>

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

<依存関係>

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

    <artifactId> jackson-annotations </ artifactId>

    <version> 2.11.2 </ version>

</依存関係>

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

<依存関係>

    <groupId> joda-time </ groupId>

    <artifactId> joda-time </ artifactId>

    <version> 2.10.6 </ version>

</依存関係>

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

<依存関係>

    <groupId> com.carrotsearch </ groupId>

    <artifactId> hppc </ artifactId>

    <version> 0.8.2 </ version>

</依存関係>

  </依存関係> 


2.インデックスの作成、メインコード:

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);  printStackTrace();  
  }
      //创建数据
      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"); 
      { 
          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(例外ex){ 
         ex.printStackTrace(); 
     }
}


おすすめ

転載: blog.51cto.com/hjun169/2536420