快速开始
> 文档中心 > 文档中心 > INFINI Easysearch > 功能手册 > 客户端 > 已弃用 > 快速开始

Java 高级 REST 客户端 #

Easysearch Java 高级 REST 客户端允许您通过 Java 方法和数据结构与您的 Easysearch 集群和索引进行交互,避免使用 HTTP 方法和 JSON。

快速开始 #

本页指导您完成Java客户端的安装过程,展示了如何实例化客户端,以及如何使用它执行基本的 Easysearch 操作。

安装 #

easysearch-client 已经发布到 Maven 中央仓库:https://repo1.maven.org/maven2/

安装需要 jdk11 或以上版本

在 Maven 项目中安装 #

在您项目的 pom 文件的 dependencies 区域添加以下依赖以引入客户端

    <dependency>
        <groupId>com.infinilabs</groupId>
        <artifactId>easysearch-client</artifactId>
        <version>1.0.1</version>
    </dependency>

以下是客户端依赖的第三方的库:

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.19.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.19.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore-nio</artifactId>
        <version>4.4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.4</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-backward-codecs</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-grouping</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-highlighter</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-join</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-memory</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-misc</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queries</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-sandbox</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-spatial3d</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-suggest</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

在 gradle 项目中安装 #

在您项目的 build.gradle 文件的 dependencies 区域添加以下依赖

dependencies {
    implementation 'com.infinilabs:easysearch-client:1.0.1'
    implementation "org.apache.logging.log4j:log4j-api:2.19.0"
    implementation "org.apache.logging.log4j:log4j-core:2.19.0"
    implementation 'org.apache.httpcomponents:httpclient:4.5.10'
    implementation 'org.apache.httpcomponents:httpcore-nio:4.4.12'
    implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
    implementation 'joda-time:joda-time:2.10.4'
    implementation ('org.apache.lucene:lucene-core:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-analyzers-common:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-backward-codecs:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-grouping:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-highlighter:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-join:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-memory:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-misc:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-queries:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-queryparser:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-sandbox:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-spatial3d:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-suggest:8.11.2') {
        exclude group: '*', module: '*'
    }
    
  ......
}

初始化客户端 #

假设您本地启动了 Easysearch 服务,并启用了安全通信加密,配置了 security.enabled:true,用户名 admin,密码 admin。 以下代码可以初始化并返回 RestHighLevelClient 对象。


public class EasearchSearchConfig {

  String userName = "admin";
  String password = "admin";
     
  public RestHighLevelClient initEasysearchClient(String user, String password) throws Exception {
    try {
        HttpHost[] httpHostArray = new HttpHost[1];
        httpHostArray[0] = new HttpHost("localhost", 9200, "https");
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true).build();
        SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE);
        return new RestHighLevelClient(RestClient.builder(httpHostArray).setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
            httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            httpAsyncClientBuilder.disableAuthCaching();
            httpAsyncClientBuilder.setSSLStrategy(sessionStrategy);
            return httpAsyncClientBuilder;
        }));
    } catch (Exception e) {
        throw new RuntimeException("Failed to create Easysearch client", e);
    }
  }
  
   
}