https://blog.csdn.net/niuchenliang524/article/details/82869319

操作es的客房端有多个,在此例出三种(具体区别自行百度),本文讲的是jest操作es的api

1、TransportClient

2、Jest

3、RestClient

springboot的配置文件:

spring.elasticsearch.jest.uris=http://192.168.106.235:9200
pom依赖:

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.0.0</version>
</dependency>

<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>

<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest-common</artifactId>
<version>5.3.3</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>

实体类:

package com.ireport.demo.es;

import lombok.Data;
import java.io.Serializable;

@Data
public class Article implements Serializable {

private Long titleId;
private String title;
private String author;
private String content;
private String publishDate;

@Override
public String toString() {
return "Article{" +
"titleId=" + titleId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", content='" + content + '\'' +
", publishDate='" + publishDate + '\'' +
'}';
}
}
连接mysql数据库:

package com.ireport.demo.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Slf4j
@Data
@Component
public class MysqlJdbcFactory {

private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String pwd = "root";

//连接数据库 mysql
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pwd);
log.info("[数据库连接成功...]");
} catch (Exception e) {
e.printStackTrace();
log.error("[数据库连接失败...]", e.getMessage());
}
return conn;
}

//关闭数据库
public static void closeAll(Connection conn, Statement st, ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
log.info("[数据库关闭...]");
} catch (Exception e) {
e.printStackTrace();
}
}
}
查询所有数据:

package com.ireport.demo.kafka;

import com.ireport.demo.es.Article;
import com.ireport.demo.util.MysqlJdbcFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Data
@Slf4j
@Component
public class ArticleService {

public Connection conn=null;
public Statement st=null;
public ResultSet rs=null;

@Autowired
MysqlJdbcFactory factory;

public List<Article> getAllArticle() {
List<Article> list=new ArrayList<Article>();
try{
String sql="select * from article";
conn=factory.getConnection();
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
Article article=new Article();
article.setTitleId(rs.getLong("id"));
article.setTitle(rs.getString("title"));
article.setAuthor(rs.getString("author"));
article.setContent(rs.getString("content"));
article.setPublishDate(rs.getString("publish_date"));
list.add(article);
}
}catch(Exception e){
e.printStackTrace();
}finally{
factory.closeAll(conn, st, rs);
}
return list;
}
}
数据结构:

客户端类:

package com.ireport.demo.es;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.*;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;

import static com.google.common.base.Preconditions.checkArgument;

@Slf4j
@Component
public class EsJestClient {

@Autowired
private JestClient esClient;

public static final String INDEX = "es-article";
public static final String TYPE = "articles";

/**
* 创建所引
* @throws IOException
*/
public void createIndex() throws IOException {
Settings.Builder builder = Settings.builder();
builder.put("number_of_shards",5);
builder.put("number_of_replicas",1);

//创建索引
CreateIndex createIndex = new CreateIndex.Builder(INDEX)
.settings(builder.build().getAsMap())
.build();
JestResult result = esClient.execute(createIndex);
log.info("【创建索引:{}...】", INDEX);
checkArgument(result.isSucceeded(), result.getErrorMessage());
createMapping();
}

/**
* 删除索引
* @param index
* @throws IOException
*/
public void deleteIndex(String index) throws IOException {
DeleteIndex deleteIndex = new DeleteIndex.Builder(index).build();
JestResult result = esClient.execute(deleteIndex);
log.info("【删除索引:{}...】", index);
checkArgument(result.isSucceeded(), result.getErrorMessage());
}

/**
* put映射
* @throws IOException
*/
public void createMapping() throws IOException {

JSONObject objSource = new JSONObject().fluentPut("properties", new JSONObject()
.fluentPut("title", new JSONObject()
.fluentPut("type", "text")
)
.fluentPut("author", new JSONObject()
.fluentPut("type", "text")
)
.fluentPut("content", new JSONObject()
.fluentPut("type", "text")
)
.fluentPut("publishDate", new JSONObject()
.fluentPut("type", "date")
)
);
PutMapping putMapping = new PutMapping.Builder(INDEX,TYPE, objSource.toJSONString()).build();
JestResult result = esClient.execute(putMapping);

log.info("【创建mapping映射成功...】");
checkArgument(result.isSucceeded(), result.getErrorMessage());

}

/**
* 向es中插入数据
* @param source
* @param id
* @throws Exception
*/
public void putSource(Object source, String id) throws Exception{
JestResult result = esClient.execute(
new Index.Builder(source)
.index(INDEX)
.type(TYPE)
.id(id)
.refresh(true)
.build());
checkArgument(result.isSucceeded(), result.getErrorMessage());
}

/**
* 根据条件查询
* @param query
* @return
* @throws IOException
*/
public SearchResult searchWithQuery(String query) throws IOException {
log.info(">>>>查询条件:\\\r query:{}", query);
Search search = new Search.Builder(query)
.addIndex(INDEX)
.addType(TYPE)
.build();
SearchResult result = esClient.execute(search);
checkArgument(result.isSucceeded(), result.getErrorMessage());
return result;
}

/**
* 根据id查询
* @param id
* @return
*/
public JestResult searchById(long id) throws Exception {
Get build = new Get.Builder(INDEX, String.valueOf(id))
.index(INDEX)
.type(TYPE)
.build();
JestResult result = esClient.execute(build);
return result;
}

/**
* 根据id删除数据
* @param id
* @return
* @throws Exception
*/
public boolean delete(Long id) throws Exception{
Delete build = new Delete.Builder(String.valueOf(id))
.index(INDEX)
.type(TYPE)
.build();
JestResult result = esClient.execute(build);
checkArgument(result.isSucceeded(), result.getErrorMessage());
return result.isSucceeded();

}

/**
* 获取index下的映射
* @return
* @throws Exception
*/
public String getMapping() throws Exception{
GetMapping build = new GetMapping.Builder().addIndex(INDEX).addType(TYPE).build();
JestResult result = esClient.execute(build);
checkArgument(result.isSucceeded(), result.getErrorMessage());
return result.getSourceAsObject(JsonObject.class).toString();
}

}
测试类:

package com.ireport.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ireport.demo.es.Article;
import com.ireport.demo.es.EsJestClient;
import com.ireport.demo.kafka.ArticleService;
import io.searchbox.client.JestResult;
import io.searchbox.core.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.swing.text.Highlighter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsTest {

@Autowired
EsJestClient esClient;

@Autowired
ArticleService articleService;

/**
* 初始化索引并创建映射
*/
@Test
public void init(){
try {
esClient.deleteIndex("es-article");
esClient.createIndex();
}catch (IOException e){
e.printStackTrace();
log.error("【es-article索引建立失败...】", e.getMessage());
}
}

/**
* 获取映射信息
*/
@Test
public void getMappingTest(){
try {
String mapping = esClient.getMapping();
log.error("【获取映射信息:{}】", mapping);
}catch (Exception e){
e.printStackTrace();
log.error("【获取映射信息失败...】", e.getMessage());
}
}

/**
* 添加数据
*/
@Test
public void putDataTest(){

try{
List<Article> list = articleService.getAllArticle();
for (Article article: list) {
esClient.putSource(article, article.getTitleId().toString());
log.info("【《{}》保存成功...】", article.getTitle());
}
}catch(Exception e){
e.printStackTrace();
log.error("【数据保存失败...】", e.getMessage());
}

}

/**
* 查询type下的所有数据
*/
@Test
public void searchType(){

JSONObject resultJson = new JSONObject();
JSONArray retJsonArray = new JSONArray();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.sort("publishDate", SortOrder.ASC); //根据发版日期排序
SearchResult result;
try {
result = esClient.searchWithQuery(searchSourceBuilder.toString());
for(SearchResult.Hit<JSONObject, Void> item : result.getHits(JSONObject.class)) {
retJsonArray.add(item.source);
}
} catch (IOException e) {
e.printStackTrace();
log.error("【查询数据失败...】", e.getMessage());
}
resultJson.fluentPut("data", retJsonArray);
log.info("【result:{}】", resultJson.toString());

}

/**
* 根据条件查询并设置高亮显示内容
*/
@Test
public void searchByTj() {
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery(MessageFormat.format("title:{0} OR content:{0}", "作品")));
searchSourceBuilder.sort("publishDate", SortOrder.DESC);
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("title");
highlightBuilder.field("content");
highlightBuilder.preTags("<em>").postTags("</em>");//高亮标签
highlightBuilder.fragmentSize(100);
searchSourceBuilder.highlighter(highlightBuilder);
SearchResult result = esClient.searchWithQuery(searchSourceBuilder.toString());
for (SearchResult.Hit<JSONObject, Void> item : result.getHits(JSONObject.class)){
if (item.highlight.containsKey("title")) {
item.source.fluentPut("highlight", item.highlight.get("title").get(0));
}
if (item.highlight.containsKey("content")) {
item.source.fluentPut("highlight_content", item.highlight.get("content").get(0));
}
item.source.remove("content");
log.info("sss:{}", JSONArray.toJSON(item).toString());
}
} catch (Exception e) {
e.printStackTrace();
log.error("【查询数据失败...】", e.getMessage());
}
}

/**
* 根据id查询
*/
@Test
public void searchByIdTest() {
try {
JestResult jestResult = esClient.searchById(1L);
Article article = jestResult.getSourceAsObject(Article.class);
log.info("【《{}》查询成功,{}...】", article.getTitle(), article);
} catch (Exception e) {
e.printStackTrace();
log.error("【查询数据失败...】", e.getMessage());
}
}

/**
* 删除数据
*/
@Test
public void deleteTest() {
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchResult result = esClient.searchWithQuery(searchSourceBuilder.toString());
for (SearchResult.Hit<JSONObject, Void> item : result.getHits(JSONObject.class)) {
esClient.delete(item.source.getLong("titleId"));
log.info("【《{}》删除成功...】", item.source.getString("title"));
}
} catch (Exception e) {
e.printStackTrace();
log.error("【删除数据失败...】", e.getMessage());
}
}

}
---------------------
作者:情蛊成殇
来源:CSDN
原文:https://blog.csdn.net/niuchenliang524/article/details/82869319
版权声明:本文为博主原创文章,转载请附上博文链接!

【Es】jest操作elasticsearch的更多相关文章

  1. springboot整合es客户端操作elasticsearch(五)

    springboot整合es客户端操作elasticsearch的总结: 客户端可以进行可以对所有文档进行查询,就是不加任何条件: SearchRequest searchRequest = new ...

  2. jest操作 Elasticsearch

    package com.lgmall.search; import com.lgmall.search.esEntity.Article;import com.lgmall.search.esEnti ...

  3. springboot整合es客户端操作elasticsearch(四)

    对文档查询,在实际开发中,对文档的查询也是偏多的,记得之前在mou快递公司,做了一套事实的揽件数据操作,就是通过这个来存储数据的,由于一天的数据最少拥有3500万数据 所以是比较多的,而且还要求查询速 ...

  4. springboot整合es客户端操作elasticsearch(二)

    在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...

  5. springboot整合es客户端操作elasticsearch(三)

    继续上个随笔: 那么我们只需要修改controller中文件就可以完成相关操作 本次主要是对文档得操作: 更新文档: package com.cxy.elasticsearch.controller; ...

  6. Es学习第十一课,使用java操作elasticsearch

    前面十节课我们已经把ES的基本概念和使用讲的差不多了,现在我们就用基于java来实际开发一个操作ES的小项目,带大家来一起练练手. 1.我们用IDEA创建一个maven项目 项目结构如上图所示,然后我 ...

  7. 简单操作elasticsearch(es版本7.6)

    简单操作elasticsearch(es版本7.6) es 官方文档 https://www.elastic.co/guide/index.html 简单操作elasticsearch主要是指管理索引 ...

  8. jest for elasticsearch

    *elasticsearch(后面简称es) 背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法:有些数据处理起来还是需要定制开发处理,不是很方便.正好需要对本项目重新进行改造,于 ...

  9. java操作elasticsearch实现组合桶聚合

    1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...

随机推荐

  1. 分布式爬虫搭建系列 之四---scrapy分布式框架

    带录入SAFCDS 

  2. 详解PHP执行定时任务的实现思路

    PHP本身是没有定时功能的,PHP也不能多线程.PHP的定时任务功能必须通过和其他工具结合才能实现,例如WordPress内置了wp-cron的功能,很厉害. 一.Linux服务器上使用CronTab ...

  3. 【原创】基于UDP广播的局域网Web Window Service日志跟踪小工具

           一直感觉Web开发或者windows服务的日志跟踪调试不是很方便          特别是在生产环境服务器上面          目前一般的解决方案是通过各种日志工具把错误信息和调试信息 ...

  4. vbs获取html内容

    Dim content,name,password,arr,pos msg1="请输入ip和端口号地址"&chr(13)&chr(10)&"如ht ...

  5. Android输入法部分遮挡UI的问题(与EditText框相切)

    首先,我们来看看遇到问题的图片 遇到的问题是,当点击输入框之后,输入法会切到红线的位置,理想状态应该是在绿线位置 那么,是什么原因造成的呢? 问题其实很简单,是因为drawableleft图片比该输入 ...

  6. Log4Net 在ASP.NET WebForm 和 MVC的全局配置

    使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能改变 ...

  7. 第01章 开发准备(对最新版的RN进行了升级)1-2+项目技术分解

  8. JAVA环境安装配置

    dk1.6 64位是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序. jdk1.6 64位安装教程 jdk1.6 64位JDK的安装路径:D:\Program Fi ...

  9. php手机号正则

    preg_match("/^1[34578]{1}\d{9}$/", $phone)

  10. Oracle——创建和管理表

    一.常见的数据库对象 对象 描述 表 基本的数据存储集合,由行和列组成 视图 从表中抽出的逻辑上相关的数据集合 序列 提供有规律的数值 索引 提高查询的效率 同以词 给对象起别名 二.Oracle 数 ...