ES Restful API GET、POST、PUT、DELETE、HEAD含义:
1)GET:获取请求对象的当前状态。
2)POST:改变对象的当前状态。
3)PUT:建立一个对象。
4)DELETE:销毁对象。
5)HEAD:请求获取对象的基础信息。html
Mysql与Elasticsearch核心概念对比示意图git
PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] }
PUT /megacorp/employee { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] }
curl -XPOST localhost:9200/_bulk --data-binary @data.jsongithub
{"index":{"_index":"meterdata","_type":"autoData"}} {"Mfid ":1,"TData":172170,"TMoney":209,"HTime":"2016-05-17T08:03:00"} {"index":{"_index":"meterdata","_type":"autoData"}} {"Mfid ":1,"TData":172170,"TMoney":209,"HTime":"2016-05-17T08:04:00"} {"index":{"_index":"meterdata","_type":"autoData"}} {"Mfid ":1,"TData":172170,"TMoney":209,"HTime":"2016-05-17T08:05:00"} {"index":{"_index":"meterdata","_type":"autoData"}} {"Mfid ":1,"TData":172170,"TMoney":209,"HTime":"2016-05-17T08:06:00"} {"index":{"_index":"meterdata","_type":"autoData"}} {"Mfid ":1,"TData":172170,"TMoney":209,"HTime":"2016-05-17T08:07:00"}
当文档存在时,执行脚本;当文档不存在时,upsert中的内容就会插入到对应的文档中sql
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : { "inline": "ctx._source.counter += count", "params" : { "count" : 4 } }, "upsert" : { "counter" : 1 } }'
可使用Script对全部的文档执行更新操做,也可使用doc对部分文档执行更新,也可使用upsert对不存在的文档执行添加操做。shell
curl -XPUT localhost:9200/test/type1/1 -d '{ "counter" : 1, "tags" : ["red"] }'
curl -XPOST "localhost:9200/gengxin/update/1/_update?pretty" -d ' { "doc": {"job": "奋斗者"} }'
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : { "inline": "ctx._source.counter += count", "params" : { "count" : 4 } } }'
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "ctx._source.name_of_new_field = \"value_of_new_field\"" }'
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "ctx._source.remove(\"name_of_field\")" }'
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : { "inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"", "params" : { "tag" : "blue" } } }'
所有更新和部分更新区别?json
所有更新,是直接把以前的老数据,标记为删除状态,而后,再添加一条更新的。 api
部分更新,只是修改某个字段。数组
参考:缓存
http://www.cnblogs.com/shihuc/p/5978078.html网络
http://www.cnblogs.com/xing901022/p/5330778.html
curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
若是在索引的时候提供了路由,那么删除的时候,也须要指定相应的路由:
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1?routing=kimchy'
上面的例子中,想要删除id为1的索引,会经过固定的路由查找文档。若是路由不正确,可能查不到相关的文档。对于某种状况,须要使用_routing参数,可是却没有任何的值,那么删除请求会广播到每一个分片,执行删除操做。
若是文档存在,es会返回200 ok的状态码,found属性值为true,_version属性的值+1。
若是文档不存在,es会返回404 Not Found的状态码,found属性值为false,可是_version属性的值依然会+1,这个就是内部管理的一部分,它保证了咱们在多个节点间的不一样操做的顺序都被正确标记了。
ES的删除操做,也是不会当即生效,跟更新操做相似。只是会被标记为已删除状态,ES后期会自动删除。
比如,你删除的操做一步一步累积,当达到它上限时,等你删除几十条数据后,ES我一次性删除,这样能够节省磁盘IO。
参考:
http://www.cnblogs.com/zlslch/p/6421648.html
http://www.cnblogs.com/xing901022/archive/2016/03/26/5321659.html
(1)查询上下文:查询操做不只仅会进行查询,还会计算分值,用于肯定相关度;
(2)过滤器上下文:查询操做仅判断是否知足查询条件,不会计算得分,查询的结果能够被缓存。
参考:http://www.cnblogs.com/xing901022/p/4975931.html
轻量级搜索,查询字符串(query string)
GET /megacorp/employee/_search?q=last_name:Smith
表明彻底匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇(若是为中文,默认当个字为一个索引,只能搜索到单个字)
POST /megacorp/employee/_search { "query": { "term": { "last_name": "Smith" } } }
terms 跟 term 有点相似,但 terms 容许指定多个匹配条件。 若是某个字段指定了多个值,那么文档须要一块儿去作匹配:
POST /megacorp/employee/_search { "query": { "terms": { "last_name": ["Bob","Smith"] } } }
容许咱们按照指定范围查找一批数据
POST /megacorp/employee/_search { "query": { "range": { "age": { "gt": 18 } } } }
能够用于查找文档中是否包含指定字段或没有某个字段,相似于SQL语句中的IS_NULL条件.
POST /megacorp/employee/_search { "query": { "exists": { "field": "title" } } }
使用bool
过滤器来合并多个过滤器以实现and
,or
和not
逻辑。should知足的匹配度更高。must
语句都须要匹配,而全部的must_not
语句都不能匹配。默认状况下,should
语句一个都不要求匹配,只有一个特例:若是查询中没有must
语句,那么至少要匹配一个should
语句。minimum_should_match
参数来控制should
语句须要匹配的数量,该参数能够是一个绝对数值或者一个百分比。
GET /my_index/my_type/_search { "query": { "bool": { "must": { "match": { "title": "quick" }}, "must_not": { "match": { "title": "lazy" }}, "should": [ { "match": { "title": "brown" }}, { "match": { "title": "dog" }} ] } } }
来实现sql中where的效果, 好比:搜索一个叫Smith,且年龄大于30的员工,能够这么检索.
POST /megacorp/employee/_search { "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "Smith" } } } } }
它容许你在数据上生成复杂的分析统计,相似于sql中的group by
GET /megacorp/employee/_search { "aggs": { "all_interests": { "terms": { "field": "interests" } } } }
聚合也容许分级汇总。例如,让咱们统计每种兴趣下职员的平均年龄
GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" : { "field" : "interests" }, "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } } }
能够查询到全部文档,是没有查询条件下的默认语句。
POST /index/doc/_search { "query" : { "match_all": {} } }
一个标准查询,无论你须要全文本查询仍是精确查询基本上都要用到它。
POST /index/doc/_search { "query" : { "match" : { "title" : "中国杭州" } } }
match
查询接受一个operator
参数,该参数的默认值是"or"
。能够将它改变为"and"
来要求全部的词条都须要被匹配,来提升搜索精度。
POST /index/doc/_search { "query": { "match": { "title": { "query": "中国 杭州", "operator": "and" } } } }
控制精度(Controlling Precision),在下面拥有3个词条的例子中,75%
会被向下舍入到66.6%
,即3个词条中的2个。不管你输入的是什么,至少有2个词条被匹配时,该文档才会被算做最终结果中的一员。
GET /index/doc/_search { "query": { "match": { "title": { "query": "中国杭州", "minimum_should_match": "75%" } } } }
分值计算(Score Calculation)
bool
查询经过将匹配的must
和should
语句的_score
相加,而后除以must
和should
语句的总数来获得相关度分值_score
。must_not
语句不会影响分值;它们惟一的目的是将不须要的文档排除在外。
容许你作match查询的基础上同时搜索多个字段,在多个字段中同时查一个:
POST /index/doc/_search { "query" : { "multi_match": { "query": "中国", "fields": [ "content", "title" ] } } }
match_phrase与match的区别在于,前者会命中”rock“ “climbing”(有序)所有匹配到的数据,然后者会命中rock balabala climbing , 前者可用调节因子slop控制不匹配的数量。
GET /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing", "slop" : 1 } } }
与 bool 过滤类似,用于合并多个查询子句。不一样的是,bool 过滤能够直接给出是否匹配成功, 而bool 查询要计算每个查询子句的 _score (相关性分值)。
must:: 查询指定文档必定要被包含。
must_not:: 查询指定文档必定不要被包含。
should:: 查询指定文档,有则能够为文档相关性加分。
使用标准的shell通配符查询
POST /index/doc/_search { "query": { "wildcard": { "content": "中*" } } }
使用regexp查询可以让你写下更复杂的模式(中文只能匹配单个字开头)
POST /index/doc/_search { "query": { "regexp": { "content": "中.*" } } }
以什么字符开头的,能够更简单地用 prefix
POST /index/doc/_search { "query": { "prefix": { "content": "中" } } }
参考:
http://blog.csdn.net/dm_vincent/article/details/41720193
http://www.cnblogs.com/ghj1976/p/5293250.html
什么是mapping
ES的mapping很是相似于静态语言中的数据类型:声明一个变量为int类型的变量, 之后这个变量都只能存储int类型的数据。一样的, 一个number类型的mapping字段只能存储number类型的数据。
同语言的数据类型相比,mapping还有一些其余的含义,mapping不只告诉ES一个field中是什么类型的值, 它还告诉ES如何索引数据以及数据是否能被搜索到。
剖析mapping
一个mapping由一个或多个analyzer组成, 一个analyzer又由一个或多个filter组成的。当ES索引文档的时候,它把字段中的内容传递给相应的analyzer,analyzer再传递给各自的filters。
filter的功能很容易理解:一个filter就是一个转换数据的方法, 输入一个字符串,这个方法返回另外一个字符串,好比一个将字符串转为小写的方法就是一个filter很好的例子。
一个analyzer由一组顺序排列的filter组成,执行分析的过程就是按顺序一个filter一个filter依次调用, ES存储和索引最后获得的结果。
总结来讲, mapping的做用就是执行一系列的指令将输入的数据转成可搜索的索引项。
默认analyzer
回到咱们的例子, ES猜想description字段是string类型,因而默认建立一个string类型的mapping,它使用默认的全局analyzer, 默认的analyzer是标准analyzer, 这个标准analyzer有三个filter:token filter, lowercase filter和stop token filter。
PUSH /libray/books { "settings" : { "number_of_shards" : 2, "number_of_replicas" : 1 }, "mappings" : { "books" : { "properties" : { "name" : { "type": "string", "index": "not_analyzed" }, "year" : { "type" : "integer" }, "detail" : { "type" : "string" } } } } }
DELETE /libray/_mapping
DELETE /libray/_mapping/books
参考
http://m.blog.csdn.net/lilongsheng1125/article/details/53862629
_source
检索设置为false参数关闭检索
GET /_search { "_source": "obj.*, obj2.*", "query" : { "match_all" : {} } }
complete control
GET /_search { "_source": { "includes": [ "obj1.*", "obj2.*" ], "excludes": [ "*.description" ] }, "query" : { "term" : { "user" : "kimchy" } } }
POST /bank/_search { "query": { "match_all" : {} }, "sort" : [ { "age" : "asc" } ] }
分类模式选项编辑
Elasticsearch支持按数组或多值字段进行排序。该mode
选项控制选择哪一个数组值来排序它所属的文档。该mode
选项能够具备如下值:
|
选择最低的价值。 |
|
选择最高的价值。 |
|
使用全部值的总和做为排序值。仅适用于基于数字的数组字段。 |
|
使用全部值的平均值做为排序值。仅适用于基于数字的数组字段。 |
|
使用全部值的中位数做为排序值。仅适用于基于数字的数组字段。 |
用于过滤搜索结果和聚合的过滤器,post_filter元素是一个顶层元素,只会对搜索结果进行过滤。
GET /cars/transactions/_search?search_type=count { "query": { "match": { "make": "ford" } }, "post_filter": { "term" : { "color" : "green" } }, "aggs" : { "all_colors": { "terms" : { "field" : "color" } } } }
对每一个命中的分数进行解释。
GET /bank/_search { "explain" : true, "query": { "bool" : { "filter" : { "term" : { "age" : 39 } } } } }
为每一个搜索命中返回一个版本。
GET /bank/_search { "version": true, "query": { "bool" : { "filter" : { "term" : { "age" : 39 } } } } }
排除_score
小于如下指定最小值的文档min_score
GET /_search { "min_score": 0.5, "query" : { "term" : { "user" : "kimchy" } } }
返回父文档,也返回匹配has-child条件的子文档,至关于在父子之间join
例子:假设咱们使用父文档存储邮件内容,子文档存储每一个邮件拥有者的信息以及对于此用户这封邮件的状态。搜索某个帐户的邮件列表时,咱们但愿搜索到邮件内容和邮件状态,能够设想假如没有Inner-hits,咱们必须得分两次查询,由于邮件内容和邮件状态分别存放在父文档和子文档中。而有了Inner_hits属性后,咱们可使用一次查询完成。
curl -XGET 'http://localhost:9200/hermes/email/_search/?pretty=true' -d '{ "query": { "has_child": { "type": "email_owner", "query": { "bool": { "must": [ { "term": { "owner": "13724100993@189.cn" } }, {"term": {"labelId": "1"} } ]} }, //注意此处 "inner_hits": {} } } }'
若是一次性要查询多条数据的话,那么必定要用batch批量操做的api,尽量减小网络开销次数,可能能够将性能提高数倍,甚至数十倍。
POST http://localhost:9200/bank/_mget { "docs" : [ { "_type" : "accout", "_id" : 1 },{ "_type" : "accout", "_id" : 2 }] }
强烈推荐:
Elasticsearch5.2核心知识篇 http://www.jianshu.com/nb/13767185
Elasticsearch5.2高手进阶篇 http://www.jianshu.com/nb/14337815
分词器
es 默认分词器原理:中文以单个字为单位进行分词,英文以空格或者标点为单位进行分词。
match与term http://blog.csdn.net/yangwenbo214/article/details/54142786
倒排索引
可参考 http://blog.csdn.net/wang_zhenwei/article/details/52831992
http://www.jianshu.com/p/ed7e1ebb2fb7
filters特性 http://www.cnblogs.com/bmaker/p/5480006.html
过滤查询以及聚合 http://blog.csdn.net/dm_vincent/article/details/42757519
_all http://blog.csdn.net/jiao_fuyou/article/details/49800969
Elasticsearch 字段数据类型 :
http://www.jianshu.com/p/ab99d2bcd63d