elasticsearch基本操作

网友投稿 937 2022-11-26

elasticsearch基本操作

elasticsearch基本操作

基础操作

# 添加数据# PUT 索引名/类型/文档idPUT a1/doc/1{ "name": "阿飞的小姨妈", "age": 18}PUT a1/doc/2{ "name": "阿飞的二姨妈", "age": 16}PUT a1/doc/3{ "name": "阿飞的老姨妈", "age":16}# 获取文档中的一条数据# GET 索引名/类型/文档idGET a1/doc/1# 获取文档中的所有数据GET a1/doc/_search# 删除一条# DELETE 索引名/类型/文档idDELETE a1/doc/3# 删除所有DELETE a1/# 更新一条数据# POST 索引名/类型/文档id/_updatePOST a1/doc/1/_update{ "doc":{ "age": 20 }}# 查询所有的索引GET _cat/indices?v# head确认索引是否存在HEAD a1 # 返回200,不存在返回404

查询的两种方式

- query string- DSL结构化查询# 查询年龄为18的GET a1/doc/_search?q=age:18GET a1/doc/_search{ "query":{ "match":{ "age":18 } }}

match系列查询

GET a1/doc/_search{ "query":{ "match":{ "name":"张磊" } }}# 查询包含白和可爱的,查询的结果为包含一个字和两字的,只要在这三个字里面的任意一个有都会返回GET a1/doc/_search{ "query":{ "match":{ "tag":"白 可爱" } }}

match_phrase短语查询

# 查询可爱,这个不会拆分,按整体查询GET a1/doc/_search{ "query":{ "match_phrase":{ "tag":"可爱" } }}# 特殊参数 slop ,表示间隔,比如 中国是世界,我们查询的时候输入中国世界,查询结果没有对应的返回数据。但是我们加上slop:1,表示跳过1个间隔去匹配GET a1/doc/_search{ "query":{ "match_phrase":{ title:{ "query":"中国世界", "slop": 1 } } }}

match_all查询所有

GET a1/doc/_search{ "query":{ "match_all":{} }}

排序查询 sort

# 插入测试数据PUT s22/doc/1{ "name":"张磊", "age":26, "desc": "大保健,shen不好", "tag": ["篮球","上午"], "both": "1990-11-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":"河北"}# 按年龄降序排序 sort排序GET s22/doc/_search{ "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ]}

分页查询 from和size

# 分页 from和size 从什么开始返回几条数据GET s22/doc/_search{ "query": { "match_all": {} }, "from": 0, "size": 2}

布尔查询

# must(and)# should(or)# must_not(not)# filter# must# 查询city是河北的GET s22/doc/_search{ "query": { "bool": { "must": [ { "match": { "city": "河北" } } ] } }}# 查询city是河北的,并且年龄是26的GET s22/doc/_search{ "query": { "bool": { "must": [ { "match": { "city": "河北" } }, { "match": { "age": 26 } } ] } }}# should 或者# 查询city是河北,或者年龄是26的人GET s22/doc/_search{ "query": { "bool": { "should": [ { "match": { "city": "河北" } }, { "match": { "age": 26 } } ] } }}# must_not# 查询city不是河北,年龄又不是18的GET s22/doc/_search{ "query": { "bool": { "must_not": [ { "match": { "city": "河北" } }, { "match": { "age": "18" } } ] } }}# filter# 查询city是河北,年龄大于25的GET s22/doc/_search{ "query": { "bool": { "must": [ { "match": { "city": "河北" } } ], "filter": { "range": { "age": { "gt": 25 } } } } }}

结果过滤 _source

# 查询city是河北,年龄大于25的city字段结果GET s22/doc/_search{ "query": { "bool": { "must": [ { "match": { "city": "河北" } } ], "filter": { "range": { "age": { "gt": 25 } } } } }, "_source": ["city"]}

高亮查询

# 高亮显示丽质# 默认GET s22/doc/_search{ "query": { "match": { "desc": "丽质" } }, "_source": ["desc"], "highlight": { "fields": { "desc": {} } }}# 自定义高亮显示GET s22/doc/_search{ "query": { "match": { "desc": "丽质" } }, "_source": ["desc"], "highlight": { "pre_tags": "", # 前半段 "post_tags": "", # 后半段 "fields": { "desc": {} } }}

聚合函数

# 聚合函数 max avg min sumGET s22/doc/_search{ "query": { "match_all": {} }, "aggs": { "my_avg": { "avg": { "field": "age" } } }}

分组查询

# 分组GET s22/doc/_search{ "query": { "match_all": {} }, "size": 0, "aggs": { "avg_group": { "range": { "field": "age", "ranges": [ { "from": 15, "to": 20 }, { "from":20, "to":25 }, { "from":25, "to":30 } ] } } }}

-------------------------------------------

个性签名:代码过万,键盘敲烂!!!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Elasticsearch下载安装配置
下一篇:vscode自定义运行和调试创建launch.json文件及项目独立配置文件
相关文章

 发表评论

暂时没有评论,来抢沙发吧~