Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 认知

  • 入门

    • 环境准备
    • RESTful&JSON
    • 客户端工具
    • 倒排索引
    • 数据格式
    • RESTful-API

    • Java-API

      • 项目准备
        • 创建maven项目
        • 修改pom添加依赖
        • 添加数据模型
        • 测试链接
      • 索引-创建
      • 索引-查询
      • 索引-删除
      • 文档-新增
      • 文档-修改
      • 文档-主键查询
      • 文档-删除
      • 文档-批量新增
      • 文档-批量删除
      • 文档-高级查询-全量
      • 文档-高级查询-关键字查询
      • 文档-高级查询-分页
      • 文档-高级查询-排序
      • 文档-高级查询-指定返回字段
      • 文档-高级查询-Bool
      • 文档-高级查询-范围
      • 文档-高级查询-模糊
      • 文档-高亮
      • 文档-聚合查询-max
      • 文档-聚合查询-分组
  • 环境

  • 进阶

  • 框架集成

  • 优化

  • 面试题

  • Database-Elasticsearch
  • 入门
  • Java-API
Shetengteng
2022-01-26

项目准备

Elasticsearch 是由Java语言开发的,可以通过Java API的方式对Elasticsearch服务进行访问

# 创建maven项目

创建es-java项目

# 修改pom添加依赖

<?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>com.stt</groupId>
    <artifactId>es-java</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch 的客户端 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch 依赖 2.x 的 log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!-- junit 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

# 添加数据模型

package com.stt.demo;

public class User {
    private String name;
    private String sex;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 测试链接

编写测试类com.stt.demo.Test01_connect

注意:9200 端口为 Elasticsearch 的 Web 通信端口 ,localhost 为启动 ES 服务的主机名

package com.stt.demo;

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

import java.io.IOException;

// 测试链接
public class Test01_connect {
    public static void main(String[] args) throws IOException {
        // 创建客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 关闭客户端
        client.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

无报错,无返回,说明链接成功

Last Updated: 2022/02/05, 15:58:51
DSL-聚合查询
索引-创建

← DSL-聚合查询 索引-创建→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式