乐趣区

Elastic Search 学习笔记

Reference

6.4 最新版英文:https://www.elastic.co/guide/…

中文:https://www.elastic.co/guide/…

5.4 中文:http://cwiki.apachecn.org/pag…

Defination
DSL(Domain Specific Language):Elasticsearch 定义的查询语言

ES 字段类型:https://blog.csdn.net/chengyu…
API

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(两种形式)

using a simple query string as a parameter
GET es-index_*/_search?q=eventid:OMGH5PageView

using a request body
GET es-index_*/_search
{
“query”: {
“term”: {
“eventid”: {
“value”: “OMGH5PageView”
}
}
}
}

Query DSL
Leaf Query Clause: 叶查询子句 Compound Query Clause: 复合查询子句
DSL 查询上下文

query context 在查询上下文中,回答的问题是:How well does this document match this query clause? 除了判断一条数据记录 (document) 是否匹配查询条件以外,还要计算其相对于其他记录的匹配程度,通过_score 进行记录。
filter context** 在查询上下文中,回答的问题是:Does this document match this query clause? 仅判断 document 是否匹配,不计算_score 一般用来过滤结构化数据, e.g. timestamp 是否在 2017-2018 范围内,status 是否是 published 频繁使用的过滤器会被 Elasticsearch 自动缓存,可提高性能

** 查询时,可先使用 filter 过滤操作过滤数据,然后使用 query 查询匹配数据
查询结果字段过滤
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” : {}, //
}
}

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:正则表达式查询
Tips
value 带 - 的特殊处理
value 带了 -,则默认会被切词,导致搜索结果不准确。解决办法之一就是在字段那里加个.raw
term: {status:’pre-active’} => term: {status.raw: ‘pre-active’}

sort
GET es-index_*/_search
{
“fields” : [“eventid”, “logtime”],
“query”: {
“term”: {
“eventid”: {
“value”: “OMGH5PageView”
}
}
},
“sort”: [
{
“logtime”: {
“order”: “asc”
}
}
]
}

聚合 aggregation
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: 文档的匹配程度

退出移动版