High Level REST Client 访问阿里云6.3 Elasticsearch 实例实现

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 开发环境:InteliJ IDEA 操作系统 :macOS Mojave Elasticsearch 版本:阿里云 6.3.2_with_X-Pack 客户端版本:REST Client 6.3.2 1. 预先创建好阿里云 ES 实例,开启公网地址访问白名单。

开发环境:InteliJ IDEA

操作系统 :macOS Mojave

Elasticsearch 版本:阿里云 6.3.2_with_X-Pack

客户端版本:REST Client 6.3.2


1. 预先创建好阿里云 ES 实例,开启公网地址访问白名单。

7267421bdcb9ac46228b2049053fe238f0e43294


2. 预先创建好 index 和 mapping(使用 Kibana Dev Tools 创建)

8d282de2736e3bc0800b2d57ab23f11f710c3813

mappings: book

properties: (book_id (keyword), name (text))

PUT index_test
{
  "mappings": {
    "book": {
      "properties" : {
        "book_id" : {
          "type":"keyword"
        },
        "name" : {
          "type":"text"
        }
      }
    }
  }
}


3. 创建项目及 RestClient 类

99cb144d9329839df7da39a352e483d273a4f220


4. 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>me.gary.es</groupId>
    <artifactId>high-level-rest-client-6</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.3.2</version>
        </dependency>
    </dependencies>
</project>

JDK至少在 1.8及以上版本。

Java high level REST Client 版本建议不高于 ES 实例的版本,最理想的是和集群版本一致。


5. 构建 REST Client 对象进行访问

Java High Level REST Client 基于 Java Low Level REST client 实现的基础上,主要目标是为了暴露该 API 特定的方法。将 request 对象作为参数,返回一个 response 对象。该 API 可以同步或异步调用,同步调用方式立即返回一个 response 对象;而异步调用方式依赖于监听,该监听当有请求返回或是错误返回时通知到该方法继续执行。Java High Level REST Client 依赖于 Elasticsearch core 项目,接收的 request 对象和返回的 response 对象和 TransportClient 一样。

本例仅演示同步调用方式。

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class RestClientTest {
    public static void main(String[] args) {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "******"));

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("es-cn-******.public.elasticsearch" +
                        ".aliyuncs.com", 9200))
                        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                            }
                        }));

        try {
            // 创建request
            Map<String, Object> jsonMap = new HashMap<>();
            jsonMap.put("book_id", "0001");
            jsonMap.put("name", "Pride and Prejudice");
            IndexRequest indexRequest = new IndexRequest("index_test", "book", "0001")
                    .source(jsonMap);

            // 同步执行
            IndexResponse indexResponse = client.index(indexRequest);
            long version = indexResponse.getVersion();
            System.out.println();


            client.close();
        } catch (IOException ioException) {
            // 异常处理
        }
    }
}


该文档为第一次更新,成功返回版本号“1”,示例运行成功。
09234e0ab64c8a73e91f27a95613f015f723668e

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
10月前
|
前端开发 Java Docker
利用 docker 部署 elasticsearch 集群(单节点多实例)
利用 docker 部署 elasticsearch 集群(单节点多实例)
467 0
|
4月前
|
搜索推荐 Java 数据处理
Elasticsearch搜索分析引擎本地部署与远程访问
Elasticsearch搜索分析引擎本地部署与远程访问
|
4月前
elasticsearch使用 scroll 滚动分页实战实例
elasticsearch使用 scroll 滚动分页实战实例
234 0
|
3月前
|
数据库 索引
Elasticsearch索引别名:管理与优化数据访问
Elasticsearch索引别名:管理与优化数据访问
|
3月前
ElasticSearch绑定IP访问
ElasticSearch绑定IP访问
|
9月前
|
Java Linux 数据安全/隐私保护
百度搜索:蓝易云【centos7系统安装elasticsearch8.7.0,并设置密码访问教程。】
现在,您已经成功安装并设置密码访问Elasticsearch 8.7.0。您可以使用设置的密码来访问和管理Elasticsearch实例。
196 1
|
4月前
|
安全 数据安全/隐私保护
Elasticsearch7.10.0添加访问密码
Elasticsearch7.10.0添加访问密码
110 1
|
4月前
|
存储 数据可视化 数据建模
阿里云大佬叮嘱我务必要科普这个 Elasticsearch API
阿里云大佬叮嘱我务必要科普这个 Elasticsearch API
51 0
|
4月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
96 0
|
10月前
|
存储 弹性计算 运维
阿里云 Elasticsearch Severless 如何做到成本降低50%
阿里云 Elasticsearch Serverless 服务正式上线。全新产品形态,基于云原生 Serverless 技术,致力于为用户打造更低成本、弹性灵活、开放兼容、开箱即用的云上 Elasticsearch 使用体验。