# Elasticsearch安装使用

## 一、ES简介

Elasticsearch简称ES，是一个开源的分布式、RESTful风格的搜索和数据分析引擎，它的底层是开源库Apache Lucene。ES在Apache Lucene封装了一层，使全文检索变得简单易用。

ES的特点如下：

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

## 二、ES的安装

从官网下载：<https://www.elastic.co/cn/downloads/elasticsearch>

解压后，执行：

```shell
cd elasticsearch-<version>
./bin/elasticsearch
```

启动成功后，ES就运行在本地的9200端口，打开：<http://localhost:9200/，如果能看到以下内容，说明启动成功，可以看到版本号是7.13.1。>

```json
{
    "name": "wangjundeMacBook-Pro.local",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "OBNQJ5cpT9mff6vvpvmCcw",
    "version": {
        "number": "7.13.1",
        "build_flavor": "default",
        "build_type": "tar",
        "build_hash": "9a7758028e4ea59bcab41c12004603c5a7dd84a9",
        "build_date": "2021-05-28T17:40:59.346932922Z",
        "build_snapshot": false,
        "lucene_version": "8.8.2",
        "minimum_wire_compatibility_version": "6.8.0",
        "minimum_index_compatibility_version": "6.0.0-beta1"
    },
    "tagline": "You Know, for Search"
}
```

## 三、Kibana的安装

Kibana是一个开源的分析和可视化平台，旨在与ES合作。Kibana提供搜索、查看和存储在ES索引中的数据进行交互的功能。开发人员可以轻松地执行高级数据分析，并在各种图表、表格和地图中可视化数据。

从官方下载：<https://www.elastic.co/cn/downloads/kibana>

解压后执行：

```shell
cd kibana-<version>
./bin/kabana
```

启动成功后，ES就运行在本地的5601端口，打开：<http://localhost:5601/，如果能看到正常的运行web页面，说明启动成功。>

## 四、ES的实践

实践之前先明确一下es的存储结构：

* index：索引，数据的顶层单位，可以理解为mysql中的一个数据库；
* document：index的单条记录成为document（文档），同一个index的document最好有相同的结构，有利于提高搜索效率，可以理解为mysql中的一行数据；
* type：document可以分组，比如一个index里面存储的是weather（天气）数据，这些数据可以按照城市分组也可以按照天气分组。(目前7.x版本一个index只能有一个type)，可以理解为mysql中的一个表。

### 4.1 数据增加

```shell
# 新建名为accounts的index
curl -X PUT 'localhost:9200/accounts'

# 新增数据,其中1为数据的id，可以为任意字符串
curl -H "Content-Type: application/json" -X PUT localhost:9200/accounts/doc/1 -d '
{
"body":"body1"
}'
# 新增数据,不指定id,es会自动生成一个id
curl -H "Content-Type: application/json" -X POST localhost:9200/accounts/doc -d '
{
"body":"body2"
}'
```

### 4.2 数据删除

```shell
# 删除index
curl -X DELETE 'localhost:9200/weather'
# 删除数据
curl -X DELETE localhost:9200/accounts/doc/1
```

### 4.3 数据查找

```shell
# 查询单条数据
curl 'localhost:9200/accounts/doc/1?pretty=true'
{
  "_index" : "accounts",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 10,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "body" : "body1"
  }
}
# 查询所有数据
curl 'localhost:9200/accounts/doc/_search?pretty=true' 
# 全文搜索
curl -H "Content-Type:application/json" 'localhost:9200/accounts/doc/_search?pretty=true'  -d '
{
  "query" : { "match" : { "body" : "body1" }}
}'
```

### 4.4 数据修改

```shell
# 和新增数据一样语法，新数据会覆盖老数据
curl -H "Content-Type: application/json" -X PUT localhost:9200/accounts/doc/1 -d '
{
"body":"body2"
}'
```

## 五、对比

### 5.1 与mysql的对比

1.概念差别对比

| ElasticSearch | Mysql         |
| ------------- | ------------- |
| index（索引）     | database（数据库） |
| Type（类型）      | Table（表）      |
| Document（文档）  | Row（行）        |
| Mapping       | Schema        |
| Fields（字段）    | Column（列）     |

2.查询速度，es采用倒排索引对于复杂查询es比mysq使用B+树的正排索引更有优势。

3.es适用于全文检索，日志分析，监控分析等场景。

> 参考：
>
> <https://developer.51cto.com/art/201904/594615.htm#topx>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jun-wang.gitbook.io/learnjava/ji-shu-xue-xi/web-zhong-jian-jian-xue-xi/elasticsearch-an-zhuang-shi-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
