ElasticSearch-02-JavaApi

简述

通过在 Java 编程中控制 ElasticSearch。

  • 截至笔记实践,已更新到了8.8.0版本。
  • ES 的 7.15.x 版本以上及以下 的 Api 使用有区别

ES 7.15.x 以下使用

ES 7.15.x 以上使用

  • 引入依赖

    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
    <dependencies>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.15.2</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.15.2</version>
    </dependency>
    <dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>7.17.10</version>
    </dependency>
    <dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.0.1</version>
    </dependency>
    <dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.17.10</version>
    </dependency>
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
    </dependency>
    </dependencies>
  • 获得客户端 (可以封装成一个工具类)

    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
    /**
    * @author 75654
    * @date 2023/6/4 15:37
    *
    * ElasticSearch JavaApi学习
    */
    public class App {
    public static void main(String[] args) throws IOException {
    // 创建 JavaApi 客户端
    // new RestHighLevelClient 7.15.x 后已被启用
    // 创建低级客户端
    RestClient restClient = RestClient.builder(
    new HttpHost("localhost", 9200)).build();
    // 使用Jackson映射器创建传输层
    // 也可以使用 ElasticsearchTransport 接收 restClientTransport
    // ElasticsearchTransport 是 RestClientTransport 的接口
    RestClientTransport restClientTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // 创建API客户端
    ElasticsearchClient client = new ElasticsearchClient(restClientTransport);

    // 查询:索引 wang 下的配置信息
    GetIndexResponse indexResponse = client.indices().get(s -> s.index("wang"));
    indexResponse.result().forEach( (k, v) -> System.out.println("k=" + k + ", v=" + v));

    restClientTransport.close();
    }
    }

    结果信息:

    1
    2
    3
    4
    5
    6
    六月 04, 2023 5:57:50 下午 org.elasticsearch.client.RestClient logResponse
    警告: request [GET http://localhost:9200/wang] returned 1 warnings: [299 Elasticsearch-7.17.10-fecd68e3150eda0c307ab9a9d7557f5d5fd71349 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."]
    k=wang, v=IndexState: {"aliases":{},"mappings":{},"settings":{"index":{"number_of_shards":"1","number_of_replicas":"1","routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"provided_name":"wang","creation_date":"1685872659618","uuid":"whZRN2vdTWKJPvMUMymmrg","version":{"created":"7171099"}}}}

    Process finished with exit code 0

获取 Elasticsearch Client 对象

1
2
3
4
5
6
7
8
9
10
11
// 创建 JavaApi 客户端
// new RestHighLevelClient 7.15.x 后已被启用
// 创建低级客户端
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
// 使用Jackson映射器创建传输层
// 也可以使用 ElasticsearchTransport 接收 restClientTransport
// ElasticsearchTransport 是 RestClientTransport 的接口
RestClientTransport restClientTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// 创建API客户端
ElasticsearchClient esClient = new ElasticsearchClient(restClientTransport);

索引操作

  1. 创建索引

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    private static void createIndex() throws IOException {
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
    ElasticsearchClient esClient = new ElasticsearchClient(
    new RestClientTransport(restClient, new JacksonJsonpMapper()));
    // 创建 user 索引
    CreateIndexResponse createIndexResponse = esClient.indices().create(s -> s.index("user"));
    boolean acknowledged = createIndexResponse.acknowledged();
    boolean b = createIndexResponse.shardsAcknowledged();
    String index = createIndexResponse.index();
    System.out.println(acknowledged);
    System.out.println(b);
    System.out.println(index);

    restClient.close();
    }
  2. 查看索引

    • 指定索引

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      private static void searchIndex() throws IOException {
      RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
      ElasticsearchClient esClient = new ElasticsearchClient(
      new RestClientTransport(restClient, new JacksonJsonpMapper()));
      // 查询 user 索引
      GetIndexResponse getIndexResponse = esClient.indices().get(s -> s.index("user"));
      Map<String, IndexState> result = getIndexResponse.result();
      Set<Map.Entry<String, IndexState>> entries = result.entrySet();
      for (Map.Entry<String, IndexState> entry : entries) {
      System.out.println("Key==" + entry.getKey());
      System.out.println("Value==" + entry.getValue());
      }
      }
    • 全部索引

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      private static void searchIndexAll() throws IOException {
      RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
      ElasticsearchClient esClient = new ElasticsearchClient(
      new RestClientTransport(restClient, new JacksonJsonpMapper()));
      // 查询全部索引 cat
      IndicesResponse indicesResponse = esClient.cat().indices();

      List<IndicesRecord> indicesRecords = indicesResponse.valueBody();
      for (IndicesRecord indicesRecord : indicesRecords) {
      System.out.println("index==" + indicesRecord);
      }
      }
  3. 删除索引

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    private static void deleteIndex() throws IOException {
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
    ElasticsearchClient esClient = new ElasticsearchClient(
    new RestClientTransport(restClient, new JacksonJsonpMapper()));
    // 删除索引 user
    DeleteIndexResponse deleteIndexResponse = esClient.indices().delete(s -> s.index("user"));

    boolean acknowledged = deleteIndexResponse.acknowledged();
    System.out.println(acknowledged);

    restClient.close();
    }

文档操作

  1. 创建文档
  • 单个创建
  • 批量创建 - BulkOperation
    2.
    查看文档
  • 查看指定文档
  • 查看索引下所有文档
  1. 修改文档
  • 覆盖式修改
  • 部分修改 - map
  1. 删除文档
  • 指定删除
    • 批量删除 - BulkOperation

**代码: **DocTest.md

★复杂文档查询

  • 复杂查询:
    • 1.条件查询 2.分页、排序 3.过滤字段 4.模糊查询 5.高亮查询
  • 聚合查询:
    • 1.最大值 2.分组统计

**代码: **QueryTest.md

问题

  1. Exception in thread “main” java.lang.NoClassDefFoundError: org/elasticsearch/client/Cancellable

    原因: elasticsearch-rest-high-level-client内部引用的是低版本,版本不一致

    image-20230531174214742

    解决:引入高版本,覆盖旧版本

  2. ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property ‘org.apache.logging.log4j.simplelog.StatusLogger.level’ to TRACE to show Log4j2 internal initialization logging.

    原因:没有 log4j2 的配置文件

    解决:添加 log4j2 的配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
    <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
    <PatternLayout pattern="[%-5p] %d %c - %m%n" />
    </Console>
    <File name="File" fileName="D:/log/elasticsearch.log">
    <PatternLayout pattern="%m%n" />
    </File>
    </Appenders>

    <Loggers>
    <Logger name="mh.sample2.Log4jTest2" level="INFO">
    <AppenderRef ref="File" />
    </Logger>
    <Root level="INFO">
    <AppenderRef ref="Console" />
    </Root>
    </Loggers>
    </Configuration>
  3. Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    原因:缺少 commons-logging 包

    解决:引入 commons-logging 包依赖

    1
    2
    3
    4
    5
    6
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
    </dependency>
  4. com/fasterxml/jackson/core/util/JacksonFeature

    原因:jackson-databind、jackson-core、jackson-annotations 版本不一致。(databind 中 包含 core、annotations 但是版本不一致)

    解决:将 jackson-databind、jackson-core、jackson-annotations 版本覆盖。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.15.2</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.15.2</version>
    </dependency>
  5. jakarta/json/JsonException

    原因:Json 版本较低

    解决:指定版本覆盖

    1
    2
    3
    4
    5
    6
    <!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api -->
    <dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.0.1</version>
    </dependency>