java创建es索引,二、ElasticSearch 使用java接口创建索引及操作文档

ElasticSearch (2)使用java接口创建索引及操作文档

所有Elasticsearch操作都是使用Client对象执行的。Client 定义的所有API都是异步执行的(要么使用事件监听器回调或者使用Future模式)。此外,客户端上的操作可以批量累积和执行。

Elasticsearch官方计划在Elasticsearch 7.0中弃TransportClient,并在8.0中完全删除它。故,应该使用Java高级REST client,rest client执行HTTP请求来执行操作,无需再序列化的Java请求。Java高级REST Client API目前支持更常用的api,但还需要添加更多的api。

1、连接ElasticSearch

新建一个maven项目,在pom.xml文件中添加依赖: (注意版本对应)

org.elasticsearch.client

transport

5.5.2

com.google.code.gson

gson

2.8.2

连接的代码:

package com.asiainfo.test;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

public class ConnTest {

// elasticsearch的端口和ip

private static String host = "10.21.13.48";

private static int port = 9300;

public static void main(String[] args) throws Exception {

@SuppressWarnings({ "resource", "unchecked" })

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));

System.out.println(client);

client.close();

}

}

输出:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

org.elasticsearch.transport.client.PreBuiltTransportClient@783f6c

前面的 ERROR 是没有配置 Log4j2 的问题。

2、创建索引、并操作文档

import com.google.gson.JsonObject;

import org.elasticsearch.action.delete.DeleteResponse;

import org.elasticsearch.action.get.GetResponse;

import org.elasticsearch.action.index.IndexResponse;

import org.elasticsearch.action.update.UpdateResponse;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.common.xcontent.XContentType;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.net.InetAddress;

/**

* @author: LIJY

* @Description: ElasticSearch客户端连接服务器测试

* @Date: 2020/9/25 21:26

*/

public class IndexTest {

private static String host = "10.21.13.48";

private static int port = 9300;

private TransportClient client = null;

/**

* 获取连接

* @throws Exception

*/

@SuppressWarnings({"resource", "unchecked"})

@Before

public void getClient() throws Exception {

client = new PreBuiltTransportClient(Settings.EMPTY)

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));

}

/**

* 关闭连接

*/

@After

public void close() {

if (client != null) {

client.close();

}

}

/**

* 创建索引、 添加文档

* @throws Exception

*/

@Test

public void testIndex() throws Exception {

JsonObject object = new JsonObject();

object.addProperty("name", "java think");

object.addProperty("price", 100.0);

object.addProperty("date", "2015-09-14");

IndexResponse indexResponse = client.prepareIndex("book", "java", "1")

.setSource(object.toString(), XContentType.JSON).get();

System.out.println("索引名称:" + indexResponse.getIndex());

System.out.println("类型:" + indexResponse.getType());

System.out.println("文档ID:" + indexResponse.getId());

System.out.println("当前实例状态:" + indexResponse.status());

}

/**

* 根据id获取文档

* ElasticSearch提供了根据索引名称,类别,文档ID来获取数据

* @throws Exception

*/

@Test

public void testGet() throws Exception {

GetResponse fields = client.prepareGet("book", "java", "1").get();

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

}

/**

* 根据id修改文档

* ElasticSearch提供了根据索引名称,类别,文档ID来修改数据,修改的设置数据可以是Map,Json串,自带工具。

* 实际开发一般用Json

* @throws Exception

*/

@Test

public void testUpdate() throws Exception {

JsonObject object = new JsonObject();

object.addProperty("name", "python doc");

object.addProperty("price", 99.99);

object.addProperty("date", "2020-09-14");

UpdateResponse response = client.prepareUpdate("book", "java", "1")

.setDoc(object.toString(), XContentType.JSON).get();

System.out.println("索引名称:" + response.getIndex());

System.out.println("类型:" + response.getType());

System.out.println("文档ID:" + response.getId());

System.out.println("当前实例状态:" + response.status());

}

/**

* 根据id删除文档

* ElasticSearch提供了根据索引名称,类别,文档ID来删除数据

* @throws Exception

*/

@Test

public void testDelete() throws Exception {

DeleteResponse response = client.prepareDelete("book", "java", "1").get();

System.out.println("索引名称:" + response.getIndex());

System.out.println("类型:" + response.getType());

System.out.println("文档ID:" + response.getId());

System.out.println("当前实例状态:" + response.status());

}

}

你可能感兴趣的:(java创建es索引)