一、Elasticsearch
Elasticsearch,高可扩展、开源、全文搜索和分析引擎。接近实时的存储、搜索和分析海量数据。一般作为底层引擎用于增强具有复杂搜索需求的应用。
适用场景:
case 1:onlineweb,Elasticsearch存储所有商品类目和清单,为客户提供搜索
case 2:收集日志或事务数据,对其进行分析挖掘。
Case3:价格报警平台,捕获vendor价格至Elasticsearch,倒排搜索使之匹配客户价格查询并将结果告知客户。
Case4:有数据分析/商业智能需求,需要快速探究、分析、可视化数据并响应大数据的ad-hoc查询。利用Elasticsearch存储数据并利用Kibana构建客户数据仪表盘,并利用Elasticsearch的汇总功能实现复杂的BI查询
基本概念
near realtime
Elasticsearch是一个近实时的搜索平台,这意味着一份文档从index到可以被搜索基本在秒级。
Cluster
由uniquename标识,在所有节点上持有整个数据并提供联邦式的索引和搜索能力。
Node
集群Cluster中的一台服务器,存储数据并参与集群的索引和搜索。在node启动(startup)时,随机赋予一个marvel字符作为其唯一标识。Node可通过配置ClusterName加入一个Cluster。
Index
具有相似特征的一组文档,比如为customerdata或者productcatalog建立的索引。索引由其name(全部小写)唯一标识,用于在文档上实现索引、搜索、更新和删除操作。
Type
在一个Index中,可以定义一个或多个Type,其为索引的一个逻辑类目或分区。一般的,为文档建立的Type包含一组普通field,比如为日志建立一个索引,在其中,为userdata、blogdata、commentsdata分别定义一个Type。
Document
可以被索引的一个基本单位,JSON格式。在一个index/type内,你可以存储尽可能多的文档。
Shards & replicas
一个index可以存储海量数据以致超过单个节点的硬件限制,比如单个索引1亿份文档,则达到1TB,为此将使得搜索响应变慢。为此,分割index至多份,称为shard。
创建Index时,可以定义shard数,每个shard自身为fully-functional、独立的Index,可以Host在集群中的任何节点上。Sharding的重要体现在两方面:线性split/scale,在shards上分布和并行化操作以提高性能和吞吐量。shard分布机制和文档如何聚集响应搜索完全由Elasticsearch管理,对User透明。Elasticsearch的Failover机制处理一个shard/node离线,使每一索引shard有若干copy,称为replicashards。其重要性体现在两方面:提供高可用性,扩展搜索volumn/吞吐量以使得在所有replica上并行执行。
二、Rest API
第一部分: 索引文档
Elasticsearch提供了丰富、强大的REST API便于与其集群交互,实现如下操作:
检查你的集群、节点以及index的健康、状态和统计信息。
管理集群、节点和索引数据、元数据。
完成index上的CRUD和搜索操作。
执行高级搜索操作比如分页、排序、过滤、脚本化、faceting、聚集等。
1. 集群health检查:curl'localhost:9200/_cat/health?v',使用curl和_catAPI。
2. 获取集群中所有节点:curl'localhost:9200/_cat/nodes?v'
3.获取索引列表:curl'localhost:9200/_cat/indices?v'
4. 创建索引:curl-XPUT 'localhost:9200/customer?pretty',利用PUT,并由pretty结尾以返回pretty-print的JSON响应。
5. 为index文档,首先需要建立type,使之属于这个type。比如,索引一份文档至customer索引、type为“external”,ID为1:
curl -XPUT'localhost:9200/customer/external/1?pretty' -d '
{ "name": "John Doe" }'
6. 检索我们索引的文档:curl-XGET 'localhost:9200/customer/external/1?pretty'
7. 删除索引:curl-XDELETE 'localhost:9200/customer?pretty'
8. 索引/替换文档:以同样的index、type、ID索引一份不同的文档,则替换为新文档内容。
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d ' { "name": "Jane Doe" }'
以同样的index、type、不同ID索引新文档,则已存在的文档不受影响。
9.修改文档:Elasticsearch并不在hood上直接修改,而是先删除旧文档再index新文档。
如下修改ID为1的文档,将其name修改为“JaneDoe”
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d ' { "doc": { "name": "Jane Doe" } }'
如下修改name,并增加age:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d ' { "doc": { "name": "Jane Doe", "age": 20 } }'
如下利用script增加age:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d ' { "script" : "ctx._source.age += 5" }'
ctx._source
指正在修改的当前文档。
10. 删除文档
:curl -XDELETE 'localhost:9200/customer/external/2?pretty'
11. 删除满足查询条件的文档,
/_query
明确使用
APIdelete-by-query
:
curl -XDELETE'localhost:9200/customer/external/_query?pretty' -d '
{ "query": { "match": { "name": "John" } } }'
12. 批处理:通过
_bulkAPI
实现,按次序执行若干操作,快速且利用很少的网络传输。如果其中一个操作失败,会继续执行剩余操作。例如索引两个文档:
curl -XPOST'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
13. Load
数据:
curl-XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary@accounts.json
第二部分 搜索
1. search API
有两种方式执行搜索,
(1).
通过为
RESTrequest URI
传递搜索参数,
curl 'localhost:9200/bank/_search?q=*&pretty'
_search
使得用于
search
的
RESTAPI
可用,上述返回
index:bank
下的所有文档。
(2).
通过为
RESTrequest body
传递搜索参数。后一种方式允许你定义更复杂的搜索且定义可读性更高的
JSON
格式的搜索。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
实现方式
1
同样的搜索。
Elasticsearch
不维护任何
server-side
资源或
opencursors
,仅仅响应
request
。
2. Query
语言简介
JSON
格式、
domain-specificLanguage
,即
QueryDSL
。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
上述查询中
,match_all
为一个具体
index
搜索所有文档;
size:
仅返回第一份文档,默认为
10
。排序功能如下所示:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
选取文档若干
fields
,并非全部
field
,由
_source
指定:
curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{ "query": { "match_all": {} }, "_source": ["account_number", "balance"] }'
具体化
query
需要满足的字段条件:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
bool query
,利用
boolean
逻辑组合若干小查询,形成大查询:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must/should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
must:"mill"and "lane";should:"mill"or "lane"。
Filters
_score:Documentscore,评价文档匹配一个具体query的程度,其值越高,匹配度越大。所有查询均触发对score的计算,如果我们不需要它,则可以使用filter。Filter可以优化查询使之加快处于如下两个原因:Filter不需要计算score,Filter可缓存至内存允许重复搜索。如下rangefilter:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }'
Aggregations
执行搜索,返回hits和搜索结果;设置size=0,则不返回hits。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" } } } }'