ElasticSearch在Java中实现增删改查

ElasticSearch在Java中实现增删改查

ElasticSearch6.x

  1. 从es中获取数据
  2.  添加一条文档
  3. 删除一条文档
  4. 使用update更新,局部更新,前提必须有数据
  5. 使用upsert更新,局部更新,之前沒有則創建,有則更新

如果是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.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class ESTest {

	/**
	 * 从es中获取数据
	 * 
	 * @throws UnknownHostException
	 */
	@Test
	public void query1() throws UnknownHostException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "my-application").build();
		System.out.println(settings);
		// 创建ES客户端,客户端端口号默认9300,不要写web端口9200
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"), 9300));
		// 数据查询
		GetResponse response = client.prepareGet("lib", "user", "1").execute().actionGet();

		System.out.println(response.getSourceAsString());

		client.close();
	}

	/**
	 * 添加一条文档
	 * 
	 * @throws IOException
	 */
	@Test
	public void add1() throws IOException {
		// 指定ES集群
		Settings settings = Settings.builder().put("cluster.name", "my-application").build();
		System.out.println(settings);
		// 创建ES客户端,客户端端口号默认9300,不要写web端口9200
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"), 9300));

		// 构建文档数据
		XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("first_name", "Green")
				.field("last_name", "Smith").field("age", 32).field("about", "I like to collect rock albums")
				.array("interests", "music", "dance").endObject();
		// 添加文档
		IndexResponse response = client.prepareIndex("lib", "user", "100").setSource(doc).get();

		// 查看执行状态,创建成功后,之前没有CREATED,之前存在OK
		System.out.println(response.status());
		client.close();
	}

	/**
	 * 删除一条文档
	 * 
	 * @throws UnknownHostException
	 */
	@Test
	public void delete1() throws UnknownHostException {
		Settings settings = Settings.builder().put("cluster.name", "my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"), 9300));
		DeleteResponse response = client.prepareDelete("lib", "user", "100").get();

		System.out.println(response.status());
		client.close();
	}

	/**
	 * 使用update更新,局部更新,前提必须有数据
	 * 
	 * @throws Exception
	 */
	@org.junit.Test
	public void update() 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));
		UpdateRequest request = new UpdateRequest();
		request.index("lib").type("user").id("101").doc(XContentFactory.jsonBuilder().startObject()
				// .field("first_name","Green")
				// .field("last_name","Jone")
				.field("age", 24)
				// .field("about","keep trying no matter how hard it seems. it will get
				// easier.")
				// .array("interests", "music","spark","donce","bbb")
				.endObject());
		UpdateResponse update = client.update(request).get();
		System.out.println(update.status());
		GetResponse get = client.prepareGet("lib", "user", "101").execute().get();
		System.out.println(get.getSourceAsString());
		client.close();
	}

	/**
	 * 使用upsert更新,局部更新,之前沒有則創建,有則更新
	 */
	@org.junit.Test
	public void upsert() 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));
		// 沒有時,則按此數據創建
		IndexRequest index = new IndexRequest("lib", "user", "102").source(
				XContentFactory.jsonBuilder().startObject().field("first_name", "Green").field("last_name", "Jone")
						.field("age", 24).field("about", "keep trying no matter how hard it seems. it will get easier.")
						.array("interests", "music", "spark", "donce", "bbb").endObject());
		// 有數據則按此數據更新
		UpdateRequest update = new UpdateRequest("lib", "user", "102")
				.doc(XContentFactory.jsonBuilder().startObject().field("age", 28).endObject()).upsert(index);
		// 執行操作
		UpdateResponse updateResponse = client.update(update).get();
		// 查看狀態
		System.out.println(updateResponse.status());
		// 查詢執行結果
		GetResponse get = client.prepareGet("lib", "user", "102").execute().get();
		System.out.println(get.getSourceAsString());
		client.close();
	}
}

 

你可能感兴趣的:(ElasticSearch,大数据)