ElasticSearch的常用增删改查DSL和代码

news2024/12/25 16:14:51

es增删改查常用语法

我们日常开发中,操作数据库写sql倒是不可能忘记,但是操作es的dsl语句有时候很容易忘记,特地记录一下方便查找。

DSL语句

1、创建索引

-- 创建索引
PUT /my_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "description": {
        "type": "text"
      },
      "timestamp": {
        "type": "date"
      }
    }
  }
}

2、插入文档

-- 插入文档
POST /my_index/_doc/1
{
  "title": "Sample Document",
  "description": "This is a sample document for Elasticsearch",
  "timestamp": "2022-01-01"
}

3、更新文档

-- 更新文档
POST /my_index/_update/主键值
{
  "doc": {
    "description": "Updated description"
  }
}

4、删除文档(单独、多个、全部)

-- 删除单条文档
DELETE /my_index/_doc/

或者
-- 删除单条文档  
POST 索引名/_delete_by_query
{
  "query":{
    "term":{
      "_id":4043
    }
  }
}

-- 删除索引中的所有数据
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、设置切片和副本数量

-- 设置切片和副本数量
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"
        }
      }
    }
  }
}

-- 分页排序查询  	不带其他条件

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、统计

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;
	    /**
	     * 最大连接数
	     */
	    private int maxConnectNum;
	    /**
	     * 最大路由连接数
	     */
	    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连接数配置
        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());
	        }
	    }
	
	    /**
	     * 统计数量
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 创建索引
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 删除索引(谨慎操作,索引下所有数据都将清空)
	     *
	     * @param index 索引名称
	     * @return AcknowledgedResponse
	     */
	    public AcknowledgedResponse deleteIndex(String index) {
	        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest();
	        deleteIndexRequest.indices(index);
	        return deleteIndices(deleteIndexRequest);
	    }
	
	    /**
	     * 删除索引(谨慎操作,索引下所有数据都将清空)
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 插入数据
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 批量插入数据
	     * 建议:数量控制在5000以内
	     *
	     * @param index     索引
	     * @param sourceMap 数据<唯一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());
	        }
	    }
	
	
	    /**
	     * 获取数据
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 更新数据
	     *
	     * @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());
	        }
	    }
	
	    /**
	     * 删除数据
	     *
	     * @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();
	// 筛选
	boolQueryBuilder.filter(QueryBuilders.termQuery("ip", "192.168.0.1"));
	// 筛选多个值  类似mysq  termsQuery和termQuery区别
	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));
}

/**
 * 统计
 */
@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("统计总数:"+ count.getCount());
}

/**
 * 删除索引
 */
@Test
void deleteIndicesTest() {
	elasticsearchTemplate.deleteIndex(index);
}

/**
 * 创建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数据
 */
@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个统计结果
	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));
}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1395355.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构:链式栈

stack.h /* * 文件名称&#xff1a;stack.h * 创 建 者&#xff1a;cxy * 创建日期&#xff1a;2024年01月18日 * 描 述&#xff1a; */ #ifndef _STACK_H #define _STACK_H#include <stdio.h> #include <stdlib.h>typedef struct stack{int data…

查找局域网树莓派raspberry的mac地址和ip

依赖python库&#xff1a; pip install socket pip install scapy运行代码&#xff1a; import socket from scapy.layers.l2 import ARP, Ether, srpdef get_hostname(ip_address):try:return socket.gethostbyaddr(ip_address)[0]except socket.herror:# 未能解析主机名ret…

Leetcode2207. 字符串中最多数目的子字符串

Every day a Leetcode 题目来源&#xff1a;2207. 字符串中最多数目的子字符串 解法1&#xff1a;贪心 一次遍历 设 pattern 的第一个字符为 x&#xff0c;第二个字符为 y。 根据题意&#xff0c;x 插入的位置越靠左&#xff0c;答案的个数越多&#xff1b;y 插入的位置越…

大数据StarRocks(八):集群扩缩容

一、FE扩缩容 StarRocks FE 节点分为 Follower 节点和 Observer 节点。Follower 节点参与选举投票和写入&#xff0c;Observer 节点只用来同步日志&#xff0c;扩展读性能。 注意&#xff1a; 所有 FE 节点的 http_port 必须相同。Follower FE 节点&#xff08;包括 Leader …

大数据工作岗位分析

前言&#xff1a;随着大数据需求的增多&#xff0c;许多中小公司和团队也新增或扩展了大数据工作岗位&#xff1b;但是却对大数据要做什么和能做什么&#xff0c;没有深入的认识&#xff1b;往往是招了大数据岗位&#xff0c;搭建起基础能力后&#xff0c;就一直处于重复开发和…

K8S Informer机制原理解读 | 架构设计

在Kubernetes系统中&#xff0c;组件之间通过HTTP协议进行通信&#xff0c;在不依赖任何中间件的情况下需要保证消息的实时性、可靠性、顺序性等。那么Kubernetes是如何做到的呢&#xff1f;答案就是Informer机制。Kubernetes的其他组件都是通过client-go的Informer机制与Kuber…

Git入门详细教程

一、Git概述&#x1f387; Git官网 Git是一个开源的分布式版本控制系统&#xff0c;用于跟踪文件的变化和协作开发。它允许多个开发者在同一项目中共同工作&#xff0c;并能够有效地管理代码的版本和历史记录。Git可以帮助开发团队更好地协作&#xff0c;追踪代码变更&#xf…

Linux shell编程学习笔记40:stat命令

程序员必备的面试技巧 “程序员必备的面试技巧&#xff0c;就像是编写一段完美的代码一样重要。在面试战场上&#xff0c;我们需要像忍者一样灵活&#xff0c;像侦探一样聪明&#xff0c;还要像无敌铁金刚一样坚定。只有掌握了这些技巧&#xff0c;我们才能在面试的舞台上闪耀…

深入探究 JavaScript 中的 String:常用方法和属性全解析(下)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

Vision Transformer(VIT)模型介绍

计算机视觉 文章目录 计算机视觉Vision Transformer&#xff08;VIT&#xff09;Patch EmbeddingsHybrid ArchitectureFine-tuning and higher resolutionPyTorch实现Vision Transformer Vision Transformer&#xff08;VIT&#xff09; Vision Transformer&#xff08;ViT&am…

算法笔记 #3

2024年1月18日 Q1&#xff1a;搜索二叉树 A&#xff1a;查找&#xff0c;左子树比根几点小&#xff0c;右子树比根大。 删除&#xff1a;1&#xff09;搜索删除的目标节点&#xff0c;记录其父节点&#xff1b; 2&#xff09;左右孩子都为空&#xff0c;直接删掉&#xff1b;…

【2023地理设计组一等奖】城市业态基因图谱与城市风格感知

作品介绍 1 设计思路 1.1 作品背景 随着城市化进程不断推进,城市的餐饮、购物、休闲和文化业态成为城市发展中的重要组成部分,深刻影响着城市经济社会发展和居民生活,塑造了城市的外在形象和内在风格,形成了各具特色的城市发展特征。因此,在区域和全国尺度上研究城市业态…

TDengine 创始人陶建辉在汽车 CIOCDO 论坛发表演讲,助力车企数字化转型

当前&#xff0c;汽车行业的数字化转型如火如荼。借助数字技术的充分利用&#xff0c;越来越多的车企进一步提升了成本优化、应用敏捷性、高度弹性和效率。这一转型使得业务应用的开发和管理模式发生了颠覆性的创新&#xff0c;赋予了汽车软件快速响应变化和动态调度资源的能力…

深度学习(1)--基础概念

一.计算机视觉(CV) (1).计算机视觉中图像表示为三位数组&#xff0c;其中三维数组中像素的值为0~255&#xff0c;像素的值越低表示该点越暗&#xff0c;像素的值越高表示该点越亮。 (2).图像表示 A*B*C&#xff0c;其中A,B分别为图像的长和宽&#xff0c;C则表示图像的颜色通道…

Spring Boot 单体应用升级 Spring Cloud 微服务

作者&#xff1a;刘军 Spring Cloud 是在 Spring Boot 之上构建的一套微服务生态体系&#xff0c;包括服务发现、配置中心、限流降级、分布式事务、异步消息等&#xff0c;因此通过增加依赖、注解等简单的四步即可完成 Spring Boot 应用到 Spring Cloud 升级。 *Spring Cloud …

vim 编辑器如何同时注释多行以及将多行进行空格

一、场景 YAML文件对空格的要求非常严格&#xff0c;因此在修改YAML时&#xff0c;我们可能需要批量添加空格。 二、操作步骤 请注意&#xff1a;您的所有操作都将以第一行为基准。也就是说&#xff0c;第一行有多少个空格&#xff0c;下面的行就会模仿添加相同数量的空格。…

一款开源且不限制大小可以设置过期时间的支持分享的的开源文件共享系统picoshare 部署教程

1.拉取镜像 2.部署 创建目录 mkdir -p /opt/picoshare/data 部署 其中:"somesecretpass"是密码 docker run \--env "PORT4001" \--env "PS_SHARED_SECRETsomesecretpass" \--publish 10005:4001/tcp \--volume "/opt/picoshare/data:…

[Android] Android架构体系(2)

文章目录 Bionic精简对系统调用的支持:不支持 System V IPC:有限的 Pthread 功能:有限支持C:不再支持本地化和/或宽字符:Bionic新增的特性系统属性硬编码写死的UID/GID内置了DNS解析硬编码写死的服务和协议 硬件抽象层Linux内核匿名共享内存(ASHMem)Binder-BinderLoggerION 内存…

找不到满意的机器视觉工程师工作,想找到满意的工作很难,人太多了,只能先就业后择业

找不到满意的机器视觉工程师工作&#xff0c;想找到满意的工作很难&#xff0c;人太多了&#xff0c;只能先就业后择业

ICCV2023 | PTUnifier+:通过Soft Prompts(软提示)统一医学视觉语言预训练

论文标题&#xff1a;Towards Unifying Medical Vision-and-Language Pre-training via Soft Prompts 代码&#xff1a;https://github.com/zhjohnchan/ptunifier Fusion-encoder type和Dual-encoder type。前者在多模态任务中具有优势&#xff0c;因为模态之间有充分的相互…