ElasticSearch封装增删改查 java rest client

最近在学习ElasticSearch,我对他的理解就像是一个数据库,既然是数据库就会存在增删改查这种常用的操作,基于ElasticSearch封装了增删改查的常用操作。

一、增加索引

索引给我的理解就是我们数据库中的表,那么增加索引相当于创建一个表,直接上代码

public static void inserIndex(String index) throws Exception {
		Environment.elasticsearchClient.indices().create(c -> c.index(index));
	}

二、删除索引

public static void deleteIndex(String index) throws Exception {
		Environment.elasticsearchClient.indices().delete(c -> c.index(index));
	}

三、批量增加数据

public static void inserData(String index, List<Map> datas) throws Exception {
		List<BulkOperation> bulkOperations = new ArrayList<>();
		for (Map data : datas) {
			bulkOperations.add(new BulkOperation.Builder().create(d -> d.document(data).index(index)).build());
		}
		BulkResponse response = Environment.elasticsearchClient.bulk(e -> e.index(index).operations(bulkOperations));
		if (response.errors() == true) {
			throw new Exception("保存数据错误");
		}
	}

四、查询数据

public static List<Map> queryData(String index,Map<String, Object> params, Page page) throws Exception {
		List<Map> datas = new ArrayList<Map>();
		BoolQuery.Builder bb = new BoolQuery.Builder();
		Set<String> set = params.keySet();
		for (String key : set) {
			WildcardQuery.Builder wb = new WildcardQuery.Builder();
			wb.field(key).value("*" + params.get(key) + "*");
			bb.must(wb.build()._toQuery());
		}

		SearchRequest.Builder srb = new SearchRequest.Builder();
		srb.index(index);
		srb.query(bb.build()._toQuery());
		if (page != null) {
			List<SortOptions> soList = new ArrayList<SortOptions>();
			SortOptions.Builder sob = new SortOptions.Builder();
			if (page.getOrder().equals("asc")) {
				sob.field(FieldSort.of(sort -> sort.field(page.getSort()).order(SortOrder.Asc)));
			} else {
				sob.field(FieldSort.of(sort -> sort.field(page.getSort()).order(SortOrder.Desc)));
			}
			soList.add(sob.build());
			srb.sort(soList);
		}
		srb.from((page.getPage() - 1) * page.getRows());
		srb.size(page.getRows());
		SearchResponse<Map> s_res = Environment.elasticsearchClient.search(srb.build(), Map.class);
		List<Hit<Map>> hms = s_res.hits().hits();
		for (Hit<Map> hm : hms) {
			Map data = hm.source();
			datas.add(data);
		}
		return datas;
	}

五、删除数据

public void deleteData(List<String> ids,String index) throws ElasticsearchException, IOException {
		for(String id : ids) {
			DeleteByQueryRequest.Builder drb = new DeleteByQueryRequest.Builder();
			drb.index(index);
			TermQuery.Builder tqb = new TermQuery.Builder();
			tqb.field("id").value(id);
			drb.query(tqb.build()._toQuery());
			Environment.elasticsearchClient.deleteByQuery(drb.build());
		}
	}

六、更新数据

public void updateData(String index,String id,Map obj) throws ElasticsearchException, IOException {
		SearchRequest.Builder srb = new SearchRequest.Builder();
		srb.index(index);
		TermQuery.Builder tqb = new TermQuery.Builder();
		tqb.field("id").value(id);
		srb.query(tqb.build()._toQuery());
		SearchResponse<Map> s_res = Environment.elasticsearchClient.search(srb.build(), Map.class);
		List<Hit<Map>> hms = s_res.hits().hits();
		for (Hit<Map> hm : hms) {
			UpdateRequest.Builder urb = new UpdateRequest.Builder();
			urb.doc(obj);
			urb.id(hm.id());
			urb.index(hm.index());
			Environment.elasticsearchClient.update(urb.build(), Map.class);
		}
	}

七、client获取

之前方法里的client获取方式

RestClient restClient = RestClient.builder(
			        new HttpHost("localhost", 9201)).build();
			ElasticsearchTransport transport = new RestClientTransport(
			        restClient, new JacksonJsonpMapper());
			Environment.elasticsearchClient = new ElasticsearchClient(transport);

八、maven引入


	      co.elastic.clients
	      elasticsearch-java
	      8.1.2
	    
	    
	      jakarta.json
	      jakarta.json-api
	      2.0.1
	    

以上就是用ElasticSearch封装的常用的增删改查操作,平时做项目应该是够用的了

你可能感兴趣的:(java,elasticsearch)