新建項(xiàng)目 springboot-es-test
源代碼:http://gitlab.heatedloan.com/cici/springboot-es-test
本地項(xiàng)目:/Users/cici/workspace/cici/springboot-es-test
第一步:pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.2</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.39</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
第二步:application.properties
# Elasticsearch
# 9200端口是用來(lái)讓HTTP REST API來(lái)訪問(wèn)ElasticSearch,而9300端口是傳輸層監(jiān)聽(tīng)的默認(rèn)端口
elasticsearch.ip=127.0.0.1
elasticsearch.port=9300
elasticsearch.pool=5
#注意cluster.name需要與config/elasticsearch.yml中的cluster.name一致
elasticsearch.cluster.name=elasticsearch_cici
server.port=8181
第三步:創(chuàng)建員工對(duì)象實(shí)體類 和 分頁(yè)類
員工對(duì)象實(shí)體類
/**
* @Description:Book實(shí)體 加上了@Document注解之后七咧,默認(rèn)情況下這個(gè)實(shí)體中所有的屬性都會(huì)被建立索引跃惫、并且分詞
*/
@Data
@ToString
@NoArgsConstructor
public class Employee {
private String id;
private Long version;
String firstName;
String lastName;
String age;
String[] interests;
}
分頁(yè)類
@Data
@ToString
public class EsPage {
/**
* 當(dāng)前頁(yè)
*/
private int currentPage;
/**
* 每頁(yè)顯示多少條
*/
private int pageSize;
/**
* 總記錄數(shù)
*/
private int recordCount;
/**
* 本頁(yè)的數(shù)據(jù)列表
*/
private List<Map<String, Object>> recordList;
/**
* 總頁(yè)數(shù)
*/
private int pageCount;
/**
* 頁(yè)碼列表的開(kāi)始索引(包含)
*/
private int beginPageIndex;
/**
* 頁(yè)碼列表的結(jié)束索引(包含)
*/
private int endPageIndex;
/**
* 只接受前4個(gè)必要的屬性叮叹,會(huì)自動(dòng)的計(jì)算出其他3個(gè)屬性的值
*
* @param currentPage
* @param pageSize
* @param recordCount
* @param recordList
*/
public EsPage(int currentPage, int pageSize, int recordCount, List<Map<String, Object>> recordList) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.recordCount = recordCount;
this.recordList = recordList;
// 計(jì)算總頁(yè)碼
pageCount = (recordCount + pageSize - 1) / pageSize;
// 計(jì)算 beginPageIndex 和 endPageIndex
// >> 總頁(yè)數(shù)不多于10頁(yè)艾栋,則全部顯示
if (pageCount <= 10) {
beginPageIndex = 1;
endPageIndex = pageCount;
}
// 總頁(yè)數(shù)多于10頁(yè),則顯示當(dāng)前頁(yè)附近的共10個(gè)頁(yè)碼
else {
// 當(dāng)前頁(yè)附近的共10個(gè)頁(yè)碼(前4個(gè) + 當(dāng)前頁(yè) + 后5個(gè))
beginPageIndex = currentPage - 4;
endPageIndex = currentPage + 5;
// 當(dāng)前面的頁(yè)碼不足4個(gè)時(shí)蛉顽,則顯示前10個(gè)頁(yè)碼
if (beginPageIndex < 1) {
beginPageIndex = 1;
endPageIndex = 10;
}
// 當(dāng)后面的頁(yè)碼不足5個(gè)時(shí)蝗砾,則顯示后10個(gè)頁(yè)碼
if (endPageIndex > pageCount) {
endPageIndex = pageCount;
beginPageIndex = pageCount - 10 + 1;
}
}
}
}
第四步:創(chuàng)建config類
@Configuration
public class ElasticSearchConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchConfig.class);
/**
* elk集群地址
*/
@Value("${elasticsearch.ip}")
private String hostName;
/**
* 端口
*/
@Value("${elasticsearch.port}")
private String port;
/**
* 集群名稱
*/
@Value("${elasticsearch.cluster.name}")
private String clusterName;
/**
* 連接池
*/
@Value("${elasticsearch.pool}")
private String poolSize;
/**
* Bean name default 函數(shù)名字
*
* @return
*/
@Bean(name = "transportClient")
public TransportClient transportClient() {
LOGGER.info("Elasticsearch初始化開(kāi)始。携冤。悼粮。。曾棕。");
TransportClient transportClient = null;
try {
// 配置信息
Settings esSetting = Settings.builder()
.put("cluster.name", clusterName) //集群名字
.put("client.transport.sniff", true)//增加嗅探機(jī)制扣猫,找到ES集群
.put("thread_pool.search.size", Integer.parseInt(poolSize))//增加線程池個(gè)數(shù),暫時(shí)設(shè)為5
.build();
//配置信息Settings自定義
transportClient = new PreBuiltTransportClient(esSetting);
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
transportClient.addTransportAddresses(transportAddress);
} catch (Exception e) {
LOGGER.error("elasticsearch TransportClient create error!!", e);
}
return transportClient;
}
}
第五步:創(chuàng)建ES操作工具類
Elasticsearch JAVA操作有三種客戶端:
1翘地、TransportClient
2申尤、JestClient
3、RestClient
還有種是2.3中有的NodeClient,在5.5.1中好像沒(méi)有了衙耕。
還有種是spring-data-elasticsearch,這里先以TransportClient為例子昧穿。
@Component
public class ElasticsearchUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchUtil.class);
@Autowired
private TransportClient transportClient;
private static TransportClient client;
/**
* @PostContruct是spring框架的注解 spring容器初始化的時(shí)候執(zhí)行該方法
*/
@PostConstruct
public void init() {
client = this.transportClient;
}
/**
* 創(chuàng)建索引
*
* @param index
* @return
*/
public static boolean createIndex(String index) {
if (!isIndexExist(index)) {
LOGGER.info("Index is not exits!");
}
CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
LOGGER.info("執(zhí)行建立成功?" + indexresponse.isAcknowledged());
return indexresponse.isAcknowledged();
}
/**
* 刪除索引
*
* @param index
* @return
*/
public static boolean deleteIndex(String index) {
if (!isIndexExist(index)) {
LOGGER.info("Index is not exits!");
}
DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
if (dResponse.isAcknowledged()) {
LOGGER.info("delete index " + index + " successfully!");
} else {
LOGGER.info("Fail to delete index " + index);
}
return dResponse.isAcknowledged();
}
/**
* 判斷索引是否存在
*
* @param index
* @return
*/
public static boolean isIndexExist(String index) {
IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
if (inExistsResponse.isExists()) {
LOGGER.info("Index [" + index + "] is exist!");
} else {
LOGGER.info("Index [" + index + "] is not exist!");
}
return inExistsResponse.isExists();
}
/**
* @Description: 判斷inde下指定type是否存在
*/
public boolean isTypeExist(String index, String type) {
return isIndexExist(index)
? client.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists()
: false;
}
/**
* 數(shù)據(jù)添加橙喘,正定ID
*
* @param jsonObject 要增加的數(shù)據(jù)
* @param index 索引时鸵,類似數(shù)據(jù)庫(kù)
* @param type 類型,類似表
* @param id 數(shù)據(jù)ID
* @return
*/
public static String addData(JSONObject jsonObject, String index, String type, String id) {
IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();
LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());
return response.getId();
}
/**
* 數(shù)據(jù)添加
*
* @param jsonObject 要增加的數(shù)據(jù)
* @param index 索引厅瞎,類似數(shù)據(jù)庫(kù)
* @param type 類型饰潜,類似表
* @return
*/
public static String addData(JSONObject jsonObject, String index, String type) {
return addData(jsonObject, index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
}
/**
* 通過(guò)ID刪除數(shù)據(jù)
*
* @param index 索引初坠,類似數(shù)據(jù)庫(kù)
* @param type 類型,類似表
* @param id 數(shù)據(jù)ID
*/
public static void deleteDataById(String index, String type, String id) {
DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());
}
/**
* 通過(guò)ID 更新數(shù)據(jù)
*
* @param jsonObject 要增加的數(shù)據(jù)
* @param index 索引彭雾,類似數(shù)據(jù)庫(kù)
* @param type 類型某筐,類似表
* @param id 數(shù)據(jù)ID
* @return
*/
public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index).type(type).id(id).doc(jsonObject);
client.update(updateRequest);
}
/**
* 通過(guò)ID獲取數(shù)據(jù)
*
* @param index 索引,類似數(shù)據(jù)庫(kù)
* @param type 類型冠跷,類似表
* @param id 數(shù)據(jù)ID
* @param fields 需要顯示的字段南誊,逗號(hào)分隔(缺省為全部字段)
* @return
*/
public static Map<String, Object> searchDataById(String index, String type, String id, String fields) {
GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
if (StringUtils.isNotEmpty(fields)) {
getRequestBuilder.setFetchSource(fields.split(","), null);
}
GetResponse getResponse = getRequestBuilder.execute().actionGet();
return getResponse.getSource();
}
/**
* 使用分詞查詢,并分頁(yè)
*
* @param index 索引名稱
* @param type 類型名稱,可傳入多個(gè)type逗號(hào)分隔
* @param startPage 當(dāng)前頁(yè)
* @param pageSize 每頁(yè)顯示條數(shù)
* @param query 查詢條件
* @param fields 需要顯示的字段,逗號(hào)分隔(缺省為全部字段)
* @param sortField 排序字段
* @param highlightField 高亮字段
* @return
*/
public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
if (StringUtils.isNotEmpty(type)) {
searchRequestBuilder.setTypes(type.split(","));
}
searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
// 需要顯示的字段蜜托,逗號(hào)分隔(缺省為全部字段)
if (StringUtils.isNotEmpty(fields)) {
searchRequestBuilder.setFetchSource(fields.split(","), null);
}
//排序字段
if (StringUtils.isNotEmpty(sortField)) {
searchRequestBuilder.addSort(sortField, SortOrder.DESC);
}
// 高亮(xxx=111,aaa=222)
if (StringUtils.isNotEmpty(highlightField)) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
//highlightBuilder.preTags("<span style='color:red' >");//設(shè)置前綴
//highlightBuilder.postTags("</span>");//設(shè)置后綴
// 設(shè)置高亮字段
highlightBuilder.field(highlightField);
searchRequestBuilder.highlighter(highlightBuilder);
}
//searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
searchRequestBuilder.setQuery(query);
// 分頁(yè)應(yīng)用
searchRequestBuilder.setFrom(startPage).setSize(pageSize);
// 設(shè)置是否按查詢匹配度排序
searchRequestBuilder.setExplain(true);
//打印的內(nèi)容 可以在 Elasticsearch head 和 Kibana 上執(zhí)行查詢
LOGGER.info("\n{}", searchRequestBuilder);
// 執(zhí)行搜索,返回搜索響應(yīng)信息
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
long totalHits = searchResponse.getHits().totalHits;
long length = searchResponse.getHits().getHits().length;
LOGGER.debug("共查詢到[{}]條數(shù)據(jù),處理數(shù)據(jù)條數(shù)[{}]", totalHits, length);
if (searchResponse.status().getStatus() == 200) {
// 解析對(duì)象
List<Map<String, Object>> sourceList = setSearchResponse(searchResponse, highlightField);
return new EsPage(startPage, pageSize, (int) totalHits, sourceList);
}
return null;
}
/**
* 使用分詞查詢
*
* @param index 索引名稱
* @param type 類型名稱,可傳入多個(gè)type逗號(hào)分隔
* @param query 查詢條件
* @param size 文檔大小限制
* @param fields 需要顯示的字段抄囚,逗號(hào)分隔(缺省為全部字段)
* @param sortField 排序字段
* @param highlightField 高亮字段
* @return
*/
public static List<Map<String, Object>> searchListData(
String index, String type, QueryBuilder query, Integer size,
String fields, String sortField, String highlightField) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
if (StringUtils.isNotEmpty(type)) {
searchRequestBuilder.setTypes(type.split(","));
}
if (StringUtils.isNotEmpty(highlightField)) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
// 設(shè)置高亮字段
highlightBuilder.field(highlightField);
searchRequestBuilder.highlighter(highlightBuilder);
}
searchRequestBuilder.setQuery(query);
if (StringUtils.isNotEmpty(fields)) {
searchRequestBuilder.setFetchSource(fields.split(","), null);
}
searchRequestBuilder.setFetchSource(true);
if (StringUtils.isNotEmpty(sortField)) {
searchRequestBuilder.addSort(sortField, SortOrder.DESC);
}
if (size != null && size > 0) {
searchRequestBuilder.setSize(size);
}
//打印的內(nèi)容 可以在 Elasticsearch head 和 Kibana 上執(zhí)行查詢
LOGGER.info("\n{}", searchRequestBuilder);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
long totalHits = searchResponse.getHits().totalHits;
long length = searchResponse.getHits().getHits().length;
LOGGER.info("共查詢到[{}]條數(shù)據(jù),處理數(shù)據(jù)條數(shù)[{}]", totalHits, length);
if (searchResponse.status().getStatus() == 200) {
// 解析對(duì)象
return setSearchResponse(searchResponse, highlightField);
}
return null;
}
/**
* 高亮結(jié)果集 特殊處理
*
* @param searchResponse
* @param highlightField
*/
private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();
StringBuffer stringBuffer = new StringBuffer();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
searchHit.getSourceAsMap().put("id", searchHit.getId());
if (StringUtils.isNotEmpty(highlightField)) {
System.out.println("遍歷 高亮結(jié)果集,覆蓋 正常結(jié)果集" + searchHit.getSourceAsMap());
Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();
if (text != null) {
for (Text str : text) {
stringBuffer.append(str.string());
}
//遍歷 高亮結(jié)果集橄务,覆蓋 正常結(jié)果集
searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString());
}
}
sourceList.add(searchHit.getSourceAsMap());
}
return sourceList;
}
}
第六步:創(chuàng)建controller
@RestController
@RequestMapping("/es")
public class EsController {
/**
* 測(cè)試索引
*/
private String indexName = "megacorp";
/**
* 類型
*/
private String esType = "employee";
/**
* 創(chuàng)建索引
* http://127.0.0.1:8080/es/createIndex
* @param request
* @param response
* @return
*/
@RequestMapping("/createIndex")
public String createIndex(HttpServletRequest request, HttpServletResponse response) {
if (!ElasticsearchUtil.isIndexExist(indexName)) {
ElasticsearchUtil.createIndex(indexName);
} else {
return "索引已經(jīng)存在";
}
return "索引創(chuàng)建成功";
}
/**
* 插入記錄
*
* @return
*/
@RequestMapping("/insertJson")
public String insertJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", DateUtil.formatDate(new Date()));
jsonObject.put("age", 25);
jsonObject.put("first_name", "j-" + new Random(100).nextInt());
jsonObject.put("last_name", "cccc");
jsonObject.put("about", "i like xiaofeng baby");
jsonObject.put("date", new Date());
String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
return id;
}
/**
* 插入記錄
*
* @return
*/
@RequestMapping("/insertModel")
public String insertModel() {
Employee employee = new Employee();
employee.setId("66");
employee.setFirstName("m-" + new Random(100).nextInt());
employee.setAge("24");
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(employee);
String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
return id;
}
/**
* 刪除記錄
*
* @return
*/
@RequestMapping("/delete")
public String delete(String id) {
if (StringUtils.isNotBlank(id)) {
ElasticsearchUtil.deleteDataById(indexName, esType, id);
return "刪除id=" + id;
} else {
return "id為空";
}
}
/**
* 更新數(shù)據(jù)
*
* @return
*/
@RequestMapping("/update")
public String update(String id) {
if (StringUtils.isNotBlank(id)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("age", 31);
jsonObject.put("name", "修改");
jsonObject.put("date", new Date());
ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
return "id=" + id;
} else {
return "id為空";
}
}
/**
* 獲取數(shù)據(jù)
* http://127.0.0.1:8080/es/getData?id=2018-04-25%2016:33:44
*
* @param id
* @return
*/
@RequestMapping("/getData")
public String getData(String id) {
if (StringUtils.isNotBlank(id)) {
Map<String, Object> map = ElasticsearchUtil.searchDataById(indexName, esType, id, null);
return JSONObject.toJSONString(map);
} else {
return "id為空";
}
}
/**
* 查詢數(shù)據(jù)
* 模糊查詢
*
* @return
*/
@RequestMapping("/queryMatchData")
public String queryMatchData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolean matchPhrase = false;
if (matchPhrase == Boolean.TRUE) {
//不進(jìn)行分詞搜索
boolQuery.must(QueryBuilders.matchPhraseQuery("first_name", "cici"));
} else {
boolQuery.must(QueryBuilders.matchQuery("last_name", "cici"));
}
List<Map<String, Object>> list = ElasticsearchUtil.
searchListData(indexName, esType, boolQuery, 10, "first_name", null, "last_name");
return JSONObject.toJSONString(list);
}
/**
* 通配符查詢數(shù)據(jù)
* 通配符查詢 ?用來(lái)匹配1個(gè)任意字符幔托,*用來(lái)匹配零個(gè)或者多個(gè)字符
*
* @return
*/
@RequestMapping("/queryWildcardData")
public String queryWildcardData() {
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("first_name.keyword", "cici");
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
return JSONObject.toJSONString(list);
}
/**
* 正則查詢
*
* @return
*/
@RequestMapping("/queryRegexpData")
public String queryRegexpData() {
QueryBuilder queryBuilder = QueryBuilders.regexpQuery("first_name.keyword", "m--[0-9]{1,11}");
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
return JSONObject.toJSONString(list);
}
/**
* 查詢數(shù)字范圍數(shù)據(jù)
*
* @return
*/
@RequestMapping("/queryIntRangeData")
public String queryIntRangeData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from(24)
.to(25));
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
return JSONObject.toJSONString(list);
}
/**
* 查詢?nèi)掌诜秶鷶?shù)據(jù)
*
* @return
*/
@RequestMapping("/queryDateRangeData")
public String queryDateRangeData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
.to("50"));
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
return JSONObject.toJSONString(list);
}
/**
* 查詢分頁(yè)
*
* @param startPage 第幾條記錄開(kāi)始
* 從0開(kāi)始
* 第1頁(yè) :http://127.0.0.1:8080/es/queryPage?startPage=0&pageSize=2
* 第2頁(yè) :http://127.0.0.1:8080/es/queryPage?startPage=2&pageSize=2
* @param pageSize 每頁(yè)大小
* @return
*/
@RequestMapping("/queryPage")
public String queryPage(String startPage, String pageSize) {
if (StringUtils.isNotBlank(startPage) && StringUtils.isNotBlank(pageSize)) {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
.to("100"));
EsPage list = ElasticsearchUtil.searchDataPage(indexName, esType, Integer.parseInt(startPage), Integer.parseInt(pageSize), boolQuery, null, null, null);
return JSONObject.toJSONString(list);
} else {
return "startPage或者pageSize缺失";
}
}
}
利用postman一個(gè)個(gè)請(qǐng)求。
截取結(jié)果如下:方法都能正常準(zhǔn)確訪問(wèn)