es中的一些api使用
2021-03-07 10:29
标签:imp highlight ack 程序 hash resource http try rate 尽量写一些通用的service。还能再抽象的,暂时先不继续了。 1.新建测试用的mapping 2.程序结构 3.controller 4.bussiness 5.businessImpl 6.service 7.serviceImpl 8.BeanToMapUtils 9.application.properties es中的一些api使用 标签:imp highlight ack 程序 hash resource http try rate 原文地址:https://www.cnblogs.com/juncaoit/p/12827578.htmlPUT /nba
{
"mappings": {
"properties": {
"jerseNo" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
},
"playYear" : {
"type" : "long"
},
"position" : {
"type" : "text"
},
"teamName" : {
"type" : "text"
}
}
}
}
package com.jun.fastpro.controller;
import com.jun.fastpro.business.EsOperateBusiness;
import com.jun.fastpro.common.domain.response.BaseResponse;
import com.jun.fastpro.dto.EsDto;
import com.jun.fastpro.dto.NbaDto;
import com.jun.fastpro.service.EsCurdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@Api(value = "es主方法", tags = "es的control")
public class EsOperateController {
@Resource
private EsCurdService esCurdService;
@Resource
private EsOperateBusiness esOperateBusiness;
private static final String INDEX = "nba";
@ApiOperation("ping es服务器")
@GetMapping("/ping")
public boolean ping(){
boolean ping = esCurdService.ping();
return ping;
}
// ================================文档的操作==================
@ApiOperation("新增文档")
@PostMapping("/add")
public BaseResponse
package com.jun.fastpro.business;
import com.jun.fastpro.common.domain.response.BaseResponse;
import com.jun.fastpro.dto.EsDto;
import com.jun.fastpro.dto.NbaDto;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
import java.util.Map;
/**
* 用于业务将service分开
*/
public interface EsOperateBusiness {
/**
* 新增文档
*/
public boolean add(EsDto
package com.jun.fastpro.business.impl;
import com.jun.fastpro.business.EsOperateBusiness;
import com.jun.fastpro.common.utils.BeanToMapUtils;
import com.jun.fastpro.dto.EsDto;
import com.jun.fastpro.dto.NbaDto;
import com.jun.fastpro.service.EsCurdService;
import org.dozer.Mapper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class EsOperateBusinessImpl implements EsOperateBusiness {
@Resource
private EsCurdService esCurdService;
@Override
public boolean add(EsDto
package com.jun.fastpro.service;
import com.jun.fastpro.dto.EsDto;
import com.jun.fastpro.dto.NbaDto;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @Descrotion 这里是一个通用的es功能service
*/
public interface EsCurdService {
/**
* 检查集群是否可用
*/
public boolean ping();
/**
* 增加文档
*/
public boolean add(Map map, String index, String id);
/**
* 查询文档
*/
public Map get(String index, String id);
/**
* 更新文档
*/
public boolean update(Map map, String index, String id);
/**
* 删除文档
*/
public boolean delete(String index, String id);
/**
* match查询
*/
public List
package com.jun.fastpro.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jun.fastpro.dto.EsDto;
import com.jun.fastpro.dto.NbaDto;
import com.jun.fastpro.service.EsCurdService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
/**
* @Descrotion 这里是一个通用的es功能service
*/
@Service
@Slf4j
public class EsCurdServiceImpl implements EsCurdService {
@Resource
private RestHighLevelClient highLevelClient;
/**
* 检查集群是否可用
* 虽然es不可用了,但是不会起阻断性的结果,程序是依然进行运行的
*/
public boolean ping(){
try {
boolean pin = highLevelClient.ping(RequestOptions.DEFAULT);
return pin;
} catch (IOException e) {
e.printStackTrace();
log.info("继续运行");
}
return false;
}
/**
* 增加
*/
public boolean add(Map stringObjectMap, String index, String id){
if(Objects.isNull(stringObjectMap)){
return false;
}
// 执行
IndexRequest indexRequest = new IndexRequest(index).id(id).source(stringObjectMap);
try {
IndexResponse response = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
log.info("增加返回结果:{}", JSONObject.toJSON(response));
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* 查询文档
*/
public Map get(String index, String id){
GetRequest getRequest = new GetRequest(index, id);
try {
GetResponse response = highLevelClient.get(getRequest, RequestOptions.DEFAULT);
return response.getSource();
} catch (IOException e) {
e.printStackTrace();
}
return Maps.newHashMap();
}
/**
* 更新文档
*/
public boolean update(Map map, String index, String id){
UpdateRequest updateRequest = new UpdateRequest(index, id).doc(map);
try {
UpdateResponse response = highLevelClient.update(updateRequest, RequestOptions.DEFAULT);
log.info("更新文档返回结果:{}", JSONObject.toJSON(response));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 删除文档
*/
public boolean delete(String index, String id){
DeleteRequest deleteRequest = new DeleteRequest(index, id);
try {
DeleteResponse response = highLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
log.info("删除文档返回结果:{}", JSONObject.toJSON(response));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* match查询
*/
public List
package com.jun.fastpro.common.utils;
import org.springframework.cglib.beans.BeanMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class BeanToMapUtils {
/**
* 将bean转为map
*/
public static
spring.elasticsearch.rest.uris=http://10.1.1.4:9200
server.port=9092