依赖

<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的更多相关文章

  1. springboot集成elk 一: springboot + Elasticsearch

    1.ELK介绍 1> Elasticsearch是实时全文搜索和分析引擎, 提供搜集.分析.存储数据三大功能: 是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统. ...

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

    注:  该集成方式,对Elasticsearch无版本限制,但是需要自行封装请求,解析结果等. <dependency> <groupId>org.elasticsearch. ...

  3. springboot 集成 elk 日志收集功能

    Lilishop 技术栈 官方公众号 & 开源不易,如有帮助请点Star 介绍 官网:https://pickmall.cn Lilishop 是一款Java开发,基于SpringBoot研发 ...

  4. 【Elastic-2】SpringBoot整合ELK、SpringBoot写ES

    ELK相关TODO 快速开始文档(https://www.cnblogs.com/lbhym/p/15934416.html) SpringBoot整合ELK ELK接入Kafka(待Kafka快速开 ...

  5. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  6. springboot集成elk实现分布式日志管理

    1.安装elk https://www.cnblogs.com/xuaa/p/10769759.html 2.idea创建springboot项目 File -> New -> Proje ...

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

    到logstash-2.0.0\bin下新建文件 logstash.conf input { tcp { mode => "server" host => " ...

  8. springboot集成Guava缓存

    很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...

  9. SpringBoot图文教程14—SpringBoot集成EasyExcel「上」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

随机推荐

  1. Windows10下安装Ubuntu18.04LTS详细教程

    这篇文章分享自己在Windows10系统下安装VMware虚拟机,然后在VMware中安装Ubuntu 18.04 LTS的详细过程.之所以选择在虚拟机中安装Ubuntu,主要是可以不影响自己电脑的正 ...

  2. [HNOI2011]括号修复 / [JSOI2011]括号序列

    传送门 Solution 一道题花费了两天的时间-- 在大佬@PinkRabbit的帮助下,终于AC了,感动-- 首先,我们考虑一个括号序列被修改成合法序列需要的次数: 我们需要修改的其实是形如... ...

  3. 由Java正则表达式的灾难性回溯引发的高CPU异常:java.util.regex.Pattern$Loop.match

    问题与分析 某天领导report了一个问题:线上的CPU自从上一个版本迭代后就一直处于居高不下的状况,领导看着这段时间的曲线图判断是有两条线程在不停的死循环. 接到任务后去查看了AWS的CloudWa ...

  4. WPF中打开网页的两种方法

    1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...

  5. Java基础系列 - JAVA集合ArrayList,Vector,HashMap,HashTable等使用

    package com.test4; import java.util.*; /** * JAVA集合ArrayList,Vector,HashMap,HashTable等使用 */ public c ...

  6. xposed代码示例

    package com.example.xposedhook; import android.util.Log; import de.robv.android.xposed.IXposedHookLo ...

  7. SpringBoot-文件在线预览解决方案-基于OpenOffice及jacob

    项目中有一个需求:实现文件(主要是Office文件)的在线预览,根据前端需求,Office文件需要转换成pdf或者html方可在浏览器中打开预览,那么后端需要将文件转为pdf/格式返回地址给前端.目前 ...

  8. IO操作之BIO、NIO、AIO

    一.BIO Blocking IO: 同步阻塞的编程方式. BIO编程方式通常是在JDK1.4版本之前常用的编程方式.编程实现过程为:首先在服务端启动一个ServerSocket来监听网络请求,客户端 ...

  9. Tosca Connection Validation error:40 - Could not open a connection to SQL Server (不知道怎么解决)

    谁知道下面这个错怎么解决,请给我留言,谢谢. 数据库能正常链接,服务也是 normal running

  10. OpenGL ES: (1) OpenGL ES的由来 (转)

    1. 电脑是做什么用的? 电脑又被称为计算机,那么最重要的工作就是计算.看过三体的同学都知道, 电脑中有无数纳米级别的计算单元,通过 0 和 1 的转换,完成加减乘除的操作. 2. 是什么使电脑工作? ...