前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 快速起步

Elasticsearch 快速起步

作者头像
dys
发布2018-04-04 11:57:21
9420
发布2018-04-04 11:57:21
举报
文章被收录于专栏:性能与架构

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环境

下载

代码语言:javascript
复制
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz

解压

代码语言:javascript
复制
tar -xvf elasticsearch-5.2.2.tar.gz

启动

代码语言:javascript
复制
cd elasticsearch-5.2.2/bin
./elasticsearch

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

4. 操作示例

列出所有 index

代码语言:javascript
复制
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

新建 index

新建一个名为 customer的 index

代码语言:javascript
复制
curl -XPUT 'localhost:9200/customer?pretty&pretty'

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

代码语言:javascript
复制
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

向 index 中添加一个 document

代码语言:javascript
复制
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 内容是

代码语言:javascript
复制
{
  "name": "John Doe"
}

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

根据 ID 取 document

代码语言:javascript
复制
curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

返回信息

代码语言:javascript
复制
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

删除 index

代码语言:javascript
复制
curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

之前的 customer 已经没有了

替换 document

先新建一个 document,ID 为 1

代码语言:javascript
复制
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' \
-H 'Content-Type: application/json' -d'
{
  "name": "华仔"
}
'

查看一下

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

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

代码语言:javascript
复制
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' \
-H 'Content-Type: application/json' -d'
{
  "name": "德华"
}
'

再查看一下

代码语言:javascript
复制
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

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

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

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

查看一下 document

代码语言:javascript
复制
curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

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

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

删除 document

代码语言:javascript
复制
curl -XDELETE 'localhost:9200/customer/external/1?pretty&pretty'

5. 小结

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JAVA高性能架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 简介
  • 2. 基本概念
    • Cluster
      • Node
        • Index
          • Type
            • Document
            • 3. 安装
            • 4. 操作示例
              • 列出所有 index
                • 新建 index
                  • 向 index 中添加一个 document
                    • 删除 index
                      • 替换 document
                        • 修改 document 数据
                          • 删除 document
                          • 5. 小结
                          相关产品与服务
                          对象存储
                          对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档