第二篇、ElasticSearch数据的增删改查
1、文档的增删改查操作
1.1、增加文档
PUT t1/doc/1 { "name": "张三", "age": 18 }
1.2、查询文档
GET t1/doc/1
GET t1/doc/_search?q=age:18
GET t1/doc/_search { "query":{ "match":{ "age":"18" } } }
1.3、修改文档数据
POST t1/doc/1/_update { "doc":{ "name":"李五" } }
1.4、删除文档数据
delete t1/doc/1 { "name":"李四" }
2、索引的操作
2.1、查询索引
get _cat/indices?v
2.2、查询指定的索引信息
get t1
2.3、查询索引下面所有数据
GET t1/doc/_search
2.4、删除索引
DELETE /t1
5、判断索引是否存在
HEAD 索引名字 200:表示存在 404:表示不存在
3、match查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT zhifou/doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] } PUT zhifou/doc/2 { "name":"大娘子", "age":18, "from":"sheng", "desc":"肤白貌美,娇憨可爱", "tags":["白", "富","美"] } PUT zhifou/doc/3 { "name":"龙套偏房", "age":22, "from":"gu", "desc":"mmp,没怎么看,不知道怎么形容", "tags":["造数据", "真","难"] } PUT zhifou/doc/4 { "name":"石头", "age":29, "from":"gu", "desc":"粗中有细,狐假虎威", "tags":["粗", "大","猛"] } PUT zhifou/doc/5 { "name":"魏行首", "age":25, "from":"广云台", "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!", "tags":["闭月","羞花"] }
3.1、match,查看from字段包含 gu的数据
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } } }
3.2、match_all,查询所有,类似SQL,select * from tabel_name
GET zhifou/doc/_search { "query": { "match_all": { } } }
3.3、match_phrase,短语查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT t1/doc/1 { "title": "中国是世界上人口最多的国家" } PUT t1/doc/2 { "title": "美国是世界上军事实力最强大的国家" } PUT t1/doc/3 { "title": "北京是中国的首都" }
GET t1/doc/_search { "query": { "match_phrase": { "title": "中国" } } } # match_phrase与match的区别 match : 按分类值排序,不管匹配不匹配得到都会显示出来 match_phrase :精确匹配,有出现的关键字才会显示出来
3.4、slop 指定位切片模糊匹配
GET t1/doc/_search { "query": { "match_phrase": { "title":{ "query": "中国世界", "slop": 2 } } } }
4、match_phrase_prefix,最左前缀查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT t3/doc/1 { "title": "maggie", "desc": "beautiful girl you are beautiful so" } PUT t3/doc/2 { "title": "sun and beach", "desc": "I like basking on the beach" }
GET t3/doc/_search { "query": { "match_phrase_prefix": { "desc": "bea" } } }
# 限制最大的查询次数,提高性能 GET t3/doc/_search { "query": { "match_phrase_prefix": { "desc": { "query": "bea", "max_expansions": 1 } } } }
5、multi_match :多字段查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT t3/doc/1 { "title": "maggie is beautiful girl", "desc": "beautiful girl you are beautiful so" } PUT t3/doc/2 { "title": "beautiful beach", "desc": "I like basking on the beach,and you? beautiful girl" }
5.1、原来的查询方法
GET t3/doc/_search { "query": { "bool": { "must": [ { "match": { "title": "beautiful" } }, { "match": { "desc": "beautiful" } } ] } } }
5.2、multi_match 查询方法
GET t3/doc/_search { "query": { "multi_match": { "query": "beautiful", "fields": ["title","desc"] } } }
5.3、type: phrase_prefix ,糊模匹配后缀
GET t3/doc/_search { "query": { "multi_match": { "query": "gi", "fields": ["title"], "type": "phrase_prefix" } } }
5.4、type: phrase ,精确匹配词
GET t3/doc/_search { "query": { "multi_match": { "query": "maggie", "fields": ["title"], "type": "phrase" } } }
5.5、term查询
主要作用:用于查询分词后,在分词里面查询关键字并且返回完整数据
# 模拟es分词结果 POST _analyze { "analyzer": "standard", "text": "Beautiful girl!" } # 创建一个索引结构 PUT w10 { "mappings": { "doc":{ "properties":{ "t1":{ "type": "text" } } } } } # 插入数据 PUT w10/doc/1 { "t1": "Beautiful girl!" } PUT w10/doc/2 { "t1": "sexy girl!" } # 查询数据 GET w10/doc/_search { "query": { "match": { "t1": "Beautiful girl!" } } } # 查分词后,分词里面的数据 GET w10/doc/_search { "query": { "term": { "t1" : ["beeautiful","sexy"] } } }
5.6、布尔查询
must:与关系,相当于关系型数据库中的and。 should:或关系,相当于关系型数据库中的or。 must_not:非关系,相当于关系型数据库中的not。 filter:过滤条件。 range:条件筛选范围。 gt:大于,相当于关系型数据库中的>。 gte:大于等于,相当于关系型数据库中的>=。 lt:小于,相当于关系型数据库中的<。 lte:小于等于,相当于关系型数据库中的<=。
布尔查询是最常用的组合查询,根据子查询的规则,只有当文档满足所有子查询条件时,
elasticsearch引擎才将结果返回。布尔查询支持的子查询条件共4中: 1、must(and) 2、should(or) 3、must_not(not) 4、filter
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT zhifou/doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] } PUT zhifou/doc/2 { "name":"大娘子", "age":18, "from":"sheng", "desc":"肤白貌美,娇憨可爱", "tags":["白", "富","美"] } PUT zhifou/doc/3 { "name":"龙套偏房", "age":22, "from":"gu", "desc":"mmp,没怎么看,不知道怎么形容", "tags":["造数据", "真","难"] } PUT zhifou/doc/4 { "name":"石头", "age":29, "from":"gu", "desc":"粗中有细,狐假虎威", "tags":["粗", "大","猛"] } PUT zhifou/doc/5 { "name":"魏行首", "age":25, "from":"广云台", "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!", "tags":["闭月","羞花"] }
1、must : 查询多维数据,关系为且(and)
# 单条件查询 GET zhifou/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } } ] } } } # 多条件查询 GET zhifou/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } }, { "match": { "age": "22" } } ] } } }
5.7、should: 查询多维数据,关系为或(or)
GET zhifou/doc/_search { "query": { "bool": { "should": [ { "match": { "from": "gu" } }, { "match": { "tags": "闭月" } } ] } } }
5.8、、must_not :查询多维数据,关系为取反(not)
# 取既不是"from": "gu",也不是"tags": "可爱",也不是 "age": "18"的数据 GET zhifou/doc/_search { "query": { "bool": { "must_not": [ { "match": { "from": "gu" } }, { "match": { "tags": "可爱" } }, { "match": { "age": "18" } } ] } } }
5.9、filter 过滤数据
filter:过滤条件。 range:条件筛选范围。 gt:大于,相当于关系型数据库中的>。 gte:大于等于,相当于关系型数据库中的>=。 lt:小于,相当于关系型数据库中的<。 lte:小于等于,相当于关系型数据库中的<=。
# 查询from=gu,并且年龄大于25 GET zhifou/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } } ], "filter": { "range": { "age": { "gt": 25 } } } } } } # 查询from=gu,并且看年龄25-30 GET zhifou/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } } ], "filter": { "range": { "age": { "gte": 25, "lte": 30 } } } } } }
6、排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PUT s22/doc/1 { "name": "张磊", "age": 29, "desc": "大帅x, 腰不好", "tag": ["针灸", "爱推拿"], "both": "1980-12-12", "city": "河北" } PUT s22/doc/2 { "name": "罗新宇", "age": 26, "desc": "小帅x, shen不好", "tag": ["针灸", "爱推拿"], "both": "1988-12-12", "city": "山东" } PUT s22/doc/3 { "name": "苏守丽", "age": 18, "desc": "大美妞, 天生丽质难自弃", "tag": ["白", "fu", "美", "可爱"], "both": "1999-08-15", "city": "山西" } PUT s22/doc/4 { "name": "崔雪飞", "age": 18, "desc": "大美女, 天生丽质难自弃", "tag": ["白", "fu", "美", "乖巧"], "both": "1999-08-15", "city": "辽宁" } PUT s22/doc/5 { "name": "he明明", "age": 25, "desc": "大美女, 天生丽质难自弃", "tag": ["白", "fu", "美", "乖巧"], "both": "1999-08-15", "city": "河北" } PUT s22/doc/6 { "name": "何青青", "age": 28, "desc": "大美女, 天生丽质难自弃", "tag": ["白", "fu", "美", "乖巧"], "both": "1999-08-15", "city": "河北" }
# 降序 GET s22/doc/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] } # 升序 GET s22/doc/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "asc" } } ] }
7、分页
# 从第3个开始,显示2条数据 GET s22/doc/_search { "query": { "match_all": {} }, "from": 3, "size": 2 }
8、_source : 显示指定字段的数据
# 查询数据的结果显示city,age两个字段 GET s22/doc/_search { "query": { "bool": { "should": [ { "match": { "city": "河北" } } ], "filter": { "range": { "age": { "gte": 28 } } } } }, "_source": ["city","age"] }