【Es】jest操作elasticsearch
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的更多相关文章
- springboot整合es客户端操作elasticsearch(五)
springboot整合es客户端操作elasticsearch的总结: 客户端可以进行可以对所有文档进行查询,就是不加任何条件: SearchRequest searchRequest = new ...
- jest操作 Elasticsearch
package com.lgmall.search; import com.lgmall.search.esEntity.Article;import com.lgmall.search.esEnti ...
- springboot整合es客户端操作elasticsearch(四)
对文档查询,在实际开发中,对文档的查询也是偏多的,记得之前在mou快递公司,做了一套事实的揽件数据操作,就是通过这个来存储数据的,由于一天的数据最少拥有3500万数据 所以是比较多的,而且还要求查询速 ...
- springboot整合es客户端操作elasticsearch(二)
在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...
- springboot整合es客户端操作elasticsearch(三)
继续上个随笔: 那么我们只需要修改controller中文件就可以完成相关操作 本次主要是对文档得操作: 更新文档: package com.cxy.elasticsearch.controller; ...
- Es学习第十一课,使用java操作elasticsearch
前面十节课我们已经把ES的基本概念和使用讲的差不多了,现在我们就用基于java来实际开发一个操作ES的小项目,带大家来一起练练手. 1.我们用IDEA创建一个maven项目 项目结构如上图所示,然后我 ...
- 简单操作elasticsearch(es版本7.6)
简单操作elasticsearch(es版本7.6) es 官方文档 https://www.elastic.co/guide/index.html 简单操作elasticsearch主要是指管理索引 ...
- jest for elasticsearch
*elasticsearch(后面简称es) 背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法:有些数据处理起来还是需要定制开发处理,不是很方便.正好需要对本项目重新进行改造,于 ...
- java操作elasticsearch实现组合桶聚合
1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...
随机推荐
- php浏览器端调试输出方法
1.利用js打印到浏览器控制台 <?php function console_log($data) { if (is_array($data) || is_object($data)) ...
- infos
C++文件流 iostream 提供了cin cout 分别用于从标准输入读取流和向标准输出写入流cout 标准输出 屏幕 输出写入fstream文件流 cin 从标准输入读取cout 向标准输入写入 ...
- 系统环境变量PATH被删除后从注册表恢复
转自:https://wenku.baidu.com/view/75d0b6ec19e8b8f67c1cb958.html 首先需要了解path这个环境变量有什么用: 当你打开命令提示窗口时,即win ...
- VS 配置外部DLL的引用路径【可执行文件的环境路径】
右键项目,属性->配置属性->调试->环境,在这里写入可执行文件运行时的环境路径,格式为:PATH=ABC,如PATH=$(SolutionDir)/env 这样,我们就可以把运行时 ...
- alsa-lib及alsa-utils成功移植(原…
准备工作 alsa-lib版本:alsa-lib-1.0.23.tar.bz2 alsa-util版本:alsa-utils-1.0.23.tar.bz2 其他版本的alsa-lib和alsa-uti ...
- Zookeeper 基础、工作流、ZAP协议
ZooKeeper 基础 在深入了解ZooKeeper的运作之前,让我们来看看ZooKeeper的基本概念.[1] 我们将在本章中讨论以下主题:1.Architecture(架构)2.Hierarch ...
- 字节流之文件输出流FileOutputStream
文件拷贝:
- Adam:一种随机优化方法
我们介绍Adam,这是一种基于一阶梯度来优化随机目标函数的算法. 简介: Adam 这个名字来源于 adaptive moment estimation,自适应矩估计.概率论中矩的含义是:如果一个随机 ...
- C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
C# 把一个文件夹下所有文件复制到另一个文件夹下 public static void CopyDirectory(string srcPath, string destPath) { try { ...
- jqentitydetail
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...