1. 程式人生 > >ElasticSearch60:Java api操作ElasticSearch5.2

ElasticSearch60:Java api操作ElasticSearch5.2

1.建立Maven專案的時候,因為是公司內網,所以需要解決代理的問題,配置代理

2.建立Maven專案

3.配置pom.xml,引用jar包

注意:本文使用的ES是5.2版本的,所以下載的jar包必須在5.0以上,否則執行報錯

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.h3c</groupId>
  <artifactId>es4j_test003</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>es4j_test003 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>5.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>transport</artifactId>
      <version>5.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.7</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.1.3</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>es4j_test003</finalName>
  </build>
</project>


4.編碼

建立package,並建立ElasticSearch4Java測試類

package com.ximua.es4j;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Before; import org.junit.Test; import java.net.InetAddress; import java.util.HashMap; import java.util.List; import java.util.Map; public class ElasticSearch4Java { private
TransportClient client; private IndexRequest source; /** * 獲取連線, 第一種方式 * @throws Exception */ @Before public void before() throws Exception { //設定叢集名稱 Map<String, String> map = new HashMap<String, String>(); map.put("cluster.name", "elasticsearch"); Settings settings = Settings.builder().put(map).build(); //建立客戶端client client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), Integer.parseInt("9300"))); } /** * 檢視叢集資訊 */ @Test public void NodeHealthInfoTest() { List<DiscoveryNode> nodes = client.connectedNodes(); for (DiscoveryNode node : nodes) { System.out.println(node.getHostAddress()); } } //api 查詢 @Test public void SearchDocumentTest(){ GetResponse response = client.prepareGet("website","article","1").execute().actionGet(); String result = response.getSourceAsString(); System.out.println(result); } }

執行結果:

1)檢視叢集IP地址:

127.0.0.1

2)查詢

{
  "title":"second article",
  "content":"this is my second article",
  "post_date":"2017-01-01",
  "author_id":100
}