JavaRestClient操作Elasticsearch基本文档的CRUD

导包

因为我用的springboot做的测试

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
         <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.yml配置

集群配置,这里主要是为了SpringDataElasticsearch

spring:
  data:
    elasticsearch:
      cluster-name: text-elastic #集群名称
      cluster-nodes: 127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303 #有多少机器,写多少

新增方法

import com.google.gson.Gson;
import com.leyou.pojo.Item;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Esdemo1 {

    private RestHighLevelClient client;

    private Gson gson = new Gson();
    
    /**
     * 先执行
     */
    @Before
    public void init (){
        // 初始化HighLevel客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://127.0.0.1:9201"),
                        HttpHost.create("http://127.0.0.1:9202"),
                        HttpHost.create("http://127.0.0.1:9203")
                )
        );
    }
    
/*
*	新增文档
*/
    @Test
    public void testAddIndex() throws IOException {
        // 准备文档数据:
        Item item = new Item(1L, "小米手机9", " 手机","小米", 3499.00, "http://image.csdn.com/13123.jpg");
        // 转为Json格式:
        String toJson = gson.toJson(item);
        // 创建一个新增索引的请求,并指定是JSON格式
        IndexRequest indexRequest = new IndexRequest("索引名", "类型名", item.getId().toString()).source(toJson, XContentType.JSON);
        // 发起请求
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println("Response:"+indexResponse);
    }

    /**
     * 后执行
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

查看文档

import com.google.gson.Gson;
import com.leyou.pojo.Item;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Esdemo1 {

    private RestHighLevelClient client;

    private Gson gson = new Gson();
    /**
     * 先执行
     */
    @Before
    public void init (){
        // 初始化HighLevel客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://127.0.0.1:9201"),
                        HttpHost.create("http://127.0.0.1:9202"),
                        HttpHost.create("http://127.0.0.1:9203")
                )
        );
    }

    /**
     * 查看文档
     */
    @Test
    public void testFindIndex() throws IOException {
        // 创建get请求,并指定id
        GetRequest getRequest = new GetRequest("索引名","类型名","1");
        // 查询,得到响应
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        // 解析响应,应该是json
        String source = response.getSourceAsString();
        //json转对象
        Item item = gson.fromJson(source, Item.class);
        //打印
        System.out.println("item::"+item);
    }

    /**
     * 后执行
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

修改文档

新增时,如果传递的id是已经存在的,则会完成修改操作,如果不存在,则是新增

import com.google.gson.Gson;
import com.leyou.pojo.Item;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Esdemo1 {

    private RestHighLevelClient client;

    private Gson gson = new Gson();
    
    /**
     * 先执行
     */
    @Before
    public void init (){
        // 初始化HighLevel客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://127.0.0.1:9201"),
                        HttpHost.create("http://127.0.0.1:9202"),
                        HttpHost.create("http://127.0.0.1:9203")
                )
        );
    }
    
/*
*	新增时,如果传递的id是已经存在的,则会完成修改操作,如果不存在,则是新增
*/
    @Test
    public void testAddIndex() throws IOException {
        // 准备文档数据:
        Item item = new Item(1L, "小米手机9", " 手机","小米", 3499.00, "http://image.csdn.com/13123.jpg");
        // 转为Json格式:
        String toJson = gson.toJson(item);
        // 创建一个新增索引的请求,并指定是JSON格式
        IndexRequest indexRequest = new IndexRequest("索引名", "类型名", item.getId().toString()).source(toJson, XContentType.JSON);
        // 发起请求
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println("Response:"+indexResponse);
    }

    /**
     * 后执行
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

删除文档

import com.google.gson.Gson;
import com.leyou.pojo.Item;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Esdemo1 {

    private RestHighLevelClient client;

    private Gson gson = new Gson();
    /**
     * 先执行
     */
    @Before
    public void init (){
        // 初始化HighLevel客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://127.0.0.1:9201"),
                        HttpHost.create("http://127.0.0.1:9202"),
                        HttpHost.create("http://127.0.0.1:9203")
                )
        );
    }

    /**
     * 删除文档
     */
    @Test
    public void testDeleteIndex() throws IOException {
        //创建删除请求,传入id
        DeleteRequest deleteRequest = new DeleteRequest("索引名","类型名","1");
        // 发起请求
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        //打印
        System.out.println("response = " + deleteResponse);
    }

    /**
     * 后执行
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值