Restful风格
一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交 互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
基本Rest命令说明:
| method |
url地址 |
描述 |
| PUT |
localhost:9200/索引名称/类型名称/文档id |
创建文档(指定文档id) |
| POST |
localhost:9200/索引名称/类型名称 |
创建文档(随机文档id) |
| POST |
localhost:9200/索引名称/类型名称/文档id/_update |
修改文档 |
| DELETE |
localhost:9200/索引名称/类型名称/文档id |
删除文档 |
| GET |
localhost:9200/索引名称/类型名称/文档id |
查询文档通过文档id |
| POST |
localhost:9200/索引名称/类型名称/_search |
查询所有数据 |
创建索引
测试put(添加数据)
1 2 3 4 5
| PUT /test/type1/1 { "name": "saxon", "age": "18" }
|
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).//使用的格式不要制定索引的类型 { "_index" : "test", "_type" : "type1", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
|
在elasticSearch head里面发现新的索引

如果kibana一直在调转,我们可以看一下Elastic Search客户端是否卡住了;
使用put创建一个库
1 2 3 4 5 6 7 8 9 10 11 12 13
| PUT /saxon { "mappings": { "properties": { "name":{ "type": "text" }, "age":{ "type": "integer" } } } }
|
1 2 3 4 5
| { "acknowledged" : true, "shards_acknowledged" : true, "index" : "saxon" }
|
如果不指定数据类型的话,es会自动指定数据
修改索引库
第一种方法:put
1 2 3 4
| PUT /test/_doc/1 { "name":"saxonmo" }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
|
需要注意的是,这个需要我们把库里面所有的字段,都要写出来;实际上就是一个覆盖
使用post 最后加上__update
修改属性
1 2 3 4 5 6
| POST /saxon/_doc/3/_update { "doc":{ "name":"json" } }
|
最新的方式:
1 2 3 4 5 6
| POST /key2/_update/jjab_nUB0kitsOqAVIOH/ { "doc":{ "name":"ss" } }
|
在索引的后面就是用_update在加上 id就可以修改,这么做的方法与上面put不同的地方在于可以不用写出全部的字段名,想要修改哪一个属性就修改那个属性,使用 doc括起来就可以
删除索引库
删除一整个库
删除某一个字段
1
| DELETE /key2/_doc/jjab_nUB0kitsOqAVIOH
|
库名/类型/id
文档的搜索
关于hits
hits包括了索引和文档信息,查询结果的总数,id等信息
1 2 3 4 5 6 7 8 9 10 11 12
| "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.9808291, "_source" : { "name" : "mo", "age" : "18" } } ]
|
不使用restful风格
1
| GET saxon/_search?q=name=mo
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.9808291, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.9808291, "_source" : { "name" : "mo", "age" : "18" } } ] } }
|
搜索指定索引根据属性
1 2 3 4 5 6 7 8
| GET saxon/_search { "query": { "match": { "age": "18" } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.08701137, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "1", "_score" : 0.08701137, "_source" : { "name" : "saxon", "age" : "18" } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "2", "_score" : 0.08701137, "_source" : { "name" : "mosong", "age" : "18" } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.08701137, "_source" : { "name" : "json", "age" : "18" } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "4", "_score" : 0.08701137, "_source" : { "name" : "saxonmo", "age" : "18" } } ] } }
|
搜索出来的数据,控制显示的属性
使用“_source”
1 2 3 4 5 6 7 8 9
| GET /saxon/_search { "query": { "match": { "name": "saxon" } }, "_source": ["name","age"] }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.2039728, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "1", "_score" : 1.2039728, "_source" : { "name" : "saxon", "age" : "18" } } ] } }
|
数据排序
sort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| GET /saxon/_search { "query": { "match": { "name": "mo" } }, "sort": [ { "age": { "order": "desc" } } ] }
|
分页
from[序号] size[页面大小]
1 2 3 4 5 6 7 8 9 10
| GET /saxon/_search { "query": { "match": { "name": "mo" } }, "from": 0 , "size": 2 }
|
条件查询
bool下面使用must或者should,使用must的话,就是and关系,必须要所有条件,排除条件的话,使用 must_not;使用should的话就只要满足一个就好
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| GET /saxon/_search { "query": { "bool": { "must": [ { "match": { "name": "saxon" } }, { "match": { "age": "18" } } ] } } }
|
就是多个条件一起查询
过滤器
使用filter选择在这个条件的时候,range选择age的范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| GET /saxon/_search { "query": { "bool": { "must": [ { "match": { "name": "saxon" } }, { "match": { "age": "18" } } ], "filter": [ {"range": { "age": { "gte": 10, "lte": 20 } }} ] } } }
|
- gte:大于等于 gt:大于
- lte:小于等于 lt:小于
查询多个的权重
1 2 3 4 5 6 7 8
| GET /saxon/_search { "query": { "match": { "tags": "男" } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| { "took" : 325, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.19350058, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.19350058, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "渣男", "男" ] } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "4", "_score" : 0.12874341, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "男人", "美铝" ] } } ] } }
|
匹配的个数不同的话,他的分数就会不一样,权重就不一样,匹配越多,权重越大;
匹配多个的话,直接使用空格隔开
精准查询
term:直接查询精确的,通过倒排索指定的词条进行精确查询
match:使用分词器分析
1 2 3 4 5 6 7 8
| GET /saxon/_search { "query": { "term": { "tags": "男" } } }
|
如果tags是关键字的话,他就会只匹配一整个,不会分词
字段的类型 text和keyword
text:会不会被拆分,会不会使用分词器分析
keyword:不会被拆分
高亮显示
1 2 3 4 5 6 7 8 9 10 11 12 13
| GET /saxon/_search { "query": { "match": { "tags": "男" } }, "highlight": { "fields": { "tags": {} } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| { "took" : 83, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.19350058, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.19350058, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "渣男", "男" ] }, "highlight" : { "tags" : [ "渣<em>男</em>", "<em>男</em>" ] } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "4", "_score" : 0.12874341, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "男人", "美铝" ] }, "highlight" : { "tags" : [ "<em>男</em>人" ] } } ] } }
|
自定义高亮样式:
使用pre_tags(前缀)**和post_tags(后缀)**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| GET /saxon/_search { "query": { "match": { "tags": "男" } }, "highlight": { "pre_tags": "<p>", "post_tags": "</p>", "fields": { "tags": {} } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| { "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.19350058, "hits" : [ { "_index" : "saxon", "_type" : "_doc", "_id" : "3", "_score" : 0.19350058, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "渣男", "男" ] }, "highlight" : { "tags" : [ "渣<p>男</p>", "<p>男</p>" ] } }, { "_index" : "saxon", "_type" : "_doc", "_id" : "4", "_score" : 0.12874341, "_source" : { "name" : "mo", "age" : 23, "desc" : "selftest", "tags" : [ "男人", "美铝" ] }, "highlight" : { "tags" : [ "<p>男</p>人" ] } } ] } }
|