elasticsearch-java api之文档(document)各种操作

使用java api和es交互时,可以是json字符串、map对象;es中内置Jackson json序列化机制,可以将自定义对象转成string或者byte,然后传给es-java api使用。

1、添加document:

public static boolean add(String indexName,String indexType,Map<String,Object> data){  
		IndexResponse actionGet = transportClient
				.prepareIndex(indexName, indexType)
				.setSource(data)
				//.setId("1") //自己设置了id,也可以使用ES自带的,但是看文档说,ES的会因为删除id发生变动。
				.execute().actionGet();
		return actionGet.isCreated();
}
注:如果设置了id,那么id存在的则直接更新(所有字段覆盖更新)

2、更新document:

1)使用updateRequest:

public static void update(String indexName,String indexType,String id,Map<String,Object> data){
		UpdateRequest updateRequest = new UpdateRequest();
		updateRequest.index(indexName)
				.type(indexType)
				.id(id)
				.doc(data);
		
		transportClient.update(updateRequest).actionGet();
		
		//或者使用下面方式(效果一样)
		/*XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();  
		
		UpdateRequest updateRequest = new UpdateRequest();
		updateRequest.index(indexName)
		.type(indexType)
		.id(id)
		.doc(jsonBuilder
				.startObject()
					.field("title").value("XContentBuilder")
				.endObject());
		
		transportClient.update(updateRequest).actionGet();*/
	}
说明:

A、可以更新局部字段(map里有多少字段更新多少,也可以增加map里面新字段);

B、当id不存在时,不会自动插入,直接报错

2)使用prepareUpdate:

public static void update1(String indexName,String indexType,String id,Map<String,Object> data){
		
		UpdateRequestBuilder prepareUpdate = transportClient.prepareUpdate(indexName,indexType,id);
		prepareUpdate
			.setDoc(data)
			//.setId("")
			//.setScript("")
			.get();
		
		//或者
		/*try {
			XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();
			UpdateRequestBuilder prepareUpdate1 = transportClient.prepareUpdate(indexName,indexType,id);
			prepareUpdate1
				.setDoc(jsonBuilder
						.startObject()
							.field("title").value("XContentBuilder")
						.endObject())
				.get();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
	}

3、删除document:

public static void delDocument(String indexName,String indexType,String id){  
		DeleteResponse deleteResponse = transportClient
				.prepareDelete(indexName, indexType, id)
				.execute().actionGet();
		deleteResponse.isFound();
	}


4、bulk操作:

bulk是一个批量操作,可以一次请求完成document的增删改。

public static boolean bulk(String indexName,String indexType,List<Map<String,Object>> datas){
		BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
		
		bulkRequestBuilder.add(transportClient.prepareIndex(indexName, indexType).setSource(datas.get(0)));
		bulkRequestBuilder.add(transportClient.prepareDelete(indexName, indexType, "id"));
		bulkRequestBuilder.add(transportClient.prepareUpdate(indexName, indexType, "id").setDoc(datas.get(1)));
		BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
		
		return bulkResponse.hasFailures();
    }


6、get 操作:

可以通过索引名、类型名、文档id一次得到一个文档集合,文档可以来自同一个索引库,也可以来自不同索引库

1)get操作:

public static Map<String, Object> get(String indexName,String indexType,String id){  
		GetResponse getResponse = transportClient
				.prepareGet(indexName, indexType, id)
				.execute().actionGet();
		
		return getResponse.getSource();
    }

2)mutilget:

public static List<Map<String,Object>> multiGet(String indexName,String indexType,String... ids){
		List<Map<String,Object>> list = new ArrayList<>();
		MultiGetResponse response = transportClient.prepareMultiGet()
				.add(indexName,indexType,ids)
				.add("liu1","liu1_type","AV8-YHAr6f3B-qEudDab")//另一个index、indextype和ids
				.execute().actionGet();
		for(MultiGetItemResponse itemResponse:response){  
            GetResponse getResponse = itemResponse.getResponse();  
            if(getResponse.isExists()){
            	list.add(getResponse.getSource());
                //System.out.println(getResponse.getSourceAsString());  
            }  
        }
		
		return list;
    }


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值