Elasticsearch java api操作(Java High Level Rest Client)

Posted yfb918

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch java api操作(Java High Level Rest Client)相关的知识,希望对你有一定的参考价值。

一、说明:

  一、Elasticsearch提供了两个JAVA REST Client版本:

  1、java low level rest client:

  低级别的rest客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有Elasticsearch版本。

  特点:maven引入

  使用介绍: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html

  2、java high rest client:

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

  从6.0.0开始加入的,目的是以java面向对象的方式进行请求、响应处理。

  每个API支持 同步、异步 两种方式,同步方法之间返回一个结果对象。异步的方法以async为后缀,通过listener参数来通知结果。高级java resy客户端依赖Elasticsearch core pproject

  兼容性说明:

  依赖jdk1.8和Elasticsearch core project

二、Java Low Level Rest Client的使用

1、JAVA High Level Rest Client maven集成

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.3.1</version>
</dependency>

版本:

Elasticsearch 6.3.1

pom文件:

<dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>0.9</version>
        </dependency>
    </dependencies>

给定集群的多个节点地址,将客户端负载均衡地向这个节点地址集发请求:

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * @Author: xiaolaotou
 * @Date: 2019/4/28
 */
public class ClientUtil {
    public static RestHighLevelClient getClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.100",9200,"http"),
                        new HttpHost("192.168.200.101",9200,"http"),
                        new HttpHost("192.168.200.102",9200,"http"))
        );
        return client;
    }
}

2、创建索引

    public static void createindex()throws Exception{
        RestHighLevelClient client = ClientUtil.getClient();
        //1、创建索引,设置索引名
        CreateIndexRequest request = new CreateIndexRequest("mess");
        //2、设置索引的settings
        request.settings(Settings.builder()
                .put("index.number_of_shards",3)//分片数
                .put("index.number_of_replicas",1)//副本数
                .put("analysis.analyzer.default.tokenizer","ik_smart"))//默认分词器
        ;
        //3、设置索引的mappings
        request.mapping("_doc",
                "  {\\n" +
                        "    \\"_doc\\": {\\n" +
                        "      \\"properties\\": {\\n" +
                        "        \\"message\\": {\\n" +
                        "          \\"type\\": \\"text\\"\\n" +
                        "        }\\n" +
                        "      }\\n" +
                        "    }\\n" +
                        "  }",
                XContentType.JSON);
        //4、设置索引的别名
        request.alias(new Alias("mmm"));
        //5、发送方式
//        //5、1采用同步方式发送请求
//        CreateIndexResponse response = client.indices().create(request);
//        //处理响应
//        boolean acknowledged = response.isAcknowledged();
//        boolean shardsAcknowledged = response.isShardsAcknowledged();
//        System.out.println("acknowledged = " + acknowledged);
//        System.out.println("shardsAcknowledged = " + shardsAcknowledged);
//        client.close();
        //5、2采用异步方法发送请求
        ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                //处理响应
                boolean acknowledged = createIndexResponse.isAcknowledged();
                boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
                System.out.println("shardsAcknowledged = " + shardsAcknowledged);
                System.out.println("acknowledged = " + acknowledged);
            }
            @Override
            public void onFailure(Exception e) {
                System.out.println("创建索引异常 = " + e.getMessage());
            }
        };
        client.indices().createAsync(request,listener);
        client.close();
    }

技术图片

技术图片

3、index document

 

 

 

 

 

 

待写!!!!!!!!!!!!!!!!!!!!!!!!

以上是关于Elasticsearch java api操作(Java High Level Rest Client)的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch Java API

Elasticsearch java api操作(Java High Level Rest Client)

Elasticsearch之JAVA API操作

Elasticsearch掰开揉碎第12篇java操作ES常用API

Elasticsearch - Java API 操作 ES7.15.0ES7.x 索引,文档;高级搜索

ElasticSearch 5.0.1 java API操作