前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1、Elasticsearch创建Index

1、Elasticsearch创建Index

作者头像
BUG弄潮儿
发布2020-06-12 15:52:38
7090
发布2020-06-12 15:52:38
举报
文章被收录于专栏:JAVA乐园JAVA乐园

阅读文本大概需要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官方有如下一段话

代码语言:javascript
复制
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文件中引入

代码语言:javascript
复制
<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>transport</artifactId>
   <version>7.0.0</version>
</dependency>

2、构建TransportClient对象

代码语言:javascript
复制
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分为单文档和多文档操作

代码语言:javascript
复制
https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.0/java-docs.html

4、创建Document

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

使用json字符串创建Index

代码语言:javascript
复制
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

代码语言:javascript
复制
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文件

代码语言:javascript
复制
<?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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档