springboot集成elk 四:springboot + Elasticsearch+Jest

Posted 陈小兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot集成elk 四:springboot + Elasticsearch+Jest相关的知识,希望对你有一定的参考价值。

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.jna</groupId>
    <artifactId>jna</artifactId>
    <version>3.0.9</version>
</dependency>

 

配置文件: 

#elasticsearch.jest
spring.elasticsearch.jest.uris=http://192.168.1.62:9200
spring.elasticsearch.jest.read-timeout=6000

 

实体类: 

public class Goods2Info implements Serializable {
    private static final long serialVersionUID = -7682211945335253642L;
    private Long id;
    private String name;
    private String description;
    public static final String INDEX_NAME = "test2";

    public static final String TYPE = "goods2";

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Goods2Info(Long id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Goods2Info() {
    }
}

 

  

jest客户端使用

package com.test.elk.controller;

import com.test.elk.model.Goods2Info;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RequestMapping("/goods2")
@RestController
public class Goods2Controller {
    private static final Logger LOGGER = LoggerFactory.getLogger(Goods2Controller.class);
    @Autowired
    private JestClient jestClient;

    @GetMapping("/save")
    public void save() {
        Goods2Info goods2Info=new Goods2Info();
        goods2Info.setId(1001L);
        goods2Info.setName("中国人");
        goods2Info.setDescription("中国人中国人");
        Index index = new Index.Builder(goods2Info).index(Goods2Info.INDEX_NAME).type(Goods2Info.TYPE).build();
        try {
            jestClient.execute(index);
            LOGGER.info("ES 插入完成");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }


    @GetMapping("/save2")
    public void save2() {
        List<Goods2Info> entityList =new ArrayList<>();
        Goods2Info goods2Info1=new Goods2Info();
        goods2Info1.setId(1002L);
        goods2Info1.setName("中国人");
        goods2Info1.setDescription("中国人中国人");
        Goods2Info goods2Info2=new Goods2Info();
        goods2Info2.setId(1003L);
        goods2Info2.setName("美国人");
        goods2Info2.setDescription("美国人美国人");
        entityList.add(goods2Info1);
        entityList.add(goods2Info2);
        Bulk.Builder bulk = new Bulk.Builder();
        for(Goods2Info entity : entityList) {
            Index index = new Index.Builder(entity).index(Goods2Info.INDEX_NAME).type(Goods2Info.TYPE).build();
            bulk.addAction(index);
        }
        try {
            jestClient.execute(bulk.build());
            LOGGER.info("ES 插入完成");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
    }

    @GetMapping("/query")
    public List<Goods2Info> searchEntity(String searchContent){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //searchSourceBuilder.query(QueryBuilders.queryStringQuery(searchContent));
        //searchSourceBuilder.field("name");
        searchSourceBuilder.query(QueryBuilders.matchQuery("name",searchContent));
        searchSourceBuilder.from(0).size(2);
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(Goods2Info.INDEX_NAME).addType(Goods2Info.TYPE).build();
        try {
            JestResult result = jestClient.execute(search);
            return result.getSourceAsObjectList(Goods2Info.class);
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

}

 


 springboot-elasticsearch-jest.zip

以上是关于springboot集成elk 四:springboot + Elasticsearch+Jest的主要内容,如果未能解决你的问题,请参考以下文章

springboot集成elk 二:springboot + elk 采集日志

springboot集成elk 一: springboot + Elasticsearch

springboot集成elk 三:springboot + Elasticsearch Rest-Client

ELK SpringData框架 Springboot集成elasticSearch

springboot 集成 elk 日志收集功能

springboot整合logback集成elk实现日志的汇总分析统计和检索功能