Shards & Replicas (分片和副本)html
Shards:每一个索引有一个或多个分片,索引的数据被分配到各个分片上,至关于一桶水用了N个杯子装 Replicas:副本,备份分片。每一个主分片都有一个备份分片在另外一个节点上,此时容许挂掉任意一个节点。
DSL(Domain Specific Language):Elasticsearch 定义的查询语言
ES字段类型:https://blog.csdn.net/chengyu...node
Elasticsearch | 关系型数据库 | NOSQL数据库 |
---|---|---|
索引(index) | 数据库(database) | 数据库(database) |
文档(document) | 行(row) | 文档(document) |
字段(fields) | 字段(columns) | 字段(fields) |
ElasticSearch相较于传统数据库的缺陷
· 不支持事务性操做
· 读写延时(NTR)
· 不适合频繁的update等操做
· 安全性可靠性
Settings API: 获取索引设置正则表达式
GET es-index_*/_settings { "es-index_*": { "settings": { "index": { "mapping": { "ignore_malformed": "true" }, "refresh_interval": "10s", "translog": { "durability": "async" }, "max_result_window": "10000", "creation_date": "1551295476399", "requests": { "cache": { "enable": "true" } }, "unassigned": { "node_left": { "delayed_timeout": "6h" } }, "priority": "5", "number_of_replicas": "1", "uuid": "-JvfCJ3-TCaMMxqnOiOfNA", "version": { "created": "2030399" }, "codec": "best_compression", "routing": {}, "search": { "slowlog": { "threshold": { "fetch": { "warn": "1s", "trace": "200ms", "debug": "500ms", "info": "800ms" }, "query": { "warn": "10s", "trace": "500ms", "debug": "1s", "info": "5s" } } } }, "number_of_shards": "12", "merge": { "scheduler": { "max_thread_count": "1" } } }, "tribe": { "name": "olap" } } } }
Stats API: 获取索引统计信息(http://cwiki.apachecn.org/pag...)数据库
GET es-index_*/_stats { "_shards": { "total": 622, "successful": 622, "failed": 0 }, //返回的统计信息是索引级的聚合结果,具备primaries和total的聚合结果。其中primaries只是主分片的值,total是主分片和副本分片的累积值。 "_all": { "primaries": { "docs": { //文档和已删除文档(还没有合并的文档)的数量。注意,此值受刷新索引的影响。 "count": 2932357017, "deleted": 86610 }, "store": { //索引的大小。 "size_in_bytes": 2573317479532, }, "indexing": {}, //索引统计信息,能够用逗号分隔的type列表组合,以提供文档级统计信息。 "get": {}, // get api调用统计 "search": {}, // search api 调用统计 }, "total": { } } }
Search API(两种形式)apache
using a simple query string as a parameterapi
GET es-index_*/_search?q=eventid:OMGH5PageView
using a request body缓存
GET es-index_*/_search { "query": { "term": { "eventid": { "value": "OMGH5PageView" } } } }
Leaf Query Clause: 叶查询子句
Compound Query Clause: 复合查询子句安全
DSL查询上下文app
** 查询时,可先使用filter过滤操做过滤数据,而后使用query查询匹配数据async
查询结果字段过滤
fields:字段过滤
script_fields:可对原始数据进行计算
"fields": ["eh"], //仅返回eh字段 "script_fields": { "test": { "script": "doc['eh'].value*2" } } // 返回eh字段值*2的数据并命名为test字段
查询过滤:query
bool 组合过滤器
{ "bool" : { "must" : [], // 全部的语句都必须匹配,至关于SQL中的and "must_not" : [], // 全部的语句都不能匹配,至关于SQL中的not "should" : [], // 至少有一个语句要匹配,至关于SQL中的OR "filter" : [] || { "and": [], "or": [], "not": [], }, // } }
filtered过滤器
{ "filtered": { "query": {}, "filter": {} // 在filter中进行数据过滤,而后再去query中进行匹配 } }
match和term
match(模糊匹配):先检查字段类型是不是analyzed,若是是,则先分词,再去去匹配token;若是不是,则直接去匹配token。
term(精确匹配):直接去匹配token。
terms: 多项查询
{ terms : { user: ['tony', 'kitty' ] } }
range范围过滤
对于date类型字段的范围选择可使用 Date Math
{ "range" : { "born" : { "gte": "01/01/2012", "lte": "2013", "format": "dd/MM/yyyy||yyyy" } } } { "range" : { "timestamp" : { "gte": "now-6d/d", // Date Math "lte": "now/d", // Date Math "time_zone": "+08:00" // 时区 } } }
exists 该条记录是否存在某个字段
{ "exists" : { "field" : "user" } }
wildcard: 通配符查询(对分词进行匹配查询)
Note that this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow wildcard queries, a wildcard term should not start with one of the wildcards * or ?
wildcard查询性能较差,尽可能避免使用*或?开头来进行wildcard匹配
prefix: 前缀查询
regexp: 正则表达式查询
value带-的特殊处理
value带了-,则默认会被切词,致使搜索结果不许确。解决办法之一就是在字段那里加个.raw
term: {status:'pre-active'} => term: {status.raw: 'pre-active'}
GET es-index_*/_search { "fields" : ["eventid", "logtime"], "query": { "term": { "eventid": { "value": "OMGH5PageView" } } }, "sort": [ { "logtime": { "order": "asc" } } ] }
date_histogram
(和 histogram 同样)默认只会返回文档数目非零的 buckets。 即便 buckets
中没有文档咱们也想返回。能够经过设置两个额外参数来实现这种效果:
"min_doc_count" : 0, // 这个参数强制返回空 buckets。 "extended_bounds" : { // 强制返回全年 "min" : "2014-01-01", "max" : "2014-12-31" }
took: 查询返回的时间(单位:毫秒)time_out: 查询是否超时_shards: 描述查询分片的信息,包括:查询了多少分片,成功的分片数量,失败的分片数量等hits:搜索的结果total: 知足查询条件的文档数max_score: hits: 知足条件的文档_score: 文档的匹配程度