1、Elasticsearch创建Index

阅读文本大概需要3分钟。

Elasticsearch官方为Java提供了三种客户端API:

  • TransportClient:这种方式通过TCP与Elasticsearch服务进行交互。

  • Java Low Level REST Client: 低级别的REST客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有ES版本。

  • Java High Level REST Client: 高级别的REST客户端,基于低级别的REST客户端,增加了编组请求JSON串、解析响应JSON串等相关api。使用的版本需要保持和ES服务端的版本一致,否则会有版本问题。

 

另外Spring框架也提供了spring-data-elasticsearch对Elasticsearch进行CURD操作,但是最底层也是基于Elasticsearch官方提供的API。

 

对于TransportClientAPI官方有如下一段话

https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-api.html

The TransportClient is deprecated infavour of the Java High Level RESTClient and will be removed in Elasticsearch 8.0. The migration guide describes all thesteps needed to migrate.

 

TransportClient API已经被标识为过期,并在Elasticsearch 8.0.版本后被移除。但是TransportClient API在一些陈年老项目还是使用到的,今天就从TransportClient API开始介绍一下Elasticsearch的几个Java客户端API。

 

1、在pom.xml文件中引入

<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>transport</artifactId>
   <version>7.0.0</version>
</dependency>

 

2、构建TransportClient对象

public static TransportClient getTransportClient(){

        TransportClient client = null;

        try {

             client = new PreBuiltTransportClient(Settings.EMPTY);

            //根据主机名获取InetAddress对象

            // InetAddress remAdd=InetAddress.getByName("es.server.com");

            //获取本地InetAddress对象

//            InetAddress locAdd=InetAddress.getLocalHost();

            //创建客户端clients

            InetAddress locAdd = InetAddress.getByName("127.0.0.1"); // ip

            client.addTransportAddress(new TransportAddress(locAdd, 9300));

            System.out.println(client);

            //client.close();

        }catch (Exception e){

            e.printStackTrace();

        }

        return client;

    }

3、document操作,document分为单文档和多文档操作

https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-docs.html

                           

4、创建Document

Index API 允许我们添加某种类型的JSON文档到特定的index ,并使之可搜索。

使用json字符串创建Index

 

public static IndexResponse getIndexResponseWithString(TransportClient client){

    String json = "{" +

            "\"user\":\"kimchy\"," +

            "\"postDate\":\"2013-01-30\"," +

            "\"message\":\"trying out Elasticsearch\"" +

            "}";



    IndexResponse response = client.prepareIndex

            ("twitter", "tweet", "1")

            .setSource(json, XContentType.JSON)

            .get();

    return response;

}

使用map创建Index

 

public static IndexResponse getIndexResponseWithMap(TransportClient client) {

    Map<String, Object> json = new HashMap<String, Object>();

    json.put("user","kimchy");

    json.put("postDate",new Date());

    json.put("message","trying out Elasticsearch");

    IndexResponse response = client.prepareIndex

            ("twitter2", "tweet2", "2")

            .setSource(json, XContentType.JSON)

            .get();

    return response;

}

生成JSON文档的方式如下:

  • 手动使用native byte[] or as a String

  • 使用一个可以自动转换为对应JSON 的Map

  • 使用第三方库如  Jackson序列化 beans

  • 使用内置的  XContentFactory.jsonBuilder()

备注:

1、pom.xml文件 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>es.transport</groupId>
    <artifactId>es-transport</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <!--<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>7.0.0</version>
        </dependency>-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.2</version>
        </dependency>

    </dependencies>

</project>

2、使用elasticsearch-5.6.13版本elasticsearch

往期精彩

01 漫谈发版哪些事,好课程推荐

02 Linux的常用最危险的命令

03 精讲Spring&nbsp;Boot—入门+进阶+实例

04 优秀的Java程序员必须了解的GC哪些

05 互联网支付系统整体架构详解

关注我

每天进步一点点

很干!在看吗?☟

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BUG弄潮儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值