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. 不用jquery实现tab页切换,刷新,后退,前进状态自动维护 很好用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 用CSS使图片上下左右都绝对居中于DIV

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [转]C#开发微信公众平台-就这么简单

    本文转自:http://www.it165.net/pro/html/201403/11102.html 写在前面 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单 ...

  4. python利用utf-8编码判断中文字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  5. 【总结整理】关于房产app的比较

    从切换城市的分类方式就能看出来,因覆盖城市很多,搜房网(房天下)跟安居客都用上了拼音选房,而链家因城市很少,只需简单罗列即可. 搜房网(房天下)覆盖城市多达651个,覆盖范围最广,安居客为500个,两 ...

  6. Linux3一些文件操作命令more,less,pr,head,tail,wc

    查看文件内容命令: more和less 用cat命令可以查看文件.有时候文件太大,可以用管道符号|配合more或者less一同使用. cat  <文本文件名称>|more cat  < ...

  7. MySQL5.7插入中文乱码

    参考: https://blog.csdn.net/kelay06/article/details/60870138 https://blog.csdn.net/itmr_liu/article/de ...

  8. CMD指令大全

    命令提示符(CMD)是在OS / 2 , Windows CE与Windows NT平台为基础的操作系统(包括Windows 2000和XP中, Vista中,和Server 2003 )下的“MS- ...

  9. Apache logresolve命令

    一.简介 logresolve是一个解析Apache访问日志中IP地址的后处理程序. 二.语法 logresolve [ -s filename ] [ -c ] < access_log &g ...

  10. PAT 1017 Queueing at Bank (25) (坑题)

    Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...