springBoot2.x集成es7.x实现常见操作(增、删、该、查、分组)
2021-03-11 09:30
标签:upd shm out ant rms false tom let date 在properties中增加下面配置 这样就可以了 需要的类 ... 后面会继续扩展 springBoot2.x集成es7.x实现常见操作(增、删、该、查、分组) 标签:upd shm out ant rms false tom let date 原文地址:https://www.cnblogs.com/liufei2/p/14129437.html集成es
引入依赖
配置
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.username=
spring.elasticsearch.rest.password=
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig {
private String host;
private Integer port;
@Bean(destroyMethod = "close")
public RestHighLevelClient client() {
return new RestHighLevelClient(RestClient.builder(
new HttpHost(host, port, "http")
));
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
基本操作前提
public class ObjectToMapUtils {
/**
* 将对象转换成Map
* @param bean
* @param
@Data
public class DocBean {
private String id;
private String firstCode;
private String secordCode;
private String content;
private Integer type;
private Date createdAt;
private Long startAt;
public DocBean(String id, String firstCode, String secordCode, String content, Integer type) {
this.id = id;
this.firstCode = firstCode;
this.secordCode = secordCode;
this.content = content;
this.type = type;
this.createdAt = new Date();
this.startAt = System.currentTimeMillis();
}
public DocBean() {
}
}
新增
public interface IElasticService {
void save(DocBean docBean) throws IOException;
}
@Service
public class IElasticServiceImpl implements IElasticService {
@Resource
private RestHighLevelClient client;
private static final String NBA_INDEX = "record_traffic-2020-12-10";
/**
* 新增操作
* @param docBean
* @throws IOException
*/
@Override
public void save(DocBean docBean) throws IOException {
IndexRequest request = new IndexRequest(NBA_INDEX).id(String.valueOf(docBean.getId())).source(ObjectToMapUtils.beanToMap(docBean));
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
}
}
修改
/**
* 更新数据
* @param docBean
* @throws IOException
*/
@Override
public void update(DocBean docBean) throws IOException {
UpdateRequest request = new UpdateRequest(NBA_INDEX, docBean.getId()).doc(ObjectToMapUtils.beanToMap(docBean));
client.update(request, RequestOptions.DEFAULT);
}
删除
根据单个id删除
@Override
public void deleteById(String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(NBA_INDEX, id);
client.delete(deleteRequest, RequestOptions.DEFAULT);
}
根据id批量删除
/***
* 删除操作。批量删除通过ids
* @param ids
* @throws IOException
*/
@Override
public void deleteByIds(String[] ids) throws IOException {
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(NBA_INDEX);
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.idsQuery().addIds(ids));
deleteByQueryRequest.setQuery(boolQueryBuilder);
client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
}
查询
根据id进行查询
@Override
public Map
分组和统计数量
@Override
public Map
文章标题:springBoot2.x集成es7.x实现常见操作(增、删、该、查、分组)
文章链接:http://soscw.com/index.php/essay/63141.html