Elasticsearch 快速起步

时间:2022-05-07
本文章向大家介绍Elasticsearch 快速起步,主要内容包括1. 简介、2. 基本概念、Node、Index、Type、Document、3. 安装、4. 操作示例、新建 index、向 index 中添加一个 document、删除 index、替换 document、修改 document 数据、删除 document、5. 小结、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

1. 简介

Elasticsearch 是一个高可靠开源全文搜索与分析引擎,可以让我们实时存储、查询、分析海量数据

具有安装方便、稳定可靠、快速、实时等特点,是当前流行的企业级搜索引擎

应用场景示例:

  • 在线商店中的产品搜索,可以使用 Elasticsearch 来存储产品信息,让客户快速的搜索,可以方便的实现搜索时的自动完成功能
  • 数据搜集,进行趋势分析、统计、监控异常现象 等等,使用 Logstash 采集数据,存储到 Elasticsearch 中,然后就可以搜索、聚合自己感兴趣的内容

2. 基本概念

Cluster

一组 server 集合构成的集群,这些 server 一起存在数据、提供索引与搜索功能

cluster 是有名字的,默认是 "elasticsearch",这个名字很重要,一个 server 只能属于一个 cluster,就是根据这个名字建立关系的,所以名字要规划好,例如 logging-dev、logging-prod 分别作为开发、产品环境下的cluster

Node

cluster 中的一个 server 就是 node 节点,node 也有名字,默认是一个 UUID

Index

有相似属性文档的集合,例如客户数据 index、产品分类 index、订单数据 index

index 使用一个全小写的 name 作为标识

Type

一个 index 内可以定义一个或多个 type,type 是 index 中的逻辑分类

例如,一个博客系统,把所有数据都存储在一个 index 中,其中就可以有多个 type,用户数据 type、博客数据 type、评论数据 type

Document

是可被索引的基本信息单元,例如,一个客户的 document、一个产品的 document、一条订单的 document

document 使用 JSON 来描述,在一个 index 或者 type 中,可以保存任意数量的 document

一个 document 必须属于 index 中的某个 type

3. 安装

前提:安装好JAVA环境

下载

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz

解压

tar -xvf elasticsearch-5.2.2.tar.gz

启动

cd elasticsearch-5.2.2/bin
./elasticsearch

会输出一系列日志信息,启动完成后会打印出 started

4. 操作示例

列出所有 index

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

新建 index

新建一个名为 customer的 index

curl -XPUT 'localhost:9200/customer?pretty&pretty'

列出所有 index,看是否添加成功

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

向 index 中添加一个 document

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

意思是添加到 customer这个 index 中的 external type,ID为 1

document 内容是

{
  "name": "John Doe"
}

Elasticsearch 并不需要在插入 document 之前先创建好 index,上面例子中,如果 customer 这个 index 不存在,会自动创建

根据 ID 取 document

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

返回信息

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

删除 index

curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

之前的 customer 已经没有了

替换 document

先新建一个 document,ID 为 1

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' 
-H 'Content-Type: application/json' -d'
{
  "name": "华仔"
}
'

查看一下

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "华仔"
  }
}

替换成新的 document,同样指定ID为 1 即可

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' 
-H 'Content-Type: application/json' -d'
{
  "name": "德华"
}
'

再查看一下

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "name" : "德华"
  }
}

修改 document 数据

修改上面 ID 为 1 的 document 的 name

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "学友" }
}
'

修改 name 同时添加一个 age 属性

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "富城", "age": 20 }
}
'

查看一下 document

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

修改数据时还可以使用一些简单的脚本,例如 给 age 增加 5 岁

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'

删除 document

curl -XDELETE 'localhost:9200/customer/external/1?pretty&pretty'

5. 小结

从上面的实践中可以发现 Elasticsearch 非常方便,没有复杂的安装过程,操作是 REST 风格,简单易懂,概念也很好理解