elasticsearch增刪改查、springboot整合es
阿新 • • 發佈:2020-12-08
以下為部分程式碼,部分執行介面。
我有全套課程設計資源 ,此係統功能完善。
java全部程式碼(可順利執行),資料庫資訊,課程設計報告,完整視訊演示 如有需要具體請聯絡圖片!!!!!!!!!看下面圖片!!!
package com.example.dao;
import com.example.constant.Constants;
import com.example.entity.Person;
import com.example.utils.EsRestHighLevelClient;
import org.elasticsearch. action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org. elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
* @author null
*/
@Repository
public class PersonDao {
public static Logger log = Logger.getLogger(PersonDao.class.toString());
private static RestHighLevelClient client = EsRestHighLevelClient.getEsClient();
/**
* 查詢
*
* @return 返回SearchHit 籃子物件
*/
public SearchHit[] listPerson(String name) {
try {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (null != name && !"".equals(name)) {
boolQueryBuilder.must(QueryBuilders.matchQuery("name", name));
}
sourceBuilder.query(boolQueryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hs = searchResponse.getHits().getHits();
return hs;
} catch (Exception e) {
log.warning("查詢資訊時異常,查詢es失敗");
return null;
}
}
/**
* 查詢一個
*
* @return 返回SearchHit 籃子物件
*/
public SearchHit[] getPersonById(String id) {
try {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("id", id));
sourceBuilder.query(boolQueryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hs = searchResponse.getHits().getHits();
return hs;
} catch (Exception e) {
log.warning("查詢資訊時異常,查詢es失敗");
return null;
}
}
/**
* 插入或更新
*
* @param person 人員物件
*/
public void insertOrUpdatePerson(Person person) {
try {
BulkRequest bulkRequest = new BulkRequest();
Map<String, Object> jsonMap = new HashMap<>(1);
jsonMap.put("id", person.getId());
jsonMap.put("name", person.getName());
jsonMap.put("age", person.getAge());
jsonMap.put("isNeighbourhood", person.getIsNeighbourhood());
IndexRequest indexRequest = new IndexRequest("person")
.id(String.valueOf(person.getId())).source(jsonMap);
bulkRequest.add(indexRequest);
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.warning("資料庫寫入/更新 ES成功");
} catch (IOException e) {
log.warning("資料寫入/更新 ES發生IO異常!");
} catch (Throwable e) {
log.warning("資料寫入/更新 ES發生異常!");
}
}
/**
* 刪除
*
* @param person 人員物件
*/
public void deletePerson(Person person) {
DeleteRequest deleteRequest = new DeleteRequest("person", String.valueOf(person.getId()));
try {
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
log.info("刪除" + deleteResponse.getId() + ", 狀態為:" + deleteResponse.status());
} catch (IOException e) {
log.warning("ES刪除資料發生IO異常!");
}
}
}