Elasticsearch 入门

Elasticsearch 是一个基于 Apache Lucene 的开源搜索引擎,适用于全文索引、大数据存储和分析场景。本文介绍了 Elasticsearch 的基本概念如集群、节点、分片和副本,详细讲解了安装过程,以及如何通过 REST API 和 Java API 进行索引、文档操作。还提到了在 SpringBoot 中集成 Elasticsearch 的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Elasticsearch 介绍

概述

场景

Elasticsearch 基础

基本概念

数据结构

Elasticsearch 安装

 系统和软件

安装说明

Elasticsearch 使用

Rest Api

集群操作

索引操作

文档操作 

Java Api 集成

SpringBoot 集成

添加依赖

添加配置

添加 Entity

添加 Service

添加 ServiceImpl

添加 Controller

Elasticsearch 总结


Elasticsearch 介绍

概述

Elasticsearch(ES) 在搜索和数据分析场景中的卓越表现越来越引人注目,它在很多互联网应用中起着非常关键的作用。

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可以说是当下最先进、高性能、全功能的搜索引擎库。

Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎,它可以被下面这样准确的形容:

  • 一个分布式的实时文档存储,每个字段可以被索引与搜索;
  • 一个分布式实时分析搜索引擎;
  • 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据。

场景

Elasticsearch 的使用场景非常丰富,甚至可以考虑使用 Elasticsearch 作为唯一的数据存储,以下罗列适合使用 Elasticsearch 的场景:

  • 支持全文索引场景;
  • 大数据存储和分析场景;
  • 作为非关系型数据存储,例如日志的记录和分析,常和 Kibana 可视化配合使用;
  • 数据不需要频繁更新、不需要强事务要求、数据之间关系较为简单。

Elasticsearch 基础

基本概念

Elasticsearch 主要包含如下几个基本概念:

  • cluster(集群):一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能;
  • node(节点):一个节点是你集群中的一个服务器,作为集群的一部分,它存储你的数据,参与集群的索引和搜索功能;
  • shard(分片):数据的最小单元块,创建一个索引的时候,可以指定分片的数量。每个分片是一个功能完善并且独立的“索引”,可以被放置到集群中的任何节点上;
  • replica(副本):分片的一份或多份拷贝叫做复制分片,或者直接叫复制。

数据结构

Elasticsearch 是面向文档型数据库,一条数据在这里就是一个文档。数据结构包含:index(索引)、type(类型)、document(文档)、fields(字段)。

  • index(索引):索引即数据,索引是文档的载体,相当于 MySQL 的 database;
  • type(类型):在 Elasticsearch 7 版本中已经被废弃;
  • document(文档):数据内容,相当 MySQL 的行,存储 JSON 非关系型数据;
  • fields(字段):字段,相当 MySQL 的列,如果存储 JSON 结构,为 JSON 的属性。

为了方便理解,将 Elasticsearch 里存储文档数据和关系型数据库 MySQL 存储数据的概念进行一个类比。

Elasticsearch 安装

 系统和软件

安装实验在 CentOS 系统进行,请提前准备,软件安装包 elasticsearch-7.8.0-linux-x86_64.tar.gz 这里获取。

  • 操作系统:CentOS Linux release 7.9.2009 (Core)
  • Elasticsearch:elasticsearch-7.8.0

安装说明

  • 创建用户设置密码

elasticsearch-7.8.0 出于安全考虑,需要非 root 权限运行,使用 root 权限添加用户 es

useradd es && passwd es 

此处输入两次密码即可
  • 修改文件描述符数量和进程数量配置 /etc/security/limits.conf
## 将以下内容追加到文件最后 
es soft nofile 65536 
es hard nofile 65535 
es soft nproc 4096 
es hard nproc 4096
  • 修改一个进程的虚拟内存数量 /etc/sysctl.conf
## 将以下内容追加到文件最后 
vm.max_map_count=655360

 ##然后执行
 sysctl -p
  • 切换 es 账户
su es && cd ~
  • 解压安装包
tar zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz && mv elasticsearch-7.8.0 es
  • 添加配置 es/config/elasticsearch.yml
## 将以下内容追加到文件最后 
cluster.name: elasticsearch 
node.name: node-1 
network.host: 0.0.0.0 
http.port: 9200 
cluster.initial_master_nodes: ["node-1"]
  • 启动程序
bin/elasticsearch

[2022-10-25T23:56:02,784][INFO ][o.e.d.DiscoveryModule    ] [node-1] using discovery type [zen] and seed hosts providers [settings]
[2022-10-25T23:56:04,315][INFO ][o.e.n.Node               ] [node-1] initialized
[2022-10-25T23:56:04,316][INFO ][o.e.n.Node               ] [node-1] starting ...
[2022-10-25T23:56:09,819][INFO ][o.e.t.TransportService   ] [node-1] publish_address {192.168.0.2:9300}, bound_addresses {[::]:9300}
[2022-10-25T23:56:10,018][INFO ][o.e.b.BootstrapChecks    ] [node-1] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2022-10-25T23:56:10,038][INFO ][o.e.c.c.Coordinator      ] [node-1] setting initial configuration to VotingConfiguration{qATFpUAGRs-Y7L3j6sGuIw}
[2022-10-25T23:56:10,272][INFO ][o.e.c.s.MasterService    ] [node-1] elected-as-master ([1] nodes joined)[{node-1}{qATFpUAGRs-Y7L3j6sGuIw}{-cJnjB3tTKKkdzqUT9QPZQ}{192.168.0.2}{192.168.0.2:9300}{dilmrt}{ml.machine_memory=1019572224, xpack.installed=true, transform.node=true, ml.max_open_jobs=20} elect leader, _BECOME_MASTER_TASK_, _FINISH_ELECTION_], term: 1, version: 1, delta: master node changed {previous [], current [{node-1}{qATFpUAGRs-Y7L3j6sGuIw}{-cJnjB3tTKKkdzqUT9QPZQ}{192.168.0.2}{192.168.0.2:9300}{dilmrt}{ml.machine_memory=1019572224, xpack.installed=true, transform.node=true, ml.max_open_jobs=20}]}
[2022-10-25T23:56:10,372][INFO ][o.e.c.c.CoordinationState] [node-1] cluster UUID set to [C45uu309Tf68UNw8ZAq4Gg]
[2022-10-25T23:56:10,458][INFO ][o.e.c.s.ClusterApplierService] [node-1] master node changed {previous [], current [{node-1}{qATFpUAGRs-Y7L3j6sGuIw}{-cJnjB3tTKKkdzqUT9QPZQ}{192.168.0.2}{192.168.0.2:9300}{dilmrt}{ml.machine_memory=1019572224, xpack.installed=true, transform.node=true, ml.max_open_jobs=20}]}, term: 1, version: 1, reason: Publication{term=1, version=1}
[2022-10-25T23:56:10,679][INFO ][o.e.h.AbstractHttpServerTransport] [node-1] publish_address {192.168.0.2:9200}, bound_addresses {[::]:9200}
[2022-10-25T23:56:10,680][INFO ][o.e.n.Node               ] [node-1] started

Elasticsearch 使用

Rest Api

Elasticsearch提供了一个非常全面和强大的 REST API,开发者利用 REST API,可以实现与集群之间的交互,主要提供了如下几个功能:

  • 检查集群、节点和索引的健康信息、状态以及各种统计信息;
  • 管理集群、节点、索引数据以及元数据;
  • 对索引进行 CRUD(创建、读取、更新和删除)和搜索操作;
  • 执行高级的搜索操作, 例如分页、排序、过滤、脚本编写(scripting)、聚合(aggregations)以及其它操作。

集群操作

  • 查看集群健康信息
curl -X GET "http://localhost:9200/_cat/health?v"

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1665584601 14:23:21  elasticsearch green           1         1      6   6    0    0        0             9              21.9h                100.0%
  • 查看节点详细信息
curl -X GET "localhost:9200/_cat/nodes?v"

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           19          39   0                          dilmrt    *      LAPTOP-M4N1OUMV

索引操作

  • 查看所有索引信息
curl -X GET "localhost:9200/_cat/indices?v"

health status index                          uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana-event-log-7.8.0-000001 BVN8w1jRSUC_NbZHnHAqeg   1   0          1            0      5.3kb          5.3kb
green  open   .apm-custom-link               AaHmK0QgRRijvG3WpMtfiA   1   0          0            0       208b           208b
green  open   .kibana_task_manager_1         JZIHyd8WTIWceeTrdk40OA   1   0          5            1     30.3kb         30.3kb
green  open   .apm-agent-configuration       9iYUjgDMS6atDTu2RyK0fw   1   0          0            0       208b           208b
green  open   .kibana_1                      0fOJ65F4RD2nYEjHPqQ9LA   1   0         21            2     63.4kb         63.4kb
  • 创建索引
curl -X PUT "http://localhost:9200/device?pretty"

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "device"
}

curl -X GET "localhost:9200/_cat/indices?v"

health status index                          uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana-event-log-7.8.0-000001 BVN8w1jRSUC_NbZHnHAqeg   1   0          1            0      5.3kb          5.3kb
green  open   .apm-custom-link               AaHmK0QgRRijvG3WpMtfiA   1   0          0            0       208b           208b
green  open   .kibana_task_manager_1         JZIHyd8WTIWceeTrdk40OA   1   0          5            1     30.3kb         30.3kb
green  open   .apm-agent-configuration       9iYUjgDMS6atDTu2RyK0fw   1   0          0            0       208b           208b
yellow open   device                         KTwSlHXnTdOKE_gMrlKKUw   1   1          0            0       208b           208b
green  open   .kibana_1                      0fOJ65F4RD2nYEjHPqQ9LA   1   0         21            2     63.4kb         63.4kb
  • 查看某个索引
curl -X GET "http://localhost:9200/device"

{
    "device": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1665585246007",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "KTwSlHXnTdOKE_gMrlKKUw",
                "version": {
                    "created": "7080099"
                },
                "provided_name": "device"
            }
        }
    }
}
  • 删除索引
curl -X DELETE "http://localhost:9200/device"

{
    "acknowledged": true
}

curl -X GET "localhost:9200/_cat/indices?v"

health status index                          uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana-event-log-7.8.0-000001 BVN8w1jRSUC_NbZHnHAqeg   1   0          1            0      5.3kb          5.3kb
green  open   .apm-custom-link               AaHmK0QgRRijvG3WpMtfiA   1   0          0            0       208b           208b
green  open   .kibana_task_manager_1         JZIHyd8WTIWceeTrdk40OA   1   0          5            1     30.3kb         30.3kb
green  open   .apm-agent-configuration       9iYUjgDMS6atDTu2RyK0fw   1   0          0            0       208b           208b
green  open   .kibana_1                      0fOJ65F4RD2nYEjHPqQ9LA   1   0         21            2     63.4kb         63.4kb
  • 添加映射
curl -X PUT "http://127.0.0.1:9200/device/_mapping?pretty" -d \
{"properties":{"devName":{"type":"text","index":true},"devProto":{"type":"keyword","index":true},"tel":{"type":"keyword","index":false}}}

 {
    "acknowledged": true
}
  • 查看映射
curl -X PUT "http://127.0.0.1:9200/device/_mapping?pretty"

{
    "device": {
        "mappings": {
            "properties": {
                "devName": {
                    "type": "text"
                },
                "devProto": {
                    "type": "keyword"
                },
                "tel": {
                    "type": "keyword",
                    "index": false
                }
            }
        }
    }
}

文档操作 

  • 创建文档信息
curl --location --request POST 'http://localhost:9200/device/_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "压力设备001",
    "sno": "YALISHEBEI001",
    "temp": 30
}'

{
    "_index": "device",
    "_type": "_doc",
    "_id": "0IDy9YMBsJn4TX1-SR7b",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 2
}
  • 查看文档信息
curl --location --request GET 'http://localhost:9200/device/_doc/0IDy9YMBsJn4TX1-SR7b'

 {
    "_index": "device",
    "_type": "_doc",
    "_id": "0IDy9YMBsJn4TX1-SR7b",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "name": "压力设备001",
        "sno": "YALISHEBEI001",
        "temp": 30
    }
}
  • 删除文档信息
curl --location --request DELETE 'http://localhost:9200/device/_doc/0IDy9YMBsJn4TX1-SR7b'

 {
    "_index": "device",
    "_type": "_doc",
    "_id": "0IDy9YMBsJn4TX1-SR7b",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 2
}
  • 全量更新文档信息
curl --location --request PUT 'http://localhost:9200/device/_doc/10000' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "压力设备002",
    "sno": "YALISHEBEI001",
    "temp": 300
}'

 {
    "_index": "device",
    "_type": "_doc",
    "_id": "10000",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 2
}
  • 查看所有文档
curl --location --request GET 'http://localhost:9200/device/_search'

 {
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "10000",
                "_score": 1.0,
                "_source": {
                    "name": "压力设备002",
                    "sno": "YALISHEBEI001",
                    "temp": 300
                }
            },
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "1ID79YMBsJn4TX1-5R73",
                "_score": 1.0,
                "_source": {
                    "name": "压力设备001",
                    "sno": "YALISHEBEI001",
                    "temp": 30
                }
            }
        ]
    }
}
  • 条件查询文档
curl --location --request GET 'http://localhost:9200/device/_search?q=name:压力设备002'

 {
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.4224334,
        "hits": [
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "10000",
                "_score": 1.4224334,
                "_source": {
                    "name": "压力设备002",
                    "sno": "YALISHEBEI001",
                    "temp": 300
                }
            },
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "1ID79YMBsJn4TX1-5R73",
                "_score": 0.72928625,
                "_source": {
                    "name": "压力设备001",
                    "sno": "YALISHEBEI001",
                    "temp": 30
                }
            }
        ]
    }
}
  • 分页关键字查询文档
curl --location --request GET 'http://localhost:9200/device/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "match": {
            "name": "001"
        }
    },
    "from": 0,
    "size": 2
}'

 {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "1ID79YMBsJn4TX1-5R73",
                "_score": 0.6931471,
                "_source": {
                    "name": "压力设备001",
                    "sno": "YALISHEBEI001",
                    "temp": 30
                }
            }
        ]
    }
}
  • 查询文档指定返回属性
curl --location --request GET 'http://localhost:9200/device/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "match": {
            "name": "001"
        }
    },
    "from": 0,
    "size": 2,
    "_source": [
        "name"
    ]
}'

 {
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "1ID79YMBsJn4TX1-5R73",
                "_score": 0.6931471,
                "_source": {
                    "name": "压力设备001"
                }
            }
        ]
    }
}
  • 对某个字段进行排序
curl --location --request GET 'http://localhost:9200/device/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "match": {
            "name": "压力设备"
        }
    },
    "from": 0,
    "size": 2,
    "_source": [
        "name",
        "temp"
    ],
    "sort": {
        "temp": {
            "order": "asc"
        }
    }
}'

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "1ID79YMBsJn4TX1-5R73",
                "_score": null,
                "_source": {
                    "temp": 30,
                    "name": "压力设备001"
                },
                "sort": [
                    30
                ]
            },
            {
                "_index": "device",
                "_type": "_doc",
                "_id": "10000",
                "_score": null,
                "_source": {
                    "temp": 300,
                    "name": "压力设备002"
                },
                "sort": [
                    300
                ]
            }
        ]
    }
}

Java Api 集成

  • 添加依赖
<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.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
  • 索引操作
package com.skystep.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsIndex {

    private RestHighLevelClient client;


    public static void main(String[] args) throws IOException, IOException {
        EsIndex esIndex = new EsIndex();
        esIndex.open();
        esIndex.createIndex("test");
        esIndex.getIndex("test");
        esIndex.deleteIndex("test");
        esIndex.close();
    }

    // 创建索引
    public Boolean createIndex(String indexName) throws IOException {
        // 创建索引 - 请求对象
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        // 发送请求,获取响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    }

    // 查看索引
    public GetIndexResponse getIndex(String indexName) throws IOException {
        // 查询索引 - 请求对象
        GetIndexRequest request = new GetIndexRequest(indexName);
        // 发送请求,获取响应
        return client.indices().get(request, RequestOptions.DEFAULT);
    }

    // 删除索引
    public Boolean deleteIndex(String indexName) throws IOException {
        // 删除索引 - 请求对象
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        // 发送请求,获取响应
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    }

    public void open() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.0.2", 9200, "http"))
        );
        this.client = client;
    }

    public void close() throws IOException {
        client.close();
    }
}
  • 文档操作
package com.skystep.es;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsDoc {
    static 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;
        }
    }


    private RestHighLevelClient client;

    public static void main(String[] args) throws IOException {
        EsDoc esDoc = new EsDoc();
        esDoc.open();
        User user = new User();
        user.setAge(26);
        user.setName("jak");
        user.setSex("男");
        String id = esDoc.createDoc("user", user);
        esDoc.getDoc("user", id);
        user.setSex("女");
        esDoc.updateDoc("user", id, user);
        esDoc.getDoc("user", id);
        esDoc.deleteDoc("user", id);
        esDoc.close();
    }


    public void open() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.0.2", 9200, "http"))
        );
        this.client = client;
    }

    public void close() throws IOException {
        client.close();
    }

    // 创建文档
    public String createDoc(String indexName, User user) throws IOException {
        // 新增文档 - 请求对象
        IndexRequest request = new IndexRequest();
        // 设置索引及唯一性标识
        request.index(indexName);
        // 添加文档数据, 数据格式为Json格式
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 客户端发送请求,获取响应对象
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        // 返回id
        return index.getId();
    }

    // 修改文档
    public String updateDoc(String indexName, String id, User user) throws IOException {
        // 修改文档 - 请求对象
        UpdateRequest request = new UpdateRequest();
        // 配置修改参数
        request.index(indexName).id(id);
        // 设置请求体,对数据进行修改
        request.doc(XContentType.JSON, "name", user.getName(), "age", user.getAge(), "sex", user.getSex());
        // 客户端发送请求,获取响应对象
        UpdateResponse index = client.update(request, RequestOptions.DEFAULT);
        // 返回id
        return index.getId();
    }

    // 查询文档
    public void getDoc(String indexName, String id) throws IOException {
        // 创建请求对象
        GetRequest request = new GetRequest().index(indexName).id(id);
        // 客户端发送请求,获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index: " + response.getIndex());
        System.out.println("_type: " + response.getType());
        System.out.println("_id: " + response.getId());
        System.out.println("source: " + response.getSourceAsString());
    }

    // 删除文档
    public void deleteDoc(String indexName, String id) throws IOException {
        // 创建请求对象
        DeleteRequest request = new DeleteRequest().index(indexName).id(id);
        // 客户端发送请求,获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        // 打印信息
        System.out.println(response.toString());
    }
}

SpringBoot 集成

SpringBoot 版本为 2.3.7.RELEASE,使用 spring-boot-starter-data-elasticsearch 实现 elasticsearch 增删改查。

添加依赖

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

添加配置

# 应用名称
spring.application.name=elasticSearch
# 应用服务 WEB 访问端口
server.port=8080
spring.elasticsearch.rest.uris=http://localhost:9200

添加 Entity

package com.skystep.elasticsearch.entity;

import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.List;

@Document(indexName = "sys_user")   //文档
@Data
@Builder
public class SysUser implements Serializable {
    @Id //主键
    private String id;  //ES中id不能定义为Long
    private String username;
    private String password;
    private int level;
    @Field(type = FieldType.Keyword)
    private List<String> roles;
}

添加 Service

package com.skystep.elasticsearch.entity;

import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.List;

@Document(indexName = "sys_user")  
@Data
@Builder
public class SysUser implements Serializable {
    @Id //主键
    private String id;  //ES中id不能定义为Long
    private String username;
    private String password;
    private int level;
    @Field(type = FieldType.Keyword)
    private List<String> roles;
}

添加 ServiceImpl

package com.skystep.elasticsearch.service.impl;

import com.skystep.elasticsearch.entity.SysUser;
import com.skystep.elasticsearch.mapper.SysUserMapper;
import com.skystep.elasticsearch.service.SysUserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class SysUserServiceImpl implements SysUserService {

    final SysUserMapper sysUserMapper;

    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public void create() {
        List<String> list = new ArrayList<>();
        list.add("teacher");
        list.add("student");
        list.add("admin");
        list.add("leader");
        for (int i = 0; i < 1000; i++) {
            int toIndex = new Random(1).nextInt(4);
            SysUser build = SysUser.builder()
                    .password("123456")
                    .username("AI码师")
                    .level(i)
                    .roles(list.subList(0, toIndex))
                    .build();
            sysUserMapper.save(build);
        }
        System.out.printf("结束");
    }

    @Override
    public List<SysUser> getList() {
        List<SysUser> sysUsers = new ArrayList<>();
        Iterable<SysUser> all = sysUserMapper.findAll();
        all.forEach((sysUser) -> {
            sysUsers.add(sysUser);
        });
        return sysUsers;
    }
}

添加 Controller

package com.skystep.elasticsearch.controller;

import com.skystep.elasticsearch.service.SysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SysUserServiceController {
    final SysUserService sysUserService;

    public SysUserServiceController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @GetMapping("/sys/user/create")
    public void create() {
        sysUserService.create();
    }

    @GetMapping("/sys/user/list")
    public void list() {
        sysUserService.getList();
    }
}

Elasticsearch 总结

Elasticsearch 是一个非常强大的搜索引擎。它目前被广泛地使用于各个 IT 公司。Elasticsearch 是由 Elastic 公司创建。它的代码位于 GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine。目前,Elasticsearch 是一个免费及开放(free and open)的项目。同时,Elastic 公司也拥有 Logstash 及 Kibana 开源项目。这个三个项目组合在一起,就形成了 ELK 软件栈。他们三个共同形成了一个强大的生态圈。简单地说,Logstash 负责数据的采集,处理(丰富数据,数据转换等),Kibana 负责数据展示,分析,管理,监督及应用。Elasticsearch 处于最核心的位置,它可以帮我们对数据进行快速地搜索及分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值