Elasticsearch--06 javaAPI基础测试

Elasticsearch版本:6.2.4

引入依赖:

        <dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>transport</artifactId>
			<version>6.2.4</version>
		</dependency>

 

/**
	 * es 简单查询数据
	 * 
	 * @throws UnknownHostException
	 */
	@SuppressWarnings("resource")
	@Test
	public void test01() throws UnknownHostException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();

		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));

		// 查询数据
		// GetResponse response = client.prepareGet("es1", "user","lisi").execute().actionGet();
		GetResponse response = client.prepareGet("es1", "user","lisi").get();

		// 获取查到的数据
		String source = response.getSourceAsString();
		System.out.println(source);

		client.close();
	}

	/**
	 * es 简单插入数据
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	@Test
	public void test02() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();

		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));

		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("name", "zhangsan22");
		// map.put("age", 122);
		// map.put("addr", "shenzhen22");

		XContentBuilder doc1 = XContentFactory.jsonBuilder()
				.startObject()
				.field("name", "lisi")
				.field("age", 25)
				.field("addr", "hubei")
				.endObject();

		// 可以指定ID , 如果不指定ID , 插入后会随机生成一个ID , setSource 设置map 或者使用  XContentBuilder
		// IndexResponse indexResponse = client.prepareIndex("es1","user").setSource(map).get();
		// IndexResponse indexResponse = client.prepareIndex("es1","user").setId("1").setSource(map).get();
		IndexResponse indexResponse = client.prepareIndex("es1", "user").setId("lisi").setSource(doc1).get();

		// IndexResponse indexResponse = client.prepareIndex("es1", "user").setSource(map).get();
		RestStatus status = indexResponse.status();// CREATED  OK
		System.out.println(status);

		client.close();
	}
	/**
	 * es 简单删除数据
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	@Test
	public void test03() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		
		DeleteResponse deleteResponse = client.prepareDelete("es1", "user","lisi1").get();
		
		RestStatus status = deleteResponse.status();//  OK
		System.out.println(status);
		
		client.close();
	}
	/**
	 * es 简单更新数据
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	@Test
	public void test04() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		
		// 修改哪个属性就加上
		// XContentBuilder doc1 = XContentFactory.jsonBuilder()
		// 		.startObject()
		// 		.field("name", "lisi")
		// 		.field("age", 55)
		// 		.field("addr", "hubei")
		// 		.endObject();
		// UpdateResponse updateResponse = client.prepareUpdate("es1","user","lisi").setDoc(doc1).get();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("age", 122);
		
		// setDoc 可以用map, 也可以用XContentBuilder
		UpdateResponse updateResponse = client.prepareUpdate("es1", "user","lisi").setDoc(map).get();
		
		RestStatus status = updateResponse.status();//  OK
		System.out.println(status);
		
		client.close();
	}
	
	
	/**
	 * es 批量查询
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	@Test
	public void test05() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		
		MultiGetResponse multiGetResponse = client.prepareMultiGet()
			.add("es1","user","1","lisi")//可以追加多个不同index
			// .add("text","user","1","lisi")//可以追加多个不同index
			.get();
		
		for(MultiGetItemResponse item : multiGetResponse) {
			GetResponse response = item.getResponse();
			if(response != null && response.isExists()) {
				System.out.println(response.getSourceAsString());
			}
		}
		
		client.close();
	}
	/**
	 * es 批量操作 Bulk
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings({ "resource", "serial" })
	@Test
	public void test06() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		
		//BulkRequestBuilder 可用于批量添加、批量删除、批量更新
		BulkRequestBuilder prepareBulk = client.prepareBulk();
		
		prepareBulk.add(client.prepareIndex("es1", "user", "wangwu3").setSource(new HashMap<String,Object>(){
								{
									put("name","wangwu1,zhangsan") ;
									put("age",22) ;
									put("addr","beij") ;
								}})) ;
		prepareBulk.add(client.prepareIndex("es1", "user", "wangwu4").setSource(new HashMap<String,Object>(){
			{
				put("name","wangwu2") ;
				put("age",25) ;
				put("addr","beijing") ;
			}})) ;
		
		BulkResponse bulkResponse = prepareBulk.get();
		System.out.println(bulkResponse.status());
		
		if(bulkResponse.hasFailures()) {
			System.out.println("有插入失败 !。。。");
		}
		
		client.close();
	}
	/**
	 * es 查询删除
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings({ "resource", "serial" })
	@Test
	public void test07() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		
		// 删除  name中值为“zhangsan”的数据
		BulkByScrollResponse bulkByScrollResponse = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
					.filter(QueryBuilders.matchQuery("name", "zhangsan"))
					.source("es1").get();
		
		// 删除的条数
		long deletedCount = bulkByScrollResponse.getDeleted();
		
		System.out.println(deletedCount);
		
		
		client.close();
	}
	
	/**
	 * es 条件查询(完全匹配查询)
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings({ "resource", "serial" })
	@Test
	public void test08() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		/*
		 {"name":"wangwu1,zhangsan","addr":"beij","age":22}   	可以匹配到(插入的时候用“,”隔开即可)
		 {"name":"zhangsan","addr":"beij","age":22} 			可以匹配到
		 {"name":"zhangsan1","addr":"beij","age":22}			匹配不到
		*/
		
		SearchResponse searchResponse = client.prepareSearch("es1")
					.setQuery(QueryBuilders.matchQuery("name", "zhangsan"))
					.get();
		
		SearchHits hits = searchResponse.getHits();
		System.out.println(hits.totalHits);
		for (SearchHit searchHit : hits) {
			System.out.println(searchHit.getSourceAsString());
		}
		
		client.close();
	}
	
	/**
	 * es 条件查询(完全匹配查询)
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings({ "resource", "serial" })
	@Test
	public void test09() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "es-cluster").build();
		
		// 创建访问es服务器的客户端
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.8.151"), 9300));
		/*
		 {"name":"wangwu1,zhangsan","addr":"beij","age":22}   	可以匹配到(插入的时候用“,”隔开即可)
		 {"name":"zhangsan","addr":"beij","age":22} 			可以匹配到
		 {"name":"zhangsan1","addr":"beij","age":22}			匹配不到
		 */
		
		SearchResponse searchResponse = client.prepareSearch("es1")
				.setQuery(QueryBuilders.matchQuery("name", "zhangsan"))
				.get();
		
		SearchHits hits = searchResponse.getHits();
		System.out.println(hits.totalHits);
		for (SearchHit searchHit : hits) {
			System.out.println(searchHit.getSourceAsString());
		}
		
		client.close();
	}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值