【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
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/xiaohanlin/p/10264913.html

时间: 2024-11-02 03:23:08

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

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

在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采用6.6.1 一 pom.xml依赖引入 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns

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

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

使用python操作elasticsearch实现数据插入分析

前言: 例行公事,有些人可能不太了解elasticsearch,下面搜了一段,大家瞅一眼. Elasticsearch是一款分布式搜索引擎,支持在大数据环境中进行实时数据分析.它基于Apache Lucene文本搜索引擎,内部功能通过ReST API暴露给外部.除了通过HTTP直接访问Elasticsearch,还可以通过支持Java.JavaScript.Python及更多语言的客户端库来访问.它也支持集成Apache Hadoop环境.Elasticsearch在有些处理海量数据的公司中已经

java操作elasticsearch实现批量添加数据(bulk)

java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOException { //1.指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application"

java操作elasticsearch实现基本的增删改查操作

一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticsearch-6.4.3/config vim elasticsearch.yml 2.查询ip 二.根据文档id查询数据 /** * */ package com.cyb.test; import java.net.InetAddress; import java.net.UnknownHostExc

elk快速入门-在kibana中如何使用devtools操作elasticsearch

在kibana中如何使用devtools操作elasticsearch:前言: 首先需要安装elasticsearch,kibana ,下载地址 https://www.elastic.co/cn/downloads/ 权威指南:https://www.elastic.co/guide/cn/index.html 视频:https://www.elastic.co/cn/webinars/getting-started-elasticsearch?elektra=home&storm=sub1

使用JAVA操作ElasticSearch(Java API 和Spring Data ElasticSearch)

Java API 我的ElasticSearch集群的版本是6.2.4,导入elasticsearch相关的maven依赖也是6.2.4,不同版本的api可能会有差异 一:maven依赖 <!--elasticsearch核心依赖--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version

通过HTTP RESTful API 操作elasticsearch搜索数据

通过HTTP RESTful API 操作elasticsearch搜索数据

使用curl命令操作elasticsearch

使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你可以通过执行curl -XGET localhost:9200/_cat获取所有_cat系列的操作=^.^=/_cat/allocation/_cat/shards/_cat/shards/{index}/_cat/master/_cat/nodes/_cat/indices/_cat/indice