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. Rest Framework 认证源码流程

    一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...

  2. Storm集群详细部署

    1.安装zookeeper 3.1下载zookeeper安装包, 建议下载3.4.5及以上的版本 http://www.apache.org/dyn/closer.cgi/zookeeper/ 3.2 ...

  3. Shell脚本 判断

    #Linux系统Shell脚本判断变量文件目录:权限.是否存在.空值.相等 1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真str1 != str2 当串str1和str2不等时 ...

  4. opencv掩膜操作

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  5. 12-scanf("%*s")与printf("%*s")

    在scanf里用*修饰符,是起到过滤读入的作用.比如一个有三列数值的数据,我只想得到第2列数值,可以在循环里用scanf(“%*d%d%*d”,a[i])来读入第i行的第2个数值到a[i].     ...

  6. 重置CentOS6.5的登录口令

    早先在虚拟机Vmware里安装了一台CentOS6.5,现在想登录,发现无论怎么输入登录口令都不正确,以至于无法登录. 查阅网上资料,可用下面步骤里的方法重置登录口令,在此记录. 1.启动机器,出现下 ...

  7. Python ---- super()使用

    Python ---- super() 我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: 1 2 3 4 5 6 7 8 9 10 11 12 13 class A:     ...

  8. codefirst 关系处理

    1.http://www.cnblogs.com/libingql/archive/2013/01/31/2888201.html 2.多对多 protected override void OnMo ...

  9. orcad找不到dll

    如果运行Capture.exe找不到cdn_sfl401as.dll,如果运行allegro.exe找不到cnlib.dll,(上面俩个库文件都在C:/Cadence/SPB_16.3/tools/b ...

  10. WordPaster2-正式包布署说明

    1.1. 多平台布署说明 提供信息如下: 1.多平台控件包(根据购买版本提供) 2.控件信息 3.配置方法   WordPaster(x86)Clsid信息 ClsidParser F4B7C0FD- ...