针对 JavaAPI ElasticSearch 操作 ---->第五章节

本文档详细介绍了如何使用Java API进行Elasticsearch的连接、索引创建及CRUD操作,涵盖索引的创建、更新、删除以及文档级别的CRUD,并提供了其他实用案例参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Java 客户端连接和索引创建

/**
 * @author hzq
 * @date 2022年07月14日 14:52
 * JAVA API 操作--ES 连接客户端
 */
@Slf4j
public class EsClinet {
	public static void main(String[] args) throws IOException {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
		esClinent.close();
	}

二、索引的CRUD 操作

/**
 * @author hzq
 * @date 2022年07月14日 19:47
 *
 * 创建索引
 */
public class EsCreateIndex {
	public static void main(String[] args) throws IOException {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
		// 创建索引
		CreateIndexRequest request = new CreateIndexRequest("user");
		CreateIndexResponse response = esClinent.indices().create(request, RequestOptions.DEFAULT);
		// 响应状态
		boolean responseAck = response.isAcknowledged();
		System.out.println("索引操作响应状态为:" + responseAck);
		esClinent.close();
	}
/**
 * @author hzq
 * @date 2022年07月14日 19:49
 *
 * 查询索引
 */
public class EsSearchIndex {
	public static void main(String[] args) throws IOException {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
		//查询索引
		GetIndexRequest request = new GetIndexRequest("user");
		GetIndexResponse getIndexResponse = esClinent.indices().get(request, RequestOptions.DEFAULT);
		//响应状态码
		System.out.println(getIndexResponse.getAliases());
		System.out.println(getIndexResponse.getMappings());
		System.out.println(getIndexResponse.getSettings());
		esClinent.close();
	}
}
/**
 * @author hzq
 * @date 2022年07月14日 19:48
 *
 * 删除索引
 */
public class EsDeleteIndex {
	public static void main(String[] args) throws IOException {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
		DeleteIndexRequest request = new DeleteIndexRequest("user");
		request.timeout("1s");
		AcknowledgedResponse delete= esClinent.indices().delete(request,RequestOptions.DEFAULT);
		System.out.println("删除索引返回的状态码{}"+delete.isAcknowledged());
	}
}

二、文档型的CRUD 操作

/**
 * @author hzq
 * @date 2022年07月14日 22:32
 * <p>
 * 添加 文档型
 *
 * 更新都一样 只需要更改对应的方法即可。updateRequest
 */
@Slf4j
public class EsInsertDoc {
	public static void main(String[] args){
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
		IndexRequest request = null;

		try {
			// 插入数据
			request = new IndexRequest();
			request.index("user").id("1");
			BookEntity bookEntity = new BookEntity();
			bookEntity.setName("ES");
			bookEntity.setMessage("开源搜索框架");
			bookEntity.setType("java");
			String userJson = JSON.toJSONString(bookEntity);
			request.source(userJson, XContentType.JSON);
			log.info("用户喜欢的书籍为:" + userJson);
			IndexResponse response = esClinent.index(request, RequestOptions.DEFAULT);
			response.getResult();
		} catch (Exception e) {
			if (request.id("1")==null){
				log.info("插入用户喜欢的书籍异常",e);
			}
			log.error(e.getMessage());
		} finally {
			if (esClinent !=null){
				try {
					esClinent.close();
				} catch (IOException e) {
					log.error("关闭流异常"+e.getMessage());
				}
			}
		}
	}
}
/**
 * @author hzq
 * @date 2022年07月15日 13:12
 *
 *  查询 文档型
 *
 *
 */
public class EsQueryDoc {
	public static void main(String[] args) throws Exception {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);

		// 查询数据
		GetRequest request = new GetRequest();

		request.index("user").id("1");
		GetResponse response = esClinent.get(request, RequestOptions.DEFAULT);
		System.out.println(response.getSourceAsString());
		esClinent.close();

	}
}
/**
 * @author hzq
 * @date 2022年07月15日 13:21
 */
public class EsDeleteDoc {
	public static void main(String[] args) throws Exception {
		RestHighLevelClient esClinent = new RestHighLevelClient(
				RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
		);
			// 删除数据
			DeleteRequest request = new DeleteRequest();
			request.index("user").id("1");
			DeleteResponse response = esClinent.delete(request, RequestOptions.DEFAULT);
			System.out.println("删除用户文档型数据:"+response.toString());
			esClinent.close();
	}
}

四、其他案例参考

https://blog.51cto.com/u_15069450/4529440

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值