ElasticSearch 基于postman基本用法

启动ElasticSearch

启动ElasticSearch,点击运行bat文件,postman测试信息

在这里插入图片描述
在这里插入图片描述

创建索引

创建索引等同于创建一个数据库 ,请求类型:put,请求地址http://localhost:9200/template
template表示索引名称

在这里插入图片描述

查询某个索引

请求类型:get,请求地址为: http://localhost:9200/template

在这里插入图片描述

查询所有索引

请求类型:get,请求地址:http://localhost:9200/_cat/indices?v

在这里插入图片描述

删除索引

请求类型:delete,请求地址:http://localhost:9200/template

在这里插入图片描述

再次请求查询所有索引,查不到template信息

在这里插入图片描述

向索引添加数据

添加数据也就是所谓的在template索引中添加字段(就如同在mysql中有张template表中添加字段)
请求方式:post,请求地址:http://localhost:9200/template/_doc
在请求时一定要添加请求体

请求体如下:
在这里插入图片描述

“result”: “created”: 创建成功

在这里插入图片描述

固定id添加数据

请求方式:post,请求地址:
http://localhost:9200/template/_doc/1000 或者 http://localhost:9200/template/_create/1000
1000 就表示固定id

在这里插入图片描述

主键查询数据

请求地址:get,请求地址:http://localhost:9200/template/_doc/1000
_source 为查询的数据源

在这里插入图片描述

没有则提示false

在这里插入图片描述

查询全部数据

请求方式:get,请求地址:http://localhost:9200/template/_search
查询template下所有的id数据

在这里插入图片描述

修改数据-主键完全覆盖

这里基本逻辑为:就是把新数据用body传输替换
请求方式:post,请求地址:http://localhost:9200/template/_doc/1000
“result”: “updated” 更新成功

在这里插入图片描述

重新主键查询

在这里插入图片描述

修改数据-局部覆盖

请求方式:post,请求地址:http://localhost:9200/template/_update/1000
“result”: “updated” 更新成功

在这里插入图片描述

主键查询,数据更新成功

在这里插入图片描述

主键删除

请求方式:delete,请求地址:http://localhost:9200/template/_doc/1000

在这里插入图片描述

主键查询,id为1000的数据不存在

在这里插入图片描述

条件查询

请求方式:get,请求地址:http://localhost:9200/template/_search?q=name:张三
q代表查询的意思(query)
本地有李四数据测试,更直观

在这里插入图片描述

在这里插入图片描述

请求体查询

请求方式:get,请求地址:http://localhost:9200/template/_search,外加请求体

{
    "query":{
        "match":{
            "name":"李四"
        }
    }
}

在这里插入图片描述

请求体全查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体

{
    "query":{
        "match_all":{
            
        }
    }
}

在这里插入图片描述

分页全查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体添加from,size

{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 2
    
}

在这里插入图片描述

指定参数查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体添加 _source

{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 2,
    "_source":"name"
    
}

在这里插入图片描述

排序查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体 sort

{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 2,
    "_source":["name","age"],
    "sort":{
        "age":{
            "order":"desc"
        }
    }
    
}

在这里插入图片描述

组合查询-同时成立(非同时成立)

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体关键字 must
若两个条件只成立一个就满足条件 请求体关键字为 should

{
    "query":{
        "bool":{ //多个条件参数bool
            "must":[//多个条件同时成立关键字 must  // 满足其一成立关键字 should
                {
                    "match":{
                        "name":"李四"
                    }
                },
                {
                    "match":{
                        "age":"40"
                    }
                }
            ] 
        }
    }
}

在这里插入图片描述

范围查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体关键字:filter,range,gt(大于),lt(小于)

{
    "query":{
        "bool":{ //多个条件参数bool
            "should":[//多个条件同时成立
                {
                    "match":{
                        "name":"张三"
                    }
                }
            ] ,
            "filter":{
                "range":{
                    "age":{
                        "gt":40
                    }
                }
            }
        }
    }
}

在这里插入图片描述

模糊查询

es会把传入的值全部拆分,比如传入“张四”,他会默认为:like ‘%张%’like ‘%四%’ 一并查询
请求方式:get,请求地址:http://localhost:9200/template/_search,请求体关键字match
若要张四一起匹配 like ‘%张四%’ ;请求体关键字match_phrase

//like '%张%'  和 like '%四%'
{
    "query":{
        "match":{
            "name":"张四"
        }
    }
}

//like '%张四%'
{
    "query":{
        "match_phrase":{
            "name":"张四"
        }
    }
}

在这里插入图片描述
在这里插入图片描述

聚合查询

请求方式:get,请求地址:http://localhost:9200/template/_search,请求体关键字 aggs

{
    "aggs":{ // aggs 聚合操作关键字
        "age_group":{ // 年龄分组 名称随意
            "terms":{ //  分组
                "field": "age" //分组字段
            }
        }
    }
}

在这里插入图片描述

年龄平均值,关键字 avg

{
    "aggs":{ // aggs 聚合操作关键字
        "age_avg":{ // 年龄分组 名称随意
            "avg":{ //  平均值
                "field": "age" //分组字段
            }
        }
    },
    "size":0
}

在这里插入图片描述

映射关系

请求方式:put,请求地址:http://localhost:9200/user/_mapping,请求体

{
    "properties":{
        "name":{
            "type":"text",
            "index":true
        },
        "sex":{
             "type":"keyword",
             "index":true
        },
        "tel":{
             "type":"keyword",
             "index":false
        }
    }
}

在这里插入图片描述

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值