深入elasticsearch(一):搭建elasticsearch环境及安装elasticsearch_head插件
深入elasticsearch(三):elasticsearch选主流程详解
SpringBoot版本:2.2.1.RELEASE
elasticsearch:5.4.2
jest:5.3.4
除springboot外相关的pom文件如下
io.searchbox
jest
5.3.4
org.elasticsearch
elasticsearch
5.4.2
elasticsearch:
jest:
connection-timeout: 3000
multi-threaded: true
uris: http://localhost:9900 //多个集群节点以,分隔
通过以上配置,就可以在项目中用jestClient来操作es了
public class JestCilentUtil {
private Gson gson = new Gson();
protected transient Logger logger = LoggerFactory.getLogger(ElasticServiceImpl.class);
@Autowired
JestClient jestClient;
/**
* 单个保存
**/
public boolean saveIndexDoc(String indexName, String indexType, String id, Map<String, Object> map) {
Index index = new Index.Builder(map).index(indexName).type(indexType).build();
JestResult jr = null;
boolean boll = false;
try {
jr = jestClient.execute(index);
boll = jr.isSucceeded();
} catch (IOException e) {
logger.error("插入失败{},{},{},{}", indexName, indexType, map, e);
}
return boll;
}
/**
* 批量添加
**/
public boolean saveIndexDocBulk(String indexName, String indexType, List<Map<String, Object>> list) {
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(indexType);
for (Map<String, Object> map : list) {
//如未设置索引唯一id值,则唯一id会默认生成,索引操作为添加操作
Index index = new Index.Builder(map).build();
bulk.addAction(index);
}
BulkResult br = null;
boolean boll = false;
try {
br = jestClient.execute(bulk.build());
boll = br.isSucceeded();
if (!boll) {
logger.error("插入失败{},{},{},{}", indexName, indexType, list, br.getJsonString());
}
} catch (IOException e) {
logger.error("插入失败{},{},{},{}", indexName, indexType, list, e);
}
return boll;
}
/**
* 修改,实际上jest操作save,如果带有id,且id在es中有值,也会更新
**/
public boolean updateDocById(String indexName, String indexType, Map<String, String> paramMap) {
Index index = new Index.Builder(paramMap).index(indexName).type(indexType).id(paramMap.get("id")).refresh(true).build();
JestResult jr = null;
boolean bool = false;
try {
jr = jestClient.execute(index);
bool = jr.isSucceeded();
} catch (IOException e) {
logger.error("更新失败{},{},{} ", indexName, indexType, paramMap, e);
}
return bool;
}
/**
* 万能分页搜索,匹配关键字
*/
public <T> List<T> baseSearch(String indexName, String indexType, String search
, Integer pageNo, Integer pageSize, Class<T> c) {
//设置默认页码第1页
pageNo = pageNo == null || pageNo < 1 ? 1 : pageNo;
//设置默认每页记录数20
pageSize = pageSize == null ? 20 : pageSize;
int startIndex = pageNo - 1;
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//设置默认分词后AND连接,StringQuery支持通配符
sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
//es分页从0开始
sourceBuilder.from(startIndex).size(pageSize);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
SearchResult result = null;
try {
result = jestClient.execute(sb);
} catch (IOException e) {
logger.error("查询es异常:{},{}", sourceBuilder.toString(), e);
}
long totalCount = 0;
List list = new ArrayList<>();
if (result != null && result.isSucceeded()) {
//获取总记录数
totalCount = result.getTotal();
if (totalCount > 0) {
JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
jsonObject.addProperty("id", jsonArray.get(i).getAsJsonObject().get("_id").toString());
Object o = gson.fromJson(jsonObject, c);
//将索引id存入map集合
list.add(o);
}
}
}
return list;
}
/**
* 万能分页搜索,匹配关键字
*
* @param indexName
* @param indexType
* @param search
* @param pageNo
* @param pageSize
* @param c
* @param
* @return 返回分页数,分页数据
*/
public <T> PageableBean<T> baseSearchPage(String indexName, String indexType, String search, Integer pageNo, Integer pageSize, Class<T> c) {
//设置默认页码第1页
pageNo = pageNo == null || pageNo < DEFAULT_PAGE ? DEFAULT_PAGE : pageNo;
//设置默认每页记录数20
pageSize = pageSize == null ? DEFAULT_SIZE : pageSize;
int startIndex = pageNo - 1;
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//设置默认分词后AND连接,StringQuery支持通配符
sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
//es分页从0开始
sourceBuilder.from(startIndex).size(pageSize);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
SearchResult result = null;
try {
result = jestClient.execute(sb);
} catch (IOException e) {
logger.error("查询es异常:{},{}", sourceBuilder.toString(), e);
}
long totalCount = 0;
List list = new ArrayList<>();
PageableBean pageableBean = new PageableBean();
pageableBean.setPageNo(pageNo);
pageableBean.setPageSize(pageSize);
if (result != null && result.isSucceeded()) {
//获取总记录数
totalCount = result.getTotal();
if (totalCount > 0) {
/*JsonArray解析结果集*/
JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
jsonObject.addProperty("id", jsonArray.get(i).getAsJsonObject().get("_id").toString());
Object o = gson.fromJson(jsonObject, c);
//将索引id存入map集合
list.add(o);
}
pageableBean.setDatas(list);
pageableBean.setCount(totalCount);
}
}
return pageableBean;
}
/**
* 自定组合BoolQueryBuilder 查询
*/
@Override
public <T> PageableBean<T> baseSearchPageByParams(String indexName, String indexType, BoolQueryBuilder queryBuilder, Integer pageNo, Integer pageSize, Class<T> c) {
//设置默认页码第1页
pageNo = pageNo == null || pageNo < DEFAULT_PAGE ? DEFAULT_PAGE : pageNo;
//设置默认每页记录数20
pageSize = pageSize == null ? DEFAULT_SIZE : pageSize;
int startIndex = pageNo - 1;
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//设置默认分词后AND连接,StringQuery支持通配符
sourceBuilder.query(queryBuilder);
sourceBuilder.from(startIndex).size(pageSize);
sourceBuilder.sort("gmt_create", SortOrder.ASC);
Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
SearchResult result = null;
try {
result = jestClient.execute(sb);
} catch (IOException e) {
logger.error("查询es异常:{},{}", sourceBuilder.toString(), e);
}
long totalCount = 0;
List list = new ArrayList<>();
PageableBean pageableBean = new PageableBean();
pageableBean.setPageNo(pageNo);
pageableBean.setPageSize(pageSize);
if (result != null && result.isSucceeded()) {
//获取总记录数
totalCount = result.getTotal();
if (totalCount > 0) {
/*JsonArray解析结果集*/
JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
jsonObject.addProperty("id", jsonArray.get(i).getAsJsonObject().get("_id").getAsString());
Object o = gson.fromJson(jsonObject, c);
//将索引id存入map集合
list.add(o);
}
pageableBean.setDatas(list);
pageableBean.setCount(totalCount);
}
}
return pageableBean;
}
public class PageableBean<T> {
private Long count;
private List<T> datas;
private Integer pageNo;
private Integer pageSize;
private Long sortScore;
public Long getSortScore() {
return sortScore;
}
public void setSortScore(Long sortScore) {
this.sortScore = sortScore;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
public List<T> getDatas() {
return datas;
}
public void setDatas(List<T> datas) {
this.datas = datas;
}
@Override
public String toString() {
return "PageableBean{" +
"count=" + count +
", datas=" + datas +
", pageNo=" + pageNo +
", pageSize=" + pageSize +
'}';
}