欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch的java客户端demo

程序员文章站 2022-07-01 15:29:28
...

相关依赖

<dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.5.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

代码

package cn.itcast.es;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TestESREST {

    private RestClient restClient;
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Before
    public void init(){
        RestClientBuilder restClientBuilder = RestClient.builder(
                new HttpHost("192.168.142.128", 9200),
                new HttpHost("192.168.142.128", 9201),
                new HttpHost("192.168.142.128", 9202)
        );

        restClientBuilder.setFailureListener(new RestClient.FailureListener(){

            @Override
            public void onFailure(Node node) {
                System.out.println("出错-->" + node);
            }
        });
        this.restClient = restClientBuilder.build();

    }

    @After
    public void close() throws IOException {
        // 跟随应用的关闭而关闭
        this.restClient.close();;
    }

    @Test
    public void testInfo() throws IOException {
        Request request = new Request("GET", "/_cluster/health");
        request.addParameter("pretty","true");
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    @Test
    public void testSave() throws Exception {
        Request request = new Request("POST", "/haoke/house");
        request.addParameter("pretty","true");
        // 构造 数据
        Map<String, Object> data = new HashMap<>();
        data.put("id", 2001);
        data.put("title", "深圳洪浪北新安三路 一室一厅");
        data.put("price", 3500);

        String json = MAPPER.writeValueAsString(data);
        request.setJsonEntity(json);
        Response response = this.restClient.performRequest(request);
        System.out.println(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    // 根据id查询数据
    @Test
    public void testQueryData() throws IOException {
        Request request = new Request("GET", "/haoke/house/ZvEKR3ABKJ9KQna8XS08");
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    // 搜索数据
    @Test
    public void testSearchData() throws IOException {
        Request request = new Request("POST", "/haoke/house/_search");
        String searchJson = "{\"query\": {\"match\": {\"title\": \"拎包入住\"}}}";
        request.setJsonEntity(searchJson);
        request.addParameter("pretty","true");
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
}

 

 

 

相关标签: ElasticSearch