ES在处理大数据搜索方面拥有关系型数据库不可比拟的速度优点。这篇不是什么专业的ES指南,也不是ES分析,这里只是我在使用ES中遇到的一些问题,一些搜索方式。由于ES的文档和API查询起来比较困难,所以本身在查询翻译文档时老是耗费不少时间,因而就想把本身遇到过的问题和搜索记录下来,给本身概括一个简单的经常使用的ES使用指南。html
ES中文档是以key-value的json数据包形式存储的,它有三个元数据。mysql
数据被存储和索引在分片「shards」中,索引只是把一个或多个分片组合在一块儿的逻辑空间。这些由ES实现。使用者无需关心。
ES为对字段类型进行猜想,动态生成了字段和类型的映射关系。sql
- date 类型的字段和 string 类型的字段的索引方式不一样的,搜索的结果也是不一样的。每一种数据类型都以不一样的方式进行索引。
GET /user/_mapping/user --Response { "user": { "mappings": { "user": { "properties": { "height": { "type": "long" }, "nickname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, } } } } }
确切值是肯定的,表示date或number等;
全文文本指 非结构化数据,如这是一个文本
值 | 解释 |
---|---|
analyzed | 首先分析这个字符串,而后索引。(以全文形式索引此字段) |
not_analyzed | 索引这个字段,使之能够被索引,但索引内容和指定值同样 |
no | 不索引这个字段 |
string 类型字段默认值是 analyzed。若是想映射字段为确切值,则设置为 not_analyzed
{ "nickname": { "type": "string", "index": "not_analyzed" } }
GET /_search --Response { "took": 7, // 整个搜索的请求花费的时间(毫秒) "timed_out": false, // 是否请求超时 "_shards": { "total": 25, // 参与查询的分片 "successful": 25, // 成功的分片 "skipped": 0, // 跳过的 "failed": 0 // 失败的 }, "hits": { "total": 1291, // 匹配到到文档数量 "max_score": 1, // 查询结果 _score 中的最大值 "hits": [ { "_index": "feed", "_type": "feed", "_id": "1JNC42oB07Tkhuy89JSd", "_score": 1, "_source": { } } ] } }
建立一个名为 megacorp 的索引
PUT /user/ --Response { "megacorp": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1558956839146", // 建立时间(微秒时间戳) "number_of_shards": "5", "number_of_replicas": "1", "uuid": "pGBqNmxrR1S8I7_jAYdvBA", // 惟一id "version": { "created": "6060199" }, "provided_name": "megacorp" } } } }
PUT /user/user/1001 --Body { "nickname": "你有病啊", "height": 180, "expect": "我喜欢看_书", "tags": ["大学党", "求偶遇"] } --Response { "_index": "user", "_type": "user", "_id": "10001", "_version": 1, // 版本号,ES中每一个文档都有版本号,每当文档变化(包括删除)_version会增长 "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
搜索 height 大于 30 且 nickname 为 threads 的文档
GET /user/user/_search --Body { "query": { "filtered": { "filter": { "range":{ "height":{ "gt": 170 } } }, "query": { "match": { "nickname": "threads" } } } } }
结构化查询须要传递
query
参数。
GET /_search { "query": 子查询 } # 子查询 { "query_name": { "argument": value,... } } # 子查询指向特定字段 { "query_name": { "field_name": { "argument": value,... } } }
查询子句能够合并简单的子句为一个复杂的查询语句。如:数据库
- 简单子句用以在将查询字符串与一个字段(或多个字段)进行比较。
- 复合子句用以合并其余的子句。
GET /user/user/_search --Body { "query": { "bool": { "must": { // must:必须知足该子句的条件 "match": { "nickname": "threads" } }, "must_not": { // must_not: 必须不知足该子句条件 "match": { "height": 170 } }, "should": [ // should: 结果可能知足该数组内的条件 { "match": { "nickname": "threads" } }, { "match": { "expect": "我喜欢唱歌、跳舞、打游戏" } } ] } } } --Response { "hits": { "total": 1, "max_score": 0.91862875, "hits": [ { "_index": "user", "_type": "user", "_id": "98047", "_score": 0.91862875, "_source": { "nickname": "threads", "height": 175, "expect": "我喜欢唱歌、跳舞、打游戏", "tags": [ "工做党", "吃鸡" ] } } ] } }
GET /user/user/_search --Body { "query":{ "match":{ "expect": "打游戏" } } } --Response { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 0.8630463, "hits": [ { "_index": "user", "_type": "user", "_id": "98047", "_score": 0.8630463, // 匹配分 "_source": { "nickname": "threads", "height": 175, "expect": "我喜欢唱歌、跳舞、打游戏", "tags": [ "工做党", "吃鸡" ] } }, { "_index": "user", "_type": "user", "_id": "94302", "_score": 0.55900055, "_source": { "nickname": "摇了摇头", "height": 173, "expect": "我喜欢rap、跳舞、打游戏", "tags": [ "工做党", "吃饭" ] } }, { "_index": "user", "_type": "user", "_id": "91031", "_score": 0.53543615, "_source": { "nickname": "你有病啊", "height": 180, "expect": "我喜欢学习、逛街、打游戏", "tags": [ "大学党", "求偶遇" ] } } ] } }
ES根据结果相关性评分来对结果集进行排序。即文档与查询条件的匹配程度
确切的匹配若干单词或短语。
GET /user/user/_search --Body { "query": { "match_phrase": { "expect": "学习" } } } --Response { "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.357075, "hits": [ { "_index": "user", "_type": "user", "_id": "91031", "_score": 1.357075, "_source": { "nickname": "你有病啊", "height": 180, "expect": "我喜欢学习、逛街、打游戏", "tags": [ "大学党", "求偶遇" ] } } ] } }
一条过滤语句会询问每一个文档的字段值是否包含着特定值:json
- 是否
created
的日期范围在xxx
到xxx
- 是否
expect
包含学习
- 是否
location
字段中的地理位置与目标点相距不超过xxkm
使用过滤语句获得的结果集,快速匹配运算并存入内存是十分方便的,每一个文档仅须要1个字节。
查询语句不只要查询相匹配的文档,还须要计算每一个文档的相关性,因此通常来讲查询语句要比过滤语句更耗时,而且查询结果也不可缓存。
数组
原则上来讲:使用查询语句作全文文本搜索或其余须要进行相关性评分的时候,剩下的所有使用过滤语句。
缓存
GET /user/user/_search --Body { "query": { "match_phrase": { "expect": "学习" } }, "highlight": { "fields": { "expect": {} } } }
GET /user/user/_search --Body { "query": { "match_all": { } }, "size": 1000, // 返回的数据集大小 "sort": { "nickname.keyword": { // 按nickname排序 "order": "desc" }, "height": { // 按height排序 "order": "desc" } } }
GET /user/user/_search --Body { "from": 2, // 同mysql 的 offset "size": 1 // 同mysql 的 limit }
GET /user/user/_search --Body { "aggs": { "all_tags": { "terms": { "field": "tags" // 这种分词对于中文很不友好,会把“学习”分为“学”,“习” "field": "tags.keyword" // 5.x后的ES,使用这种写法能够完美分词 }, "aggs": { "avg_height": { "avg": { "field": "height" } } } } }
term
过滤term
主要用于精确匹配哪些值,好比数字,日期,布尔值或not_analyzed
的字符串
{"term": {"height": 175}} // height值为175 (number类型) {"term": {"date": "2014-09-01"}} // date值为2014-09-01(date类型) {"term": {"public": true}} // public值为true(布尔类型) {"term": {"nickname": "threads"}} // nickname值为threads(full_text类型)
terms
过滤terms 跟 term 同样,但 terms 容许指定多个匹配条件。若是某个字段指定了多个值,那么文档须要一块儿去作匹配文档。
// 匹配 nickname 为 threads 或 摇了摇头 的结果(使用keyword关键词匹配中文) { "query": { "terms": { "nickname.keyword": ["threads", "摇了摇头"] } } }
range
过滤
range
过滤是按照指定范围查找数据。
{ "query": {"range":{"height":{"gte": 150, "lt": 180}}} } // gt: 大于 // gte: 大于等于 // lt: 小于 // lte: 小于等于
exists
和 missing
过滤exists
和missing
过滤能够用于查找文档中是否包含指定字段或没有某个字段,相似于SQL语句中的IS_NULL
条件。
// 查询存在 nickname 字段的结果 {"exists": {"field": "nickname"}}
bool
过滤bool
过滤能够用来合并多个过滤条件查询结果的布尔逻辑:数据结构
must
:多个查询条件的彻底匹配,至关于 and
must_not
: 多个查询条件的相反匹配,至关于 not
should
:至少有一个查询条件匹配,至关于 or
{ "bool": { "must": {"term": {"nickname": "threads"}}, "must_not": {"term": {"height": 165}}, "should": [ {"term": {"height": 175}}, {"term": {"nickname": "threads"}}, ] } }
match_all
查询使用 match_all
能够查询到因此文档,是没有查询条件下的默认语句。(经常使用于合并过滤条件)app
{ "match_all":{} }
match
查询是一个标准查询,无论你须要全文本查询仍是精确查询基本上都要用到它。elasticsearch
{ "match": {"nickname": "threads"} }
multi_match
查询multi_match
查询容许你作 match 查询的基础上同时搜索多个字段:
// 搜索 nickname 或 expect 包含 threads 的结果 { "multi_match": { "query": "threads", "fields": ["nickname", "expect"] } }
bool
查询若是bool
查询下没有must
子句,那至少应该有一个should
子句。可是若是有must
子句,那么没有should
子句也能够进行查询。
{ "query": { "bool": { "must": { "multi_match": {"query": "学习", "fields": ["nickname", "expect"]} }, "must_not": { "match": {"height": 175} }, "should": [ {"match": {"nickname": "threads"}} ] } } }
filter
带过滤的查询使用filter
来同时使一个语句中包含查询
和过滤
// nickname 为 threads 的结果,并在此结果集中筛选出 height 为 175 的结果 { "query": { "bool": { "must": {"match": {"nickname": "threads"}}, "filter": {"term": {"height":175}} } } }
GET /user/user/_validate/query { "query": { "match_all": {"nickname": "threads"} } } --Response { "valid": false, "error": "ParsingException[[4:13] [match_all] unknown field [nickname], parser not found]; nested: XContentParseException[[4:13] [match_all] unknown field [nickname], parser not found];; org.elasticsearch.common.xcontent.XContentParseException: [4:13] [match_all] unknown field [nickname], parser not found" }
GET /user/user/_validate/query { "query": { "match_all": {"nickname": "threads"} } } --Response { "valid": false, "error": "ParsingException[[4:13] [match_all] unknown field [nickname], parser not found]; nested: XContentParseException[[4:13] [match_all] unknown field [nickname], parser not found];; org.elasticsearch.common.xcontent.XContentParseException: [4:13] [match_all] unknown field [nickname], parser not found" } // 经过返回能够看出,验证结果是非法的
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead." } ], ... }, "status": 400 }
5.x后对排序,聚合这些操做用单独的数据结构(fielddata)缓存到内存里了,须要单独开启。
开启fielddata
PUT user/_mapping/user/ --Body { "properties": { "tags": { "type": "text", "fielddata": true } } }