@
目录
Elasticsearch是一款开源的分布式搜索引擎,基于 JSON 开发而来,具有 RESTful 风格,基于 Apache Lucene 的基础上开发而成的
引用官网的说法:
引用官网的说法,Elasticsearch的用途:
Elasticsearch 的用途是什么?
环境准备:
相关工具软件:
docker入门博客可以参考我的docker系统博客专栏: 链接
docker镜像搜索:
docker search elasticsearch
需要加上版本,不加版本默认是laster(最新)版本,貌似没提供laster版本
docker pull elasticsearch:7.2.0
查看所有镜像:
docker images
运行docker镜像:
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d --name ES01 elasticsearch:7.2.0
linux内用curl访问:
curl http://localhost:9200
浏览器直接访问也是可以的: http://your_ip_addr:9200
ps:详情可以参考官方文档: Install Elasticsearch with Docker
| 目录 | 配置文件 | 描述 |
|---|---|---|
| bin | 脚本文件,包括启动 Elasticsearch、安装插件,运行统计数据等。 | |
| config | elasticsearch.yml | 集群配置文件 |
| JDK | Java 运行环境 | |
| data | path.data | 数据文件 |
| lib | Java 类库 | |
| logs | path.logs | 日志文件 |
| modules | 包含所有 ES 模块 | |
| plugins | 包含所有已安装插件 |
ps:Elasticsearch也有linux版和window版,本博客不做介绍
cat 命令可以帮助开发者快速查询 Elasticsearch 的相关信息
_cat 参数
_cat 参数可以查看支持的命令
[root@localhost ~]# curl localhost:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
?v 参数
?v 参数,来显示详细的信息
[root@localhost ~]# curl localhost:9200/_cat/master?v id host ip node 8x63m-D8Q2CP4xRbq7rEFA 172.17.0.2 172.17.0.2 7610b4e6e11b
其它常用命令:
help参数
help 参数,来输出可以显示的列
[root@localhost ~]# curl localhost:9200/_cat/master?help id | | node id host | h | host name ip | | ip address node | n | node name
h参数
h 参数,可以指定输出的字段
[root@localhost ~]# curl localhost:9200/_cat/master?h=host,ip,node 172.17.0.2 172.17.0.2 7610b4e6e11b
ElasticSearch把数据存放到一个或者多个索引(indices)中。如果用关系型数据库模型对比,索引(index)的地位与数据库实例(database)相当,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。
如果一个索引(index)有多个文档的情况,可以根据类型(type)进行归类,如果用关系型数据库模型对比,索引(index)的地位与数据表(table)相当。文档类型使得同一个索引中在存储结构不同文档时,只需要依据文档类型就可以找到对应的参数映射(Mapping)信息
文档(Document)由一个或者多个域(Field)组成,每个域(Field)由一个域名(此域名非彼域名)和一个或者多个值组成(有多个值的值称为多值域(multi-valued))
如果用关系型数据库模型对比,域(Field)的地位与数据库模型中的列(column)相当
每台服务器可以运行多个 Elastic 实例,单独一个ElasticSearch服务器实例称为一个节点
集群是多个ElasticSearch节点的集合,Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。
集群能够存储超出单机容量的信息。为了实现这种需求,ElasticSearch把数据分发到多个存储Lucene索引的物理机上。这些Lucene索引称为分片索引,这个分发的过程称为索引分片(Sharding)。在ElasticSearch集群中,索引分片(Sharding)是自动完成的,而且所有分片索引(Shard)是作为一个整体呈现给用户的
索引副本(Replica)机制的的思路很简单:为索引分片创建一份新的拷贝,它可以像原来的主分片一样处理用户搜索请求
在运行的过程中,ElasticSearch会收集集群的状态、索引的参数等信息。这些数据被存储在Gateway中。
下面用一张表格简单记录对比:
| Elasticsearch | 关系数据库 |
|---|---|
| 索引 (Index) | 数据库(Database) |
| 类型(Type) | 数据表(Table) |
| 文档(Document) | 表中记录/数据行(Row) |
| 域(Field) | 数据列(Column) |
| 参数映射(Mapping) | 模式(Schema) |
一个 ElasticSearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多
个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个域
Elasticsearch是支持RESTFul API格式的,所以先补充一些RESTFul API知识:
GET 请求:获取服务器中的对象
POST 请求:在服务器上更新对象
PUT 请求:在服务器上创建对象
DELETE 请求:删除服务器中的对象
HEAD 请求:仅仅用于获取对象的基础信息
查询所有的索引:
curl -X GET 'http://localhost:9200/_cat/indices?v'
ok,进行索引实践,建议以官网对应版本文档为准,本博客参考7.2版本的,链接: https://www.elastic.co/guide/en/elasticsearch/reference/7.2/docs.html
[root@localhost ~]# curl -H "Content-Type:application/json" -X PUT localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"123"}'
返回json:
{
"_index":"company",
"_type":"employees",
"_id":"1",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":0,
"_primary_term":1
}
[root@localhost ~]# curl -X GET localhost:9200/company/employees/1?pretty=true
{
"_index" : "company",
"_type" : "employees",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id":1,
"name":"admin",
"password":"123"
}
}
[root@localhost ~]# curl -X DELETE localhost:9200/company/employees/1
{
"_index":"company",
"_type":"employees",
"_id":"1",
"_version":2,
"result":"deleted",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":1,
"_primary_term":1
}
curl -H "Content-Type:application/json" -X POST localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"111"}'
{
"_index":"company",
"_type":"employees",
"_id":"1",
"_version":2,
"result":"updated",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":3,
"_primary_term":1
}
实验环境:
maven配置:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>springboot-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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.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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.1版本,只要指定uris,旧版本要配置cluster-nodes和cluster-name
spring:
elasticsearch:
rest:
uris: 192.168.7.96:9200
指定index和type
package com.example.springboot.elasticsearch.bean;
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;
/**
* <pre>
* Employee
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/07/16 17:02 修改内容:
* </pre>
*/
@Data
@Document(indexName = "company",type = "employees",shards = 5,replicas = 1)
public class Employee implements Serializable {
@Id
private Long id;
@Field(type = FieldType.Text)
private String name;
@Field(type = FieldType.Text)
private String password;
@Override
public String toString() {
return "Employee{" +
"id='" + id + '/'' +
", name='" + name + '/'' +
", password='" + password + '/'' +
'}';
}
}
实现ElasticsearchRepository
package com.example.springboot.elasticsearch.repository;
import com.example.springboot.elasticsearch.bean.Employee;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* <pre>
* EmployeeRepository
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/07/16 17:24 修改内容:
* </pre>
*/
@Repository
public interface EmployeeRepository extends ElasticsearchRepository<Employee,Long>{
}
新增索引:
@Autowired
EmployeeRepository employeeRepository;
@Test
void createIndex(){
Employee employee = new Employee();
employee.setId(2L);
employee.setName("sys");
employee.setPassword("123");
employeeRepository.save(employee);
}
查询索引:
@Autowired
EmployeeRepository employeeRepository;
@Test
void selectIndex(){
Optional<Employee> employee = employeeRepository.findById(2L);
System.out.println(employee.get().toString());
}
代码例子下载: code download
附录 优质博客:
全文搜索引擎 Elasticsearch 入门教程