1.需要添加的依赖
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.3.3</version>
</dependency>
2.es集群的配置
es集群的配置
elasticsearch
**集群名称:随便起名
spring.data.elasticsearch.cluster-name=**
集群节点
spring.data.elasticsearch.clusternodes=*,**,*
开启 Elasticsearch 仓库。(默认值:true。)
pring.data.elasticsearch.repositories.enabled=true
elasticsearch jest客户端
spring.elasticsearch.jest.uris=,
spring.elasticsearch.jest.read-timeout=**
**
3.简单的增删改查
package witparking.service.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.searchbox.action.SingleResultAbstractDocumentTargetedAction;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/**
* @author Tom
*/
@Component
public class JestPackClient {
private static final Logger LOGGER = LoggerFactory.getLogger(JestPackClient.class);
@Autowired
private JestClient jestClient;
public synchronized JestClient getClient() {
return jestClient;
}
/**
* 增加一条记录,
*
* @param index 索引记录
* @param type 类型集合
* @param jsonObject
* @return
*/
public JestResult addDataOne(String index, String type, JSONObject jsonObject) {
return addDataMany(index, type, Collections.singletonList(jsonObject));
}
/**
* 增加多条记录
*
* @param index 索引集合
* @param type 类型集合
* @param jsonObjectList 多个对象的json串,注意每个对象里面必须有id
* @return
*/
private JestResult addDataMany(String index, String type, List<JSONObject> jsonObjectList) {
ArrayList<Object> indexList = Lists.newArrayList();
for (JSONObject jsonObject : jsonObjectList) {
indexList.add(new Index.Builder(jsonObject).id(jsonObject.getString("id")).build());
}
Bulk bulk = new Bulk.Builder().defaultIndex(index).defaultType(type).addAction(indexList).build();
JestResult result = null;
try {
result = jestClient.execute(bulk);
LOGGER.info(index + "---" + type + "---处理结果:" + result.getJsonString());
} catch (IOException e) {
LOGGER.info(index + "---" + type + "---处理出错:" + JSON.toJSONString(jsonObjectList) + "," + e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 根据id获得数据
*
* @param index 相当于数据库
* @param type
* @param id 主键id
* @return
*/
public JestResult getById(String index, String type, String id) {
Get get = new Get.Builder(index, id).type(type).build();
return executeCommand(get);
}
/**
* 根据id更新数据
*
* @param index 索引集合
* @param type 类型集合
* @param id 要更新的实体的主键
* @param jsonObject 要更新的实体的json串
* @return
*/
public JestResult updateById(String index, String type, String id, JSONObject jsonObject) {
HashMap<String, Object> payload = Maps.newHashMap();
payload.put("doc", jsonObject);
Update update = new Update.Builder(payload).index(index).type(type).id(id).build();
return executeCommand(update);
}
/**
* 根据id删除数据
*
* @param index
* @param type
* @param id
* @return
*/
public JestResult delete(String index, String type, String id) {
Delete delete = new Delete.Builder(id).index(index).type(type).build();
return executeCommand(delete);
}
private JestResult executeCommand(SingleResultAbstractDocumentTargetedAction command) {
JestResult execute = null;
try {
execute = jestClient.execute(command);
} catch (IOException e) {
LOGGER.error("Exception occurred executing CRUD action", e);
}
return execute;
}
}
————————————————
版权声明:本文为CSDN博主「阳光叶落」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35603348/article/details/81171948