一、restful风格介绍
二、索引增删改查
1、增加索引
命令:put 索引名称/文档类型/文档id
PUT test/user/1 { "name":"张三", "age": 12, "dec": "测试一下" }
字段也是可以自己定义类型的 如果要添加字段类型,则如下操作
常见类型
说明:
1、put 索引名称/文档类型/文档id 中的文档类型在8版本废弃掉 默认就是 _doc类型
2、在es 2.*版本里面是没有这两个字段,只有string字段。5.*之后,把string字段设置为了过时字段,引入text,keyword字段
这两个字段都可以存储字符串使用,但建立索引和搜索的时候是不太一样的
keyword:存储数据时候,不会分词建立索引
text:存储数据时候,会自动分词,并生成索引(这是很智能的,但在有些字段里面是没用的,所以对于有些字段使用text则浪费了空间)。
一切文本类型的字符串可以定义成 “text”或“keyword”两种类型。区别在于,text类型会使用默认分词器分词,当然你也可以为他指定特定的分词器。如果定义成keyword类型,那么默认就不会对其进行分词。
2、查询
命令 GET 索引名
3、修改
方式1:使用增加命令覆盖原来内容
方式2:
POST 索引/文档类型/文档id/_update
POST test/user/2/_update { "doc":{ "name":"李四2", "age": 12, "dec": "测试一下", "tags":["唱歌","rap","旅游"] } }
4、删除
通过DELETE 命令判断删除的是索引还是文档记录
三、文档操作
1、增加文档
PUT test/user/1 { "name":"张三", "age": 12, "dec": "测试一下" } PUT test/user/2 { "name":"李四四", "age": 12, "dec": "测试一下", "tags":["唱歌","rap","旅游"] }
2、修改文档
//put方式更新 每有值得会被置为空 PUT test/user/2 { "name":"李四四" } //推荐post _update更新 POST test/user/2/_update { "doc":{ "name":"李四2", "age": 12, "dec": "测试一下", "tags":["唱歌","rap","旅游"] } }
3、删除文档
DELETE test/user/1
4、查询文档
通过id查询
GET test/user/2
条件查询
GET test/user/_search?q=name:张三
复杂操作查询
1、模糊匹配查询
GET test/user/_search { "query":{ "match": { "name": "张三" } } }
结果
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 0.847103, "hits" : [ { "_index" : "test", "_type" : "user", "_id" : "3", "_score" : 0.847103, "_source" : { "name" : "张三三", "age" : 12, "dec" : "测试一下" } }, { "_index" : "test", "_type" : "user", "_id" : "1", "_score" : 0.8259841, "_source" : { "name" : "张三", "age" : 12, "dec" : "测试一下" } }, { "_index" : "test", "_type" : "user", "_id" : "4", "_score" : 0.62774795, "_source" : { "name" : "张三学java", "age" : 12, "dec" : "测试一下" } } ] } }
hist就是索引和文档的信息,是一个json,通过java可以遍历查询相关的信息
2、查询指定的字段即字段过滤
只查询name
GET test/user/_search { "query":{ "match": { "name": "张三" } }, "_source":["name"] }
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 0.847103, "hits" : [ { "_index" : "test", "_type" : "user", "_id" : "3", "_score" : 0.847103, "_source" : { "name" : "张三三" } }, { "_index" : "test", "_type" : "user", "_id" : "1", "_score" : 0.8259841, "_source" : { "name" : "张三" } }, { "_index" : "test", "_type" : "user", "_id" : "4", "_score" : 0.62774795, "_source" : { "name" : "张三学java" } } ] } }
3、结果排序
GET test/user/_search { "query":{ "match": { "name": "张三" } }, "sort":[ { "age":{ "order":"desc" } } ] }
4、分页插叙
from第几页开始 size返回多少条数据
GET test/user/_search { "query":{ "match": { "name": "张三" } }, "from":0, "size":3 }
说明
数据下标还是从0开始的
5、布尔值查询
must所有的条件都是符合类似and
should类似or
must_not类似not
GET test/user/_search { "query":{ "bool": { "must": [ { "match": { "name": "张三" } }, { "match": { "age": 22 } } ] } } }
6、使用filte过滤
GET test/user/_search { "query":{ "bool": { "should": [ { "match": { "name": "张三" } } ], "filter": [ { "range": { "age": { "gte": 10, "lte": 18 } } } ] } } }
gt大于
lt小于
gte大于等于
lte小于等于
7、匹配多条件
GET test/user/_search { "query":{ "match": { "tags": "唱歌 rap" } } }
多个条件使用空格分隔,只要满足其中一个就可以查询
8、term精确查询
1. term&match
- term: 精确查询,对查询的值不分词,直接进倒排索引去匹配。
- match; 模糊查询,对查询的值分词,对分词的结果一一进入倒排索引去匹配
2. text&keyword
- text: 在写入时,对写入的值进行分词,然后一一插入到倒排索引。
- keyword: 在写入时,将整个值插入到倒排索引中,不进行分词。
3. 实例分析
- 写入值为 hello world,
- 查询值为 hello world
查询类型 | 写入类型 | 结果 |
term | text | 无 |
term | keyword | 有 |
match | text | 有 |
match | keyword | 有 |
//创建索引 PUT /test { "mappings": { "properties": { "name":{ "type": "keyword" }, "desc":{ "type": "text" } } } } //增加数据 PUT test/_doc/1 { "name":"测试 java一号", "desc":"测试 java一号" } //name是keyword类型的 可以精确匹配查询到 GET test/_search { "query": { "term": { "name": "测试 java一号" } } } //desc是text类型无法精确匹配查询 GET test/_search { "query": { "term": { "desc":"测试 java一号" } } }
9、多个值匹配的精确查询
10、高亮查询
默认是em标签
GET test/_search { "query": { "match": { "desc": "测试" } }, "highlight": { "fields": { "desc": {} } } }
如何自定义标签
GET test/_search { "query": { "match": { "desc": "测试" } }, "highlight": { "pre_tags": "<p class='key' style='color:red'>", "post_tags": "</p>", "fields": { "desc": {} } } }