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", "uncheck