Elasticsearch 是一个高可靠开源全文搜索与分析引擎,可以让我们实时存储、查询、分析海量数据
具有安装方便、稳定可靠、快速、实时等特点,是当前流行的企业级搜索引擎
应用场景示例:
一组 server 集合构成的集群,这些 server 一起存在数据、提供索引与搜索功能
cluster 是有名字的,默认是 "elasticsearch",这个名字很重要,一个 server 只能属于一个 cluster,就是根据这个名字建立关系的,所以名字要规划好,例如 logging-dev、logging-prod 分别作为开发、产品环境下的cluster
cluster 中的一个 server 就是 node 节点,node 也有名字,默认是一个 UUID
有相似属性文档的集合,例如客户数据 index、产品分类 index、订单数据 index
index 使用一个全小写的 name 作为标识
一个 index 内可以定义一个或多个 type,type 是 index 中的逻辑分类
例如,一个博客系统,把所有数据都存储在一个 index 中,其中就可以有多个 type,用户数据 type、博客数据 type、评论数据 type
是可被索引的基本信息单元,例如,一个客户的 document、一个产品的 document、一条订单的 document
document 使用 JSON 来描述,在一个 index 或者 type 中,可以保存任意数量的 document
一个 document 必须属于 index 中的某个 type
前提:安装好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
curl -XGET 'localhost:9200/_cat/indices?v&pretty'
新建一个名为 customer
的 index
curl -XPUT 'localhost:9200/customer?pretty&pretty'
列出所有 index,看是否添加成功
curl -XGET 'localhost:9200/_cat/indices?v&pretty'
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"
}
}
curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'
之前的 customer 已经没有了
先新建一个 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" : "德华"
}
}
修改上面 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"
}
'
curl -XDELETE 'localhost:9200/customer/external/1?pretty&pretty'
从上面的实践中可以发现 Elasticsearch 非常方便,没有复杂的安装过程,操作是 REST 风格,简单易懂,概念也很好理解