關于ElasticSearch的常用增刪改查DSL和代碼
es增刪改查常用語法
我們日常開發(fā)中,操作數(shù)據(jù)庫寫sql倒是不可能忘記,但是操作es的dsl語句有時候很容易忘記,特地記錄一下方便查找。
注意,如果有些字段設置的text類型,那么在查詢的時候加上.keyword,比如查詢code字段
{ "query": { "terms": { "code.keyword": ["aaa","bbb"] } } }
DSL語句
1、創(chuàng)建索引
-- 創(chuàng)建索引 PUT /my_index { "mappings": { "properties": { "title": { "type": "text" }, "description": { "type": "text" }, "timestamp": { "type": "date" } } } }
2、插入文檔
-- 插入文檔 POST /my_index/_doc/主鍵值 { "title": "Sample Document", "description": "This is a sample document for Elasticsearch", "timestamp": "2022-01-01" }
3、更新文檔
-- 更新一個文檔 POST /my_index/_update/1 // 主鍵 { "doc": { "字段1": "內容1", "字段2": "內容2", "字段3": "內容3" // 添加需要更新的多個字段及對應的內容 } } -- 批量更新多個文檔字段 POST /my_index/_update_by_query { "query": { "terms": { "_id": [111, 222] // 指定id為111和222的文檔 }, "script": { "source": "ctx._source.aaa= 1; ctx._source.bbb= 2" // 更新aaa和bbb字段為1和2 } } -- 批量更新多個文檔字,指定字段內容為另外一個字段的內容 POST /search_order_index/_update_by_query { "query": { "terms": { "orderNo": [ "1111", // 指定更新訂單編號為111和222的記錄 "222" ] } }, "script": { "source": " ctx._source.字段1= 1; // 指定字段1 為1 ctx._source.字段2= 1; // 指定字段2 為1 ctx._source.字段3= 0; // 指定字段3為0 ctx._source.字段4= ctx._source.字段5" // 指定字段4為字段5的內容 } } -- 更新所有文檔字段 POST /my_index/_update_by_query { "query": { "match_all": {} // 匹配所有文檔 }, "script": { "source": "ctx._source.aaa = 1; ctx._source.bbb = 1" // 批量更新aaa和bbb字段為1 } }
4、刪除文檔(單獨、多個、全部)
-- 刪除單條文檔 DELETE /my_index/_doc/主鍵值 或者 -- 刪除單條文檔 POST 索引名/_delete_by_query { "query":{ "term":{ "_id":4043 } } } -- 刪除多條文檔 POST 索引名/_delete_by_query { "query": { "terms": { "_id": [4043, 4044, 4045] // 添加多個id值 } } } -- 刪除索引中的所有數(shù)據(jù) POST my_index/_delete_by_query { "query": { "match_all": { } } }
5、刪除索引
-- 刪除索引 DELETE /my_index
6、設置索引別名
-- 設置索引別名 POST /_aliases { "actions": [ {"add": {"index": "my_index2", "alias": "my_index"}} ] }
7、設置切片和副本數(shù)量
-- 設置切片和副本數(shù)量 PUT your_index { "mappings" : { "properties" : { #索引字段(略) } } "settings" : { "number_of_shards" : 3, "number_of_replicas" : 1 } }
8、查詢
-- 查詢單個字段內容 POST /my_index/_search { "query": { "bool": { "must": { "term": { "messageId": "CS202303160008-2" } } } } } -- 查詢單個字段的多個內容 類似mysql中的in 用terms 多了個s POST /my_index/_search { "query": { "bool": { "must": { "terms": { "messageId": ["22222","1111"] } } } } } -- 分頁排序查詢 不帶其他條件 POST /my_index/_search { "query": { "match_all": {} }, "from": 0, "size": 20, "sort": [ { "createdAt": { "order": "desc" } } ] } -- 分頁排序查詢 帶其他條件 { "query": { "bool": { "must": [ { "prefix": { "action": "aa開頭" } }, { "wildcard": { "param": "*左右匹配內容*" } } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [ { "createdAt": { "order": "desc" } } ] }
9、統(tǒng)計
POST /my_index/_count { "query": { "bool": { "must": { "term": { "messageId": "CS202303160008-2" } } } } }
代碼
pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.sdk</groupId> <artifactId>elasticsearch-util</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>elasticsearch-util</name> <description>Spring Boot Support for elasticsearch-util</description> <properties> <java.version>11</java.version> <elasticsearch.version>7.10.0</elasticsearch.version> <spring-boot.version>2.7.0</spring-boot.version> <hutool.version>5.8.15</hutool.version> <pagehelper.version>1.4.2</pagehelper.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring-boot.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
1、ES配置類
import cn.hutool.core.text.CharSequenceUtil; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.springframework.boot.context.properties.ConfigurationProperties; /** * ES配置類 * @author ppp * @date 2023/3/21 */ @ConfigurationProperties(prefix = "elasticsearch.config") public class ElasticsearchProperties { /** * 域名 */ private String host; /** * 端口 */ private String port; /** * 用戶名 */ private String username; /** * 密碼 */ private String password; /** * 連接超時時間 */ private int connectTimeOut; /** * 連接超時時間 */ private int socketTimeOut; /** * 獲取連接的超時時間 */ private int connectionRequestTimeOut; /** * 獲取搜索的超時時間 */ private long searchRequestTimeOut = 10000L; /** * 最大連接數(shù) */ private int maxConnectNum; /** * 最大路由連接數(shù) */ private int maxConnectPerRoute; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public int getConnectTimeOut() { return connectTimeOut; } public void setConnectTimeOut(int connectTimeOut) { this.connectTimeOut = connectTimeOut; } public int getSocketTimeOut() { return socketTimeOut; } public void setSocketTimeOut(int socketTimeOut) { this.socketTimeOut = socketTimeOut; } public int getConnectionRequestTimeOut() { return connectionRequestTimeOut; } public void setConnectionRequestTimeOut(int connectionRequestTimeOut) { this.connectionRequestTimeOut = connectionRequestTimeOut; } public long getSearchRequestTimeOut() { return searchRequestTimeOut; } public void setSearchRequestTimeOut(long searchRequestTimeOut) { this.searchRequestTimeOut = searchRequestTimeOut; } public int getMaxConnectNum() { return maxConnectNum; } public void setMaxConnectNum(int maxConnectNum) { this.maxConnectNum = maxConnectNum; } public int getMaxConnectPerRoute() { return maxConnectPerRoute; } public void setMaxConnectPerRoute(int maxConnectPerRoute) { this.maxConnectPerRoute = maxConnectPerRoute; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public CredentialsProvider getCredentialsProvider() { if (CharSequenceUtil.isNotBlank(username) && CharSequenceUtil.isNotBlank(password)) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); return credentialsProvider; } return null; } }
2、ES工具自動配置類
import cn.hutool.core.text.CharSequenceUtil; import com.demo.sdk.elasticsearch.template.ElasticsearchUtilTemplate; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * ES工具自動配置類 * @author ppp * @date 2023/3/21 */ @Configuration @ConditionalOnClass(ElasticsearchUtilTemplate.class) @EnableConfigurationProperties(ElasticsearchProperties.class) public class ElasticsearchUtilAutoConfiguration { @Bean @ConditionalOnMissingBean public RestHighLevelClient esRestClient(ElasticsearchProperties esearchProperties) { if (esearchProperties == null) { throw new NullPointerException("Es Configuration Properties Is Null"); } String host = esearchProperties.getHost(); String port = esearchProperties.getPort(); RestClientBuilder builder; if (CharSequenceUtil.isNotBlank(host) && CharSequenceUtil.isBlank(port)) { builder = RestClient.builder(HttpHost.create(host)); }else { builder = RestClient.builder(new HttpHost(host, Integer.parseInt(port))); } // 異步httpclient連接延時配置 builder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectTimeout(esearchProperties.getConnectTimeOut()); requestConfigBuilder.setSocketTimeout(esearchProperties.getSocketTimeOut()); requestConfigBuilder.setConnectionRequestTimeout(esearchProperties.getConnectionRequestTimeOut()); return requestConfigBuilder; }); // 異步httpclient連接數(shù)配置 builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(esearchProperties.getMaxConnectNum()); httpClientBuilder.setMaxConnPerRoute(esearchProperties.getMaxConnectPerRoute()); httpClientBuilder.setDefaultCredentialsProvider(esearchProperties.getCredentialsProvider()); return httpClientBuilder; }); return new RestHighLevelClient(builder); } @Bean @ConditionalOnMissingBean public ElasticsearchUtilTemplate elasticsearchUtilTemplate(RestHighLevelClient esRestClient, ElasticsearchProperties elasticsearchProperties) { if (esRestClient == null) { throw new NullPointerException("RestHighLevelClient init Error"); } return new ElasticsearchUtilTemplate(esRestClient,elasticsearchProperties.getSearchRequestTimeOut()); } }
3、自動裝配配置
裝配要生效需要ElasticsearchUtilAutoConfiguration加入springboot的自動裝配文件spring.factories
這是spring配置的特定目錄文件,自己新建一個,名字和目錄要一致
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.demo.sdk.elasticsearch.config.ElasticsearchUtilAutoConfiguration
4、定義一個模板工具類
import cn.hutool.json.JSONUtil; import com.github.pagehelper.Page; import com.github.pagehelper.page.PageMethod; import com.demo.sdk.elasticsearch.exception.ElasticsearchErrorException; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; 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.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.support.master.AcknowledgedResponse; 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.client.core.CountRequest; import org.elasticsearch.client.core.CountResponse; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * ES搜索引擎模板 * @author ppp * @date 2023/3/21 */ public class ElasticsearchUtilTemplate { private final RestHighLevelClient esRestClient; private final long searchRequestTimeOut; public ElasticsearchUtilTemplate(RestHighLevelClient esRestClient, long searchRequestTimeOut) { this.searchRequestTimeOut = searchRequestTimeOut; this.esRestClient = esRestClient; } /** * 列表查詢 * * @param searchSourceBuilder SearchSourceBuilder * @param clazz 返回結果class對象 * @param indices ES索引 * @return java.util.List 對象列表 */ public <T> List<T> listSearch(SearchSourceBuilder searchSourceBuilder, Class<T> clazz, String... indices) { Page<T> resultPage = PageMethod.getLocalPage(); boolean isResultPage = resultPage != null; if (isResultPage) { PageMethod.clearPage(); searchSourceBuilder.from((int) resultPage.getStartRow()); searchSourceBuilder.size(resultPage.getPageSize()); } if (isResultPage && resultPage.isCount()) { resultPage.setTotal(count(searchSourceBuilder, indices)); } SearchResponse searchResponse = search(searchSourceBuilder, indices); List<T> resultList = formatSearchResult(searchResponse, clazz); if (isResultPage && resultPage.isCount()) { resultPage.addAll(resultList); return resultPage; } return resultList; } public SearchResponse search(SearchSourceBuilder searchSourceBuilder, String... indices) { SearchRequest searchRequest = new SearchRequest(indices); searchSourceBuilder.timeout(TimeValue.timeValueMillis(searchRequestTimeOut)); searchRequest.source(searchSourceBuilder); return search(searchRequest); } public SearchResponse search(SearchRequest searchRequest) { try { return esRestClient.search(searchRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 統(tǒng)計數(shù)量 * * @param searchSourceBuilder SearchSourceBuilder * @param indices ES索引 * @return CountResponse */ public long count(SearchSourceBuilder searchSourceBuilder, String... indices) { CountRequest countRequest = new CountRequest(indices); searchSourceBuilder.timeout(TimeValue.timeValueMillis(searchRequestTimeOut)); countRequest.query(searchSourceBuilder.query()); return count(countRequest, RequestOptions.DEFAULT).getCount(); } public CountResponse count(CountRequest countRequest, RequestOptions options) { try { return esRestClient.count(countRequest, options); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 創(chuàng)建索引 * * @param createIndexRequest CreateIndexRequest * @return CreateIndexResponse */ public CreateIndexResponse createIndices(CreateIndexRequest createIndexRequest) { try { return esRestClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 刪除索引(謹慎操作,索引下所有數(shù)據(jù)都將清空) * * @param index 索引名稱 * @return AcknowledgedResponse */ public AcknowledgedResponse deleteIndex(String index) { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(); deleteIndexRequest.indices(index); return deleteIndices(deleteIndexRequest); } /** * 刪除索引(謹慎操作,索引下所有數(shù)據(jù)都將清空) * * @param deleteIndexRequest DeleteIndexRequest * @return AcknowledgedResponse */ public AcknowledgedResponse deleteIndices(DeleteIndexRequest deleteIndexRequest) { try { return esRestClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 插入數(shù)據(jù) * * @param index 索引 * @param id 唯一id * @param source 插入對象 * @return IndexResponse */ public IndexResponse add(String index, Object id, Object source) { IndexRequest indexRequest = new IndexRequest(index).id(String.valueOf(id)); indexRequest.source(JSONUtil.toJsonStr(source), XContentType.JSON); try { return esRestClient.index(indexRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 批量插入數(shù)據(jù) * 建議:數(shù)量控制在5000以內 * * @param index 索引 * @param sourceMap 數(shù)據(jù)<唯一id,對象> * @return BulkResponse */ public BulkResponse addBulk(String index, Map<String, Object> sourceMap) { BulkRequest request = new BulkRequest(); sourceMap.forEach((id, source) -> { request.add(new IndexRequest(index).id(id).source(JSONUtil.toJsonStr(source), XContentType.JSON)); }); try { return esRestClient.bulk(request, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 獲取數(shù)據(jù) * * @param getRequest GetRequest * @return GetResponse */ public GetResponse get(GetRequest getRequest) { try { return esRestClient.get(getRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 更新數(shù)據(jù) * * @param updateRequest UpdateRequest * @return UpdateResponse */ public UpdateResponse update(UpdateRequest updateRequest) { try { return esRestClient.update(updateRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 刪除數(shù)據(jù) * * @param deleteRequest DeleteRequest * @return DeleteResponse */ public DeleteResponse delete(DeleteRequest deleteRequest) { try { return esRestClient.delete(deleteRequest, RequestOptions.DEFAULT); } catch (Exception e) { throw new ElasticsearchErrorException(e.getMessage(), e.getCause()); } } /** * 格式化搜索結果 * * @param searchResponse 搜索結果 * @param clazz 返回對象 * @return java.util.List */ public <T> List<T> formatSearchResult(SearchResponse searchResponse, Class<T> clazz) { SearchHit[] searchHits = searchResponse.getHits().getHits(); List<T> resultList = new ArrayList<T>(); for (SearchHit searchHit : searchHits) { resultList.add(JSONUtil.toBean(searchHit.getSourceAsString(), clazz)); } return resultList; } }
5、定義一個異常
/** * 錯誤異常 * @author ppp * @date 2023/3/21 */ public class ElasticsearchErrorException extends RuntimeException { public ElasticsearchErrorException(String message) { super(message); } public ElasticsearchErrorException(String message, Throwable cause) { super(message, cause); } }
6、application.yml配置
# elasticsearch elasticsearch.config: host: 127.0.0.1 port: 9200 username: admin password: admin123 connect-time-out: 1000 socket-time-out: 30000 connection-request-time-out: 500 search-request-time-out: 5000 max-connect-num: 100 max-connect-per-route: 100
7、測試
class DemoApplicationTests { /** * 獲取ElasticsearchUtilTemplate模板 */ @Autowired private ElasticsearchUtilTemplate elasticsearchTemplate; /** * 分頁查詢 */ @Test void getListPage() { // 分頁 PageHelper.startPage(1, 10); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); // 篩選 filter比must要好一點,filter會對條件進行緩存,這有助于提高查詢性能,特別是對于頻繁使用的過濾條件 boolQueryBuilder.filter(QueryBuilders.termQuery("ip", "192.168.0.1")); // 篩選多個值 類似mysql的in效果 termsQuery和termQuery區(qū)別 boolQueryBuilder.filter(QueryBuilders.termsQuery("name", Arrays.asList("張三","李四")); // 模糊 boolQueryBuilder.must(QueryBuilders.wildcardQuery("ext1", "*測試*")); searchSourceBuilder.query(boolQueryBuilder); // 排序 searchSourceBuilder.sort("createTime", SortOrder.DESC); List<StudyLogIndex> logIndexList = this.elasticsearchTemplate.listSearch(searchSourceBuilder, StudyLogIndex.class, index); PageInfo<StudyLogIndex> studyLogIndexPageInfo = new PageInfo<>(logIndexList); System.out.println(JSONUtil.toJsonStr(studyLogIndexPageInfo)); } /** * 統(tǒng)計 */ @Test void countTest() { // 分頁 PageHelper.startPage(1, 10); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); // 篩選 boolQueryBuilder.filter(QueryBuilders.termQuery("ip", "127.0.0.1")); // 模糊 boolQueryBuilder.must(QueryBuilders.wildcardQuery("ext2", "*用*")); searchSourceBuilder.query(boolQueryBuilder); CountResponse count = elasticsearchTemplate.count(searchSourceBuilder, index); System.out.println("統(tǒng)計總數(shù):"+ count.getCount()); } /** * 刪除索引 */ @Test void deleteIndicesTest() { elasticsearchTemplate.deleteIndex(index); } /** * 創(chuàng)建es索引 */ @Test void createIndicesTest() { CreateIndexRequest createIndexRequest = new CreateIndexRequest(index); createIndexRequest.mapping("{\n" + " \"properties\": {\n" + " \"globalId\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"site\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"tag\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"uid\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"classId\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"courseId\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"videoId\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"startTime\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"time\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"ip\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"start\": {\n" + " \"type\": \"integer\"\n" + " },\n" + " \"end\": {\n" + " \"type\": \"integer\"\n" + " },\n" + " \"createTime\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"ext1\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"ext2\": {\n" + " \"type\": \"keyword\"\n" + " }\n" + " }\n" + "}", XContentType.JSON); elasticsearchTemplate.createIndices(createIndexRequest); } /** * 插入es數(shù)據(jù) */ @Test void addDataTest() { for (int i = 0; i < 10; i++) { StudyLogIndex studyLogIndex = new StudyLogIndex(); studyLogIndex.setGlobalId("CX"+i); studyLogIndex.setSite("CX"); studyLogIndex.setTag("SUCCESS"); studyLogIndex.setUid(12000000L+i); studyLogIndex.setClassId(123456L); studyLogIndex.setCourseId(123456L); studyLogIndex.setVideoId(123456L); studyLogIndex.setStartTime(123456L); studyLogIndex.setTime(123456L); studyLogIndex.setIp("127.0.0.1"); studyLogIndex.setStart(0); studyLogIndex.setEnd(0); studyLogIndex.setCreateTime(0L); studyLogIndex.setExt1("測試es工具"); studyLogIndex.setExt2("備用"); elasticsearchTemplate.add(index, studyLogIndex.getGlobalId(), studyLogIndex); } } /** * 批量插入es */ @Test void bulkAddDataTest() { Map<String, Object> map = new HashMap<>(16); for (int i = 100; i < 1000; i++) { StudyLogIndex studyLogIndex = new StudyLogIndex(); studyLogIndex.setGlobalId(IdUtil.getSnowflakeNextIdStr()); studyLogIndex.setSite("ZJ"); studyLogIndex.setTag("SUCCESS"); studyLogIndex.setUid(12000000L+i); studyLogIndex.setClassId(123456L); studyLogIndex.setCourseId(123456L); studyLogIndex.setVideoId(123456L); studyLogIndex.setStartTime(123456L); studyLogIndex.setTime(123456L); studyLogIndex.setIp("192.168.0.3"); studyLogIndex.setStart(0); studyLogIndex.setEnd(0); studyLogIndex.setCreateTime(0L); studyLogIndex.setExt1("批量測試es工具"); studyLogIndex.setExt2("備用"); map.put(studyLogIndex.getGlobalId(), studyLogIndex); } elasticsearchTemplate.addBulk(index, map); } /** * 聚合檢索 */ @Test void aggregateTest() { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 聚合檢索不設置大小會默認只返回10個統(tǒng)計結果 TermsAggregationBuilder field = AggregationBuilders.terms("group_by_ip").field("ip").size(1000); searchSourceBuilder.aggregation(field).size(10); SearchResponse search = elasticsearchTemplate.search(searchSourceBuilder, index); System.out.println(JSONUtil.toJsonStr(search)); Map<String, Long> countMap = AggregationsUtil.getCountMap(search.getAggregations()); System.out.println(JSONUtil.toJsonStr(countMap)); } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring MessageSource獲取消息不符合預期的問題解決方案
最近我參與的產(chǎn)品要做國際化支持,選擇了用Spring MessageSource來實現(xiàn),這個Spring 框架提供的工具使用很簡單,網(wǎng)上有各種教程文章,這里不做贅述,只說一個實際遇到的問題及解決方案,需要的朋友可以參考下2024-01-01教你如何用Eclipse創(chuàng)建一個Maven項目
這篇文章主要介紹了教你如何用Eclipse創(chuàng)建一個Maven項目,文中有非常詳細的代碼示例,對正在入門Java的小伙伴們是非常有幫助的喲,需要的朋友可以參考下2021-05-05使用springboot防止反編譯proguard+xjar
介紹了三種代碼混淆和加密工具的使用方法:ProGuard、Xjar和ClassFinal,ProGuard用于混淆Java字節(jié)碼,Xjar提供對JAR包內資源的加密和動態(tài)解密,而ClassFinal則支持直接加密JAR包或WAR包,通過預研和實際操作2024-11-11SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解
這篇文章主要想和大家聊聊kaptcha的用法,畢竟這個已經(jīng)有16年歷史的玩意還在有人用,說明它的功能還是相當強大的,感興趣的小伙伴可以了解一下2022-05-05Maven將代碼及依賴打成一個Jar包的方式詳解(最新推薦)
這篇文章主要介紹了Maven將代碼及依賴打成一個Jar包的方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05