Elasticsearch7用java代码实现CRUD方法类

1.引入pom文件依赖

	
		
			org.elasticsearch.client
			elasticsearch-rest-high-level-client
			7.15.0
		
		
			org.elasticsearch
			elasticsearch
			7.15.0
		

		
			org.apache.logging.log4j
			log4j-core
			2.11.0
		
		
			com.alibaba
			fastjson
			1.2.83
		

	

2.代码实现

2.1编写获取连接测试方法

	public static void main(String[] args) throws Exception {
		createIndex();
		writeDoc();
		batchWriteDoc();
		searchFormSize();
		searchAfter();
		searchScroll();
	}

	/**
	 * 获取ES连接客户端
	 * 
	 * @return RestHighLevelClient
	 */
	private static RestHighLevelClient getRestHighLevelClient() {
		HttpHost host = new HttpHost("172.21.xx.xxx", 9200, HttpHost.DEFAULT_SCHEME_NAME);

		RestClientBuilder restClientBuilder = RestClient.builder(host);

		CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
		restClientBuilder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
		RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

		return restHighLevelClient;
	}

2.2测试创建索引

	/**
	 * 创建索引
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void createIndex() throws Exception {
		// 获取连接
		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		XContentBuilder builder = XContentFactory.jsonBuilder();
		builder.startObject();
		{
			builder.startObject("mappings");
			{
				builder.startObject("properties");
				{
					builder.startObject("id");
					{
						builder.field("type", "long");
					}
					builder.endObject();
					builder.startObject("username");
					{
						builder.field("type", "keyword");
					}
					builder.endObject();
					builder.startObject("age");
					{
						builder.field("type", "integer");
					}
					builder.endObject();

				}
				builder.endObject();
			}
			builder.endObject();
		}
		builder.endObject();

		CreateIndexRequest createIndexRequest = new CreateIndexRequest("test_limit_index");
		createIndexRequest.source(builder);

		restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
		restHighLevelClient.close();

	}

2.3测试写入文档

	/**
	 * 写入doc
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void writeDoc() throws Exception {

		// 获取连接
		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		for (int i = 0; i < 20000; i++) {

			IndexRequest indexRequest = new IndexRequest();

			indexRequest.index("test_limit_index").id(String.valueOf(i));

			UserInfo userInfo = new UserInfo();
			userInfo.setId(i);
			userInfo.setUsername("黄xx" + i);
			userInfo.setAge(18);
			// 再将我们的json数据放进request中作为数据源
			indexRequest.source(JSON.toJSONString(userInfo), XContentType.JSON);
			IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

			// 查看结果
			DocWriteResponse.Result result = response.getResult();
			if (Result.CREATED.equals(result)) {
				System.out.println("创建文档" + i + "成功");
			}

		}

		// 关闭客户端
		restHighLevelClient.close();

	}

2.4测试批量写入

	/**
	 * 批量写入
	 * 
	 * @throws Exception
	 */
	public static void batchWriteDoc() throws Exception {
		// 获取连接
		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		// 创建批量请求
		BulkRequest request = new BulkRequest();

		// 添加20000个文档到批量请求中
		for (int i = 10001; i <= 20000; i++) {
			IndexRequest indexRequest = new IndexRequest("test_limit_index");
			indexRequest.id(String.valueOf(i));

			UserInfo userInfo = new UserInfo();
			userInfo.setId(i);
			userInfo.setUsername("黄xx" + i);
			userInfo.setAge(18);
			indexRequest.source(JSON.toJSONString(userInfo), XContentType.JSON);

			request.add(indexRequest);
		}

		// 执行批量请求
		BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);

		// 处理批量响应
		for (BulkItemResponse bulkItemResponse : response) {
			if (bulkItemResponse.isFailed()) {
				BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
				System.out.println(bulkItemResponse.getId() + ", Reason: " + failure.getMessage());
			}
		}

		// 关闭客户端
		restHighLevelClient.close();

	}

2.5测试删除文档

	/**
	 * 删除文档
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void deleteDoc() throws Exception {

		// 获取连接
		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		String indexName = "test_limit_index";
		String documentId = "3";

		DeleteRequest request = new DeleteRequest(indexName).id(documentId);

		DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
		System.out.println(response);

		restHighLevelClient.close();

	}

2.6search+form分页

        只能查询前10000条,如果要解除限制,需要设置index.max_result_window的值

	/**
	 * 使用form+size查询
	 * 
	 * @throws Exception
	 */
	public static void searchFormSize() throws Exception {
		// 获取连接
		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		// 创建searchRequest
		SearchRequest searchRequest = new SearchRequest("test_limit_index");

		// 指定查询条件
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		// 必须加上track_total_hits,不然就只显示10000
		sourceBuilder.trackTotalHits(true);

		// 起始行
		sourceBuilder.from(10000);
		// 每页多少条数据
		sourceBuilder.size(10);
		// 设置唯一排序值定位
		sourceBuilder.sort("id", SortOrder.DESC);

		// 将sourceBuilder对象添加到搜索请求中
		searchRequest.source(sourceBuilder);

		// 发送请求
		SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
		SearchHit[] hits = searchResponse.getHits().getHits();
		List> result = new ArrayList<>();
		if (hits != null && hits.length > 0) {
			for (SearchHit hit : hits) {
				// 获取需要数据
				Map sourceAsMap = hit.getSourceAsMap();
				result.add(sourceAsMap);
			}
		}

		// 关闭客户端
		restHighLevelClient.close();
	}

2.7searchAfter分页

        可以查询所有记录数,默认是系统的uuid,不重复,但是需要指定唯一id,再标记从这个id查询后面的数据,如果有重复的自定义主键,则数量可能会变少。

	/**
	 * 继续查询 ,searchAfter方式
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void searchAfter() throws Exception {

		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		SearchRequest searchRequest = new SearchRequest("test_limit_index");
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
		// 设置每页查询的数据个数
		sourceBuilder.size(1000);
		// 设置唯一排序值定位
		sourceBuilder.sort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
		// 将sourceBuilder对象添加到搜索请求中
		searchRequest.source(sourceBuilder);

		// 发送请求
		SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
		SearchHit[] searchHits = searchResponse.getHits().getHits();

		List> result = new ArrayList<>(25000);
		if (searchHits != null && searchHits.length > 0) {
			do {
				for (SearchHit searchHit : searchHits) {
					Map sourceAsMap = searchHit.getSourceAsMap();
					result.add(sourceAsMap);
				}
				// 取得最后得排序值sort,用于记录下次将从这个地方开始取数
				SearchHit[] hits = searchResponse.getHits().getHits();
				Object[] lastNum = hits[hits.length - 1].getSortValues();
				// 设置searchAfter的最后一个排序值
				sourceBuilder.searchAfter(lastNum);
				searchRequest.source(sourceBuilder);
				// 进行下次查询
				searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			} while (searchResponse.getHits().getHits().length != 0);
		}
		// 关闭客户端
		restHighLevelClient.close();

		System.out.println("查询数量是:" + result.size());

	}

2.8Scroll滚动分页

        每次获取scrollId往下查询

	/**
	 * 滚动查询
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void searchScroll() throws Exception {

		RestHighLevelClient restHighLevelClient = getRestHighLevelClient();

		SearchRequest searchRequest = new SearchRequest("test_limit_index");
		searchRequest.scroll(TimeValue.timeValueMinutes(1L));
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(1000);
		searchSourceBuilder.sort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
		searchRequest.source(searchSourceBuilder);

		// 获取返回结果scrollId,source
		SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
		String scrollId = searchResponse.getScrollId();
		SearchHit[] searchHits = searchResponse.getHits().getHits();

		List> result = new ArrayList<>(20000);
		for (SearchHit hit : searchHits) {
			result.add(hit.getSourceAsMap());
		}

		// 不断循环查询
		while (true) {
			// 获取 scrollId 去查询下一页
			SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
			// 指定scrollId的生存时间
			scrollRequest.scroll(TimeValue.timeValueMinutes(1L));
			// 执行查询获取返回结果
			SearchResponse scrollResp = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
			// 判断是否查询到了数据,输出
			SearchHit[] hits = scrollResp.getHits().getHits();
			// 循环输出下一页
			if (hits != null && hits.length > 0) {
				for (SearchHit hit : hits) {
					result.add(hit.getSourceAsMap());
				}
			} else {
				// 判断没有查询到数据,退出循环
				break;
			}
		}

		// 创建ClearScrollRequest
		ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
		// 指定scrollId
		clearScrollRequest.addScrollId(scrollId);
		// 删除scrollId
		ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest,
				RequestOptions.DEFAULT);
		// 输出结果
		boolean succeeded = clearScrollResponse.isSucceeded();
		System.out.println(succeeded);

		// 关闭客户端
		restHighLevelClient.close();

		System.out.println("查询数量是:" + result.size());
	}

你可能感兴趣的:(java,开发语言)