目录

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);// 返回idreturn 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);// 返回idreturn 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不能定义为Longprivate 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不能定义为Longprivate 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;}@Overridepublic 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("结束");}@Overridepublic 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 处于最核心的位置,它可以帮我们对数据进行快速地搜索及分析。

Elasticsearch 入门相关推荐

  1. python elasticsearch 入门教程(二) ---全文搜索

    python elasticsearch 入门教程(二) ---全文搜索 截止目前的搜索相对都很简单:单个姓名,通过年龄过滤.现在尝试下稍微高级点儿的全文搜索--一项 传统数据库确实很难搞定的任务. ...

  2. Spring Data ElasticSearch入门案例

    Spring Data ElasticSearch入门案例 创建maven工程elasticsearch_springdata 基于maven导入坐标 导入spring data elasticsea ...

  3. ElasticSearch入门 第一篇:Windows下安装ElasticSearch

    这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  4. ElasticSearch入门 :Windows下安装ElasticSearch

    这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  5. ElasticSearch入门 附.Net Core例子

    1.什么是ElasticSearch? Elasticsearch是基于Lucene的搜索引擎.它提供了一个分布式,支持多租户的全文搜索引擎,它具有HTTP Web界面和无模式JSON文档. Elas ...

  6. ElasticSearch入门教程-索引

    ElasticSearch入门教程-索引 在本节中,我们将向Elasticsearch添加一些索引,映射和数据.此数据将用于本教程中说明的示例中. 创建索引 PUT http://localhost: ...

  7. ElasticSearch入门 第五篇:使用C#查询文档

    网址:http://www.cnblogs.com/ljhdo/p/4550135.html 这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Win ...

  8. 和我一起打造个简单搜索之ElasticSearch入门

    本文简单介绍了使用 Rest 接口,对 es 进行操作,更深入的学习,可以参考文末部分. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch ,这个版本比较稳 ...

  9. 全文搜索引擎 Elasticsearch 入门概念

    基本概念 Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例. 单个 Elastic 实例称为一个节点(nod ...

  10. python snap7 简书_第14篇-Python中的Elasticsearch入门

    我的Elasticsearch系列文章,逐渐更新中,欢迎关注 另外Elasticsearch入门,我强烈推荐ElasticSearch搭建手册给你,非常想尽的入门指南手册. 在本文中,我将讨论Elas ...

最新文章

  1. resin php,resin竟然开始支持PHP
  2. mysql字符集说明
  3. JavaScript三种弹出框(alert,confirm和prompt)用法举例
  4. mybatis缓存二级缓存_MyBatis缓存与Apache Ignite的陷阱
  5. html判断是安卓还是苹果手机,网页能够自己判断是pc端首页还是手机android和苹果。...
  6. python入门(5)使用文件编辑器编写代码并保存执行
  7. win2003下如何自动备份MySQL数据库
  8. Nginx 过滤模块
  9. ajax成功跨域_自己写的
  10. 长城汽车旗下品牌“欧拉好猫”涉嫌虚假宣传 曾召回部分欧拉汽车
  11. 51nod 1092 回文字符串【LCS】
  12. mysql 查询索引
  13. leetCode 203. Remove Linked List Elements 链表
  14. python程序员专用壁纸_Python程序员必用的电脑桌面
  15. mysql 序列号_mysql 序列号生成器(转)
  16. 推荐5款纯净好用的良心浏览器,实现浏览自由
  17. Informatic学习总结_day03
  18. python 赚钱 知乎_2020年,小红书、知乎与B站谁能赚钱?
  19. 小武与GPU与pytorch的bug 还有反向传播
  20. Node.js基础(二)-- 模块化、npm与包

热门文章

  1. 计算机代码rsi是什么意思,自编RSI指标
  2. ERP系统开发需要多少钱?
  3. docker安装和基础知识要点
  4. 小程序源码:花体字转换器-多玩法安装简单
  5. You have enabled checking of packages via GPG keys. This is a good thing.  However, you do not have
  6. 血与荣耀(第四章-命令的镣铐)
  7. 云南农业大学matlab,云南农业大学关于公第七届学生科技.doc
  8. [转帖]从磁悬浮到奔驰公交——中国式发展的糜烂
  9. 浅聊古代————汉朝
  10. java宠物王国_宠物王国单机版