Spring Boot整合ElasticSearch实现索引和文档的CRUD(与Kibana语法结合)

1.创建SpringBoot项目,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.liu</groupId>
    <artifactId>esapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>esapi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.13.3</elasticsearch.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.创建ES客户端配置类,注意使用@Bean时需要指定bean的名字,因为ES默认会自动初始化客户端对象,否则在使用@Autowired依赖注入时,会有多个同一类型的bean,导入注入失败。

@Configuration
public class ClientConfig {
    @Bean("client")
    public RestHighLevelClient getRestHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                //es服务的地址和端口,如果有多个es(es集群)可以链式编程,添加es地址
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
        );
        return client;
    }
}

3.创建实体类,在插入文档时需要

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private Integer age;
    private String address;
}

4.索引和文档的CRUD单元测试

@SpringBootTest
@Slf4j
class EsapiApplicationTests {



    @Autowired
    private RestHighLevelClient client;


    /**
     * 创建索引(索引库)
     *
     *
     * kibana返回结果
     *
     *
     * PUT /test
     *
     * {
     *   "acknowledged" : true,
     *   "shards_acknowledged" : true,
     *   "index" : "test"
     * }
     *
     */
    @Test
    public void createIndex() throws Exception{
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("index_one");
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        log.info(createIndexResponse.index());
        log.info(Boolean.toString(createIndexResponse.isAcknowledged()));
        log.info(Boolean.toString(createIndexResponse.isShardsAcknowledged()));
    }


    /**
     *
     * GET /test
     *
     *
     * {
     *   "test" : {
     *     "aliases" : { },
     *     "mappings" : { },
     *     "settings" : {
     *       "index" : {
     *         "routing" : {
     *           "allocation" : {
     *             "include" : {
     *               "_tier_preference" : "data_content"
     *             }
     *           }
     *         },
     *         "number_of_shards" : "1",
     *         "provided_name" : "test",
     *         "creation_date" : "1626834365541",
     *         "number_of_replicas" : "1",
     *         "uuid" : "CDJ24TLGRIe2aCVxblKuTw",
     *         "version" : {
     *           "created" : "7130399"
     *         }
     *       }
     *     }
     *   }
     * }
     *
     *
     * 查找索引库,判断这个库是否存在
     *
     */
    @Test
    public void getIndex() throws Exception{

        GetIndexRequest getIndexRequest = new GetIndexRequest("index_one");
        boolean flag = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
        if(flag){
            log.info("index_one索引库存在");
        }else{
            log.info("index_one索引库不存在");
        }

    }


    /**
     * 索引库删除
     *
     *
     * DELETE /test
     *
     * {
     *   "acknowledged" : true
     * }
     */
    @Test
    public void deleteIndex() throws Exception{
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("index_one");
        AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        //判断  true表示删除成功
        if(acknowledgedResponse.isAcknowledged()){
            log.info("删除成功");
        }else{
            log.info("删除失败");
        }
    }


    /**
     * 创建文档
     * PUT /index_one/_doc/1
     * {
     *   "name":"刘红飞",
     *   "age":20,
     *   "address":"山西省临汾市洪洞县"
     * }
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 1,
     *   "result" : "created",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 0,
     *   "_primary_term" : 1
     * }
     *
     *
     */
    @Test
    public void createDocument() throws Exception{
       User user = new User("张文文",20,"山西省太原市");
       //需要操作哪个索引库
       IndexRequest indexRequest = new IndexRequest("index_one");
       indexRequest.id("2");
       //将对象转换为json字符串存储
       indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
       IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);
       log.info("------对应kibana中的API请求返回结果");
       log.info(indexResponse.getIndex());
       log.info(indexResponse.getType());
       log.info(indexResponse.getId());
       log.info(String.valueOf(indexResponse.getVersion()));
       log.info(indexResponse.getResult().getLowercase());
       ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
       log.info(String.valueOf(shardInfo.getSuccessful()));
       indexResponse.status();
    }


    /**
     * 获取文档
     *
     * GET /index_one/_doc/1
     *
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 1,
     *   "_seq_no" : 0,
     *   "_primary_term" : 1,
     *   "found" : true,
     *   "_source" : {
     *     "name" : "刘红飞",
     *     "age" : 20,
     *     "address" : "山西省临汾市洪洞县"
     *   }
     * }
     */
    @Test
    public void getDocument() throws Exception{

        //查询所有时,进行遍历,的每一个都是map,将map放到list里,最后再通过list展示所有

        GetRequest getRequest = new GetRequest("index_one","1");
        if(client.exists(getRequest,RequestOptions.DEFAULT)){
            GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
            //返回的是单个文档的数据,key为字段名称,value为字段的值
            Map<String,Object> map = getResponse.getSource();
            log.info(String.valueOf(map.get("name")));
            log.info(String.valueOf(map.get("age")));
            log.info(String.valueOf(map.get("address")));
        }else{
             log.info("当前文档不存在");
        }
    }


    /**
     *
     *更新文档
     *
     * POST /index_one/_doc/1/_update
     * {
     *   "doc":{
     *     "age":30
     *   }
     * }
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 2,
     *   "result" : "updated",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 2,
     *   "_primary_term" : 1
     * }
     *
     *
     */
    @Test
    public void updateDocument() throws Exception{
        UpdateRequest updateRequest = new UpdateRequest("index_one","1");
        User user = new User();
        user.setAge(10000);
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
        log.info("更新结果:" + updateResponse.getResult().getLowercase());
    }


    /**
     * 删除文档
     *
     * DELETE /index_one/_doc/2
     *
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "2",
     *   "_version" : 4,
     *   "result" : "deleted",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 6,
     *   "_primary_term" : 1
     * }
     *
     */
    @Test
    public void deleteDocument() throws Exception{
        DeleteRequest deleteRequest = new DeleteRequest("index_one","2");
        DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);
        log.info("删除结果:" + deleteResponse.status().name());
        log.info("删除结果:" + deleteResponse.getResult().getLowercase());
    }
}

最后顺便给大家解释下索引和文档的含义:
1.索引:又叫索引库,相当于MySQL中的表
2.文档:相当于MySQL表中的数据
一个索引库中有多个文档,也就是相当于表中有多条数据,
文档中的每个字段可以称之为域(lucene中的叫法),域类型和域值

ElasticSearch的数据可视化客户端可以选择 ElasticSearch Head如下,非常清晰好用
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值