Bootstrap

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

;