elk部署springboot

1.设置es版本:
<properties>
   <java.version>1.8</java.version>
   <elasticsearch.version>6.8.13</elasticsearch.version>
</properties>
2.导入ES依赖,JSON依赖:
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.54</version>
</dependency>

<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
   <groupId>org.elasticsearch</groupId>
   <artifactId>elasticsearch</artifactId>
</dependency>
3.创建一个实体类
package cn.com.galaxy.elkTest;

import ch.qos.logback.core.joran.spi.NoAutoStart;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class Employee {

    private String name;
    private Integer age;
    private String text;
    private String num;

    public Employee() {
    }

    public Employee(String name, Integer age, String text, String num) {
        this.name = name;
        this.age = age;
        this.text = text;
        this.num = num;
    }
}
4.测试
package cn.com.galaxy.elkTest;


import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.ArrayList;

@Slf4j
@Configuration
public class EsConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
        return client;
    }

    @Test
    void testCreateIndex() throws IOException {
        HttpHost host = HttpHost.create("http://124.221.27.165:9200");
        RestClientBuilder builder = RestClient.builder(host);
        RestHighLevelClient client = new RestHighLevelClient(builder);

        CreateIndexRequest request = new CreateIndexRequest("user");
        client.indices().create(request, RequestOptions.DEFAULT);

        client.close();
    }

    /**
     * 批量添加
     *
     * @throws IOException
     */
    @Test
    void add() throws IOException {
        RestHighLevelClient client = restHighLevelClient();
        BulkRequest bulkRequest = new BulkRequest();

        ArrayList<Employee> users = new ArrayList<>();
        users.add(new Employee("李文", 23, "研发工程师", "2352"));
        users.add(new Employee("罗文", 17, "测试工程师", "8732"));
        users.add(new Employee("徐洁", 22, "算法工程师", "8791"));
        users.add(new Employee("罗辑", 31, "高级研发工程师", "8765"));
        users.add(new Employee("叶文洁", 70, "资深研发工程师", "8551"));

        for (int i = 0; i < users.size(); i++) {
            bulkRequest.add(new IndexRequest("test").id("" + (i + 1)).type("pinyin")
                    .source(JSON.toJSONString(users.get(i)), XContentType.JSON));
        }
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(!bulkResponse.hasFailures());
    }

    /**
     * 添加单条记录
     * @throws IOException
     */
    @Test
    void add2() throws IOException {
        RestHighLevelClient client = restHighLevelClient();
        // 1.要在指定索引下创建文档,所以要先创建索引,再创建文档
        IndexRequest request=new IndexRequest();
        // index()方法设置索引名;id()方法设置唯一id标识
        request.index("test").id("5").type("pinyin");
        // 2.创建实体类对象,填充数据
        Employee employee=new Employee();
        employee.setName("张三丰");
        employee.setAge(30);
        employee.setNum("23");
        employee.setText("添加");
        // 3.利用jackson将实体类对象转换成JSON格式字符串
        request.source(JSON.toJSONString(employee), XContentType.JSON);
        // 5.发送请求,获取响应结果
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println("_index: "+response.getIndex());
        System.out.println("_id: "+response.getId());
        System.out.println("_result: "+response.getResult());
    }


    /**
     * 修改
     */
    @Test
    void update() throws IOException {
        RestHighLevelClient client = restHighLevelClient();
        UpdateRequest request = new UpdateRequest("test", "pinyin", "1");
        Employee user = new Employee("张三", 18, "测试", "1234");
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse.status() == RestStatus.OK);
    }

    /**
     * 删除
     */

    @Test
    void delete() throws IOException {
        RestHighLevelClient client = restHighLevelClient();
        HttpHost host = HttpHost.create("http://124.221.27.165:9200");
        client = new RestHighLevelClient(RestClient.builder(host));

        DeleteRequest request = new DeleteRequest("test", "pinyin", "5");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status() == RestStatus.OK);
    }


    /**
     * 全查(除id)
     *
     * @throws Exception
     */
    @Test
    public void select() {
        RestHighLevelClient client = restHighLevelClient();
        try {
            // 构建查询条件
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            // 创建查询请求对象,将查询对象配置到其中
            SearchRequest searchRequest = new SearchRequest("test");
            searchRequest.source(searchSourceBuilder);
            // 执行查询,然后处理响应结果
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            // 根据状态和数据条数验证是否返回了数据
            if (RestStatus.OK.equals(searchResponse.status())) {
                SearchHits hits = searchResponse.getHits();
                for (SearchHit hit : hits) {
                    // 将 JSON 转换成对象
                    Employee userInfo = JSON.parseObject(hit.getSourceAsString(), Employee.class);
                    // 输出查询信息
                    System.out.println(userInfo.toString());
                }
            }
        } catch (IOException e) {
            log.error("", e);
        }
    }
5.ElasticSearch进阶:各种es查询在java中实现

https://www.cnblogs.com/wk-missQ1/p/16664511.html

相关推荐
VinciYan2 小时前
基于Jenkins+Docker的自动化部署实践——整合Git与Python脚本实现远程部署
python·ubuntu·docker·自动化·jenkins·.net·运维开发
黑胡子大叔的小屋4 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
计算机毕设孵化场5 小时前
计算机毕设-基于springboot的校园社交平台的设计与实现(附源码+lw+ppt+开题报告)
spring boot·课程设计·计算机毕设论文·计算机毕设ppt·计算机毕业设计选题推荐·计算机选题推荐·校园社交平台
苹果醋36 小时前
Golang的文件加密工具
运维·vue.js·spring boot·nginx·课程设计
jwensh6 小时前
【Jenkins】Declarative和Scripted两种脚本模式有什么具体的区别
运维·前端·jenkins
小马爱打代码8 小时前
Spring Boot 中 Map 的最佳实践
java·spring boot·spring
全栈开发帅帅8 小时前
基于springboot+vue实现的博物馆游客预约系统 (源码+L文+ppt)4-127
java·spring boot·后端
m0_748255659 小时前
Springboot基于Web的景区疫情预警系统设计与实现5170q(程序+源码+数据库+调试部署+开发环境)
前端·数据库·spring boot
平行线也会相交9 小时前
云图库平台(三)——后端用户模块开发
数据库·spring boot·mysql·云图库平台
lxyzcm10 小时前
深入理解C++23的Deducing this特性(上):基础概念与语法详解
开发语言·c++·spring boot·设计模式·c++23