ElasticSearch 简单的crud查询
//数据库和es的对应关系(学习文档可以参考https://es.xiaoleilu.com/010_Intro/35_Tutorial_Aggregations.html)
//如下接口调用都是使用postman工具
//新增一个用户,该用户具有主键,姓名,性别,年龄三个字段,如果按照mysql的思路,我们应该先创建一个user库,然后创建一张userInfo表,接着insert一条数据进入,如果insert的时候没有指定主键值,则主键会递增
es的思路也是这样:localhost:9200/index(数据库)/type(表)/id(代表一行记录的主键,可以不写,不写的话es会自动创建),下面用这样的思路来创建一个用户:
post: localhost:9200/user/userinfo/1 参数为json:
{
"name" : "xiaoMing",
"sex":"男",
"age": 18
}
响应值:
{
"_index": "user", //这里对应数据库userdb
"_type": "userinfo", //这里对应的是数据库表
"_id": "1", //id对应一行记录的主键,代表唯一性,es是通过这个唯一的id进行倒排序的
"_version": 1, //版本号用于作乐观锁用,修改一次,版本号会加1
"result": "created", //操作的类型为新增
"_shards": { //这里代表分片,具体意思自行查询资料
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
//查询一个用户(使用get请求就行了)
get: localhost:9200/user/userinfo/1
响应值:
{
"_index": "user",
"_type": "userinfo",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"name": "xiaoMing",
"sex": "男",
"age": 18
}
}
//修改一个用户(使用put方法)
put: localhost:9200/user/userinfo/1 参数为json:
{
"name" : "xiaoMing",
"sex":"女",
"age": 140
}
响应值:
{
"_index": "user",
"_type": "userinfo",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
//删除一个用户(使用delete方法):
delete:localhost:9200/user/userinfo/1
响应值:
{
"_index": "user",
"_type": "userinfo",
"_id": "1",
"_version": 5,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
//到此基本的增删改查已经完成了,后面介绍高级点的查询用法:
先新增3条记录:
post:localhost:9200/user/userinfo/1
{
"name" : "小花",
"sex":"女",
"age": 12
}
post:localhost:9200/user/userinfo/2
{
"name" : "小丽",
"sex":"女",
"age": 11
}
post:localhost:9200/user/userinfo/3
{
"name" : "小军",
"sex":"男",
"age": 22
}
//查询全部 get/post不带参数: localhost:9200/user/userinfo/_search
或者使用post请求:localhost:9200/user/userinfo/_search,参数:
{
"query":{
"match_all":{}
}
}
//match用来匹配指定字段的值,match会模糊匹配,例如下面查询"小军的用户,会出现所有具有小字的用户"
post: localhost:9200/user/userinfo/_search
{
"query":{
"match":{
"name":"小军"
}
}
}
响应结果(模糊匹配了):score代表匹配度高低,数值越大越匹配,这里小军的匹配度是最高了
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5753642,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
},
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
},
{
"_index": "user",
"_type": "userinfo",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "小花",
"sex": "女",
"age": 12
}
}
]
}
}
//将参数改成
{
"query":{
"match":{
"name":"花"
}
}
}
响应(只有一个值符合了):
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "小花",
"sex": "女",
"age": 12
}
}
]
}
}
//term用于精确匹配:很多时候我们希望的是精确匹配
post: localhost:9200/user/userinfo/_search(以下所有的查询都是使用该url)
{
"query":{
"term":{
"name": "小军"
}
}
}
响应:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
//上面的结果是不是很意外,明明有小军这个用户,却查不出来,如果将条件改成只有一个军字时:
{
"query":{
"term":{
"name": "军"
}
}
}
响应结果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
//问题:为何term做精确查询"小军"的时候查不到数据,
//原因:elasticsearch 里默认的IK分词器是会将每一个中文都进行了分词的切割,所以你直接想查一整个词,或者一整句话是无返回结果的。
解决方法使用keyword:
{
"query":{
"term":{
"name.keyword": "小军"
}
}
}
响应:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
//此时已经可以精确查询了
//multi_match(query_string)在多个字段上进行参数匹配
{
"query":{
"multi_match":{
"query":"小军",
"fields":["name","sex"]
}
}
}
或
{
"query":{
"query_string":{
"query":"小",
"fields":["name","sex"]
}
}
}
响应:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
},
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
//range进行区间查询,要数字类型才有效果,字符串没效果
gt 大于
gte 大于等于
lt 小于
lte 小于等于
请求参数:
{
"query":{
"range":{
"age":{
"lte":11
}
}
}
}
响应:
{
"took": 92,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 1,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
}
]
}
}
//terms多个值匹配:(精确匹配,所以加keyword)
{
"query":{
"terms":{
"name.keyword":["小军","小丽"]
}
}
}
响应:
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 1,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
},
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 1,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
组合查询bool
//1.跟must组合
{
"query":{
"bool":{
"must":{
"match":{
"name.keyword":"小军" //此处没有keyword的话,会匹配所有带有小字和所有带有军字的记录
}
}
}
}
}
响应:
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
//跟must_not搭配
{
"query":{
"bool":{
"must_not":{
"match":{
"name.keyword":"小军"
}
}
}
}
}
响应:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 1,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
}
]
}
}
//should 满足条件的任意语句
{
"query":{
"bool":{
"should":{
"match":{
"name.keyword":"小军"
}
}
}
}
}
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "小军",
"sex": "男",
"age": 22
}
}
]
}
}
//filter 必须匹配(不评分,根据过滤条件来筛选文档)
{
"query":{
"bool":{
"should":{
"match":{
"name":"小"
}
},
"filter":{
"match":{
"age":11
}
}
}
}
}
响应:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
}
]
}
}
//使用constant_score可以取代只有filter的bool查询
{
"query":{
"constant_score":{
"filter":{
"match":{
"age":11
}
}
}
}
}
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "userinfo",
"_id": "2",
"_score": 1,
"_source": {
"name": "小丽",
"sex": "女",
"age": 11
}
}
]
}
}
ElasticSearch 简单的crud查询的更多相关文章
- elasticsearch简单查询
elasticsearch简单查询示例: { "from": "0", //分页,从第一页开始 "size": "10" ...
- ElasticSearch第四步-查询详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- Elasticsearch Span Query跨度查询
ES基于Lucene开发,因此也继承了Lucene的一些多样化的查询,比如本篇说的Span Query跨度查询,就是基于Lucene中的SpanTermQuery以及其他的Query封装出的DSL,接 ...
- 8天掌握EF的Code First开发系列之2 简单的CRUD操作
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 创建控制台项目 根据.Net中的类来创建数据库 简单的CRUD操作 数据库模式更改介绍 本章小结 本人的实验环境 ...
- NEST.net Client For Elasticsearch简单应用
NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...
- spring集成mongodb封装的简单的CRUD
1.什么是mongodb MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB是一个介 ...
- ElasticSearch(6)-结构化查询
引用:ElasticSearch权威指南 一.请求体查询 请求体查询 简单查询语句(lite)是一种有效的命令行_adhoc_查询.但是,如果你想要善用搜索,你必须使用请求体查询(request bo ...
- Mongodb系列- java客户端简单使用(CRUD)
Mongodb提供了很多的客户端: shell,python, java, node.js...等等. 以 java 为例实现简单的增删改查 pom文件: <dependencies> & ...
- springboot + mybatis 的项目,实现简单的CRUD
以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD 上图是项目的 ...
随机推荐
- Asp.net Core启动流程讲解(四)
Asp.net Core内 DI(DependencyInjection)贯穿了项目的始终,要学习Asp.net Core就无法越过DI. 下面讲解一下DI在Asp.Net Core内的流程 asp. ...
- day38:MySQL数据库之约束&索引&外键&存储引擎
目录 part1:数据类型 part2:约束 part3:主键索引 PRI &唯一索引 UNI &普通索引 MUL part4:外键:foreign key part5:在外键中设置联 ...
- vscode 安装go插件失败后,最简单的方法
vscode 安装go插件 参考: https://github.com/goproxy/goproxy.cn/blob/master/README.zh-CN.md https://goproxy. ...
- SpringBoot(19)---SpringBoot整合Apollo
SpringBoot(19)---SpringBoot整合Apollo 有关Apollo之前已经写了两篇文章: 1.[Apollo](1)--- Apollo入门介绍篇 2.[Apollo](2)-- ...
- 关于Chrome浏览器自动同步的问题
Chrome浏览器是开发者最喜欢的浏览器,没有之一,那么公司办公和在家办公的话数据需要有一致性,这个时候就用到了浏览器的自动同步的功能 因为网络的问题,谷歌账户很难登录,基本需要VPN翻墙处理之后才能 ...
- 教你如何在linux操作系统下玩【俄罗斯方块】高清+语音教程
主讲人小冰QQ:986945193 新浪微博:http://weibo.com/mcxiaobing 百度贴吧:忆驹家族小冰 腾讯微博:http://t.qq.com/q986945193 高清视频 ...
- boot磁盘空间大于80警报
WARNING=80SPACE_USED=`df |grep '^/dev/sda' |tr -s ' ' %|cut -d% -f5|sort -n|tail -n1`[ "$SPACE_ ...
- Android 设备指纹
Android唯一识别号(设备指纹)的生成及原理 https://blog.csdn.net/xiechengfa/article/details/70049409?utm_source=itdada ...
- 【小白学PyTorch】8 实战之MNIST小试牛刀
文章来自微信公众号[机器学习炼丹术].有什么问题都可以咨询作者WX:cyx645016617.想交个朋友占一个好友位也是可以的~好友位快满了不过. 参考目录: 目录 1 探索性数据分析 1.1 数据集 ...
- 一起来读官方文档-----SpringIOC(04)
1.4.2.依赖性和详细配置 如上一节所述,您可以将bean属性和构造函数参数定义为对其他托管bean(协作者)的引用或内联定义的值.Spring的基于XML的配置元数据为此目的在其和元素中支持子元素 ...