Elasticsearch 入门
Elasticsearch 是一个高度可扩展且开源的全文检索和分析引擎
产品数据
收集日志或交易数据
价格警告平台- reverse-search(Percolator)(反向搜索(过滤器))
分析/商业智能
基本概念
Near Realtime(NRT 近实时)
索引一个文档开始直到它可以被查询时会有轻微的延迟时间
Cluster(集群)
名字 默认为 “elasticsearch”
节点加入集群通过集群名字
命名尽量有含义 例如:logging-dev production
Node(节点)
node(节点)是一个单独的服务器
Index(索引) 一个消费者数据的索引,一个产品目录的索引,和另一个是订单数据的索引
Type(类型) 一个用户数据类型,博客数据类型,和评论数据类型
document(文档) 是索引信息的基本单位
Shards & Replicas(分片 & 副本) 默认情况下,Elasticsearch 中的每个索引分配了 5 个主分片和 1 个副本
把 Index(索引)拆分到多个 Shard(分片)
故障转移 设置一个或多个索引的 Shard 副本到所谓的副本分片
安装
TODO
搜索集群
集群健康 curl -XGET 'localhost:9200/_cat/health?v&pretty'
status: green,yellow,或者 red
Green 表示一切正常(集群功能齐全),
yellow 表示所有数据可用,但是有些副本尚未分配(集群功能齐全)
red 意味着由于某些原因有些数据不可用
获取我们集群的节点列表 curl -XGET 'localhost:9200/_cat/nodes?v&pretty'
查找所有索引.../indices
创建索引 curl -XPUT 'localhost:9200/customer?pretty&pretty'
索引名称:customer
末尾pretty:输出json格式
索引查询文档
存放数据 如果customer索引没有,自动创建
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -d'
{
"name": "John Doe"
}
响应:
{
"_index" : "customer",//索引
"_type" : "external",//类型
"_id" : "1",//id
"_version" : 1,
"result" : "created",
"_shards" : {//分片
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
查找数据
curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
{
"_index" : "customer",//索引
"_type" : "external",//类型
"_id" : "1",//id
"_version" : 1,//版本
"found" : true,//找到了
"_source" : { "name": "John Doe" }//文档类容
}
删除索引 curl -XDELETE 'localhost:9200/customer?pretty&pretty'
修改数据 -XPUT 如果已经存在则修改 -POST 不指定ID
更新文档 _update 实际是删除后重新建
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
{
"script" : "ctx._source.age += 5"//ctx._source 代表当前将被更新的源文档
}'
删除文档 curl -XDELETE 'localhost:9200/customer/external/2?pretty&pretty'
批处理
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -d'
{"index":{"_id":"1"}}
{"name": "John Doe"} //增加
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }//修改
{"delete":{"_id":"2"}} //删除
搜索数据
搜索API
curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty'
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}'
介绍查询语言
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{//默认找top10文档
"query": { "match_all": {} },//文档规则 {}全部
"sort": { "balance": { "order": "desc" } }//账户降序
"from": 10,//从第10个开始
"size": 10//总共10个
}'
执行查询
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{
"query": { "match_all": {} },
"query": { "match": { "account_number": 20 } }//账户编号为 20 的文档 :
"query": { "match": { "address": "mill" } }//所有在 address 中包含 term 为 “mill” 的账户
"query": { "match": { "address": "mill lane" } }//所有在 address 中包含了 term 为 “mill” 或 “lane” 的账户
"query": { "match_phrase": { "address": "mill lane" } }//在 address 中所有包含 phrase 为 “mill lane” 的账户
"_source": ["account_number", "balance"]//只获取结果的"account_number", "balance"
}'
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{//should 两个match包含一个 must是都包含 must_not 都不包含
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
执行过滤 filter
curl -XGET 'localhost:9200/bank/_search?pretty' -d' 20000 且小于或等于 30000
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
执行聚合
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{
"size": 0,
"aggs": {//统计
"group_by_state": {//按state分组
"terms": {
"field": "state.keyword"//state
}
}
}
}'
state先分组balance再平均
curl -XGET 'localhost:9200/bank/_search?pretty' -d'
{
"size": 0,
"aggs": {//先分组
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {//再平均
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'
按年龄段(20-29岁,30-39岁,和 40-49岁),然后按性别,最后获得每个年龄段,每个性别的平均账户余额
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [//范围分组
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}