首页 文章 中间件 ElasticSearch在Java中实现批量操作
0
0
0
15

ElasticSearch在Java中实现批量操作

elasticsearch ElasticSearch Java

ElasticSearch在Java中实现批量操作

适用范围ElasticSearch6.x

如果是ElasticSearch7或以上版本情转到如下链接

REST高级客户端-DocumentApi:https://blog.csdn.net/u014646662/article/details/96473156

REST高级客户端SearchApi:https://blog.csdn.net/u014646662/article/details/96853830

更多新的查询方式(ElasticSearch相关文档):https://blog.csdn.net/u014646662/article/category/8747782

对人工智能感兴趣的同学,可以点击以下链接:

现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。

https://www.cbedai.net/u014646662

package cn.com.es.test;

import java.net.InetAddress;
 
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class ESMultiTest {
	
	/**
	 * mget 批量查詢
	 * @throws Exception
	 */
	@Test
	public void mget() throws Exception {
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		MultiGetResponse multiGetResponse = client.prepareMultiGet()
				.add("lib", "user", "100","101","102")
				.add("lib2", "books", "1","2")
				.get();
		for (MultiGetItemResponse item : multiGetResponse) {
			GetResponse rp = item.getResponse();
			if(rp != null && rp.isExists()) {
				System.out.println(rp.getSourceAsString());
			}
		}
	}
	
	/**
	 * bulk更新
	 * @throws Exception
	 */
	@Test
	public void bulk() throws Exception{
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
		
		bulkRequestBuilder.add(
				client.prepareIndex("lib2", "books", "1")
				.setSource(
						XContentFactory.jsonBuilder()
						.startObject()
						.field("title","java")
						.field("price","90")
						.endObject()
						)
				);
		
		bulkRequestBuilder.add(
				client.prepareIndex("lib2", "books", "2")
				.setSource(
						XContentFactory.jsonBuilder()
						.startObject()
						.field("title","html5")
						.field("price","90")
						.endObject()
						)
				);
		
		BulkResponse bulkResponse = bulkRequestBuilder.get();
		
		System.out.println(bulkResponse.status());
		
		if(bulkResponse.hasFailures()) {
			System.out.println("存在失败操作");
		}
	}
}

 

到此这篇关于“ElasticSearch在Java中实现批量操作”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持Go语言编程网!

相关文章

创建博客

开始创作
写作能提升自己能力,也能为他人分享知识。

在线教程

查看更多
  • Go入门指南

    Go入门指南

  • Go语言高级编程

    Go语言高级编程

  • Go Web 编程

    Go Web 编程

  • GO专家编程

    GO专家编程

  • Go语言四十二章经

    Go语言四十二章经

  • 数据结构和算法(Golang实现)

    数据结构和算法(Golang实现)

Go语言编程网

微信扫码关注订阅号


博客 资讯 教程 我的