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 上图是项目的 ...
随机推荐
- 对接接口时,组织参数json出现的问题
在进行对接第三方接口时,进行参数组装成json的过程中出现参数传递格式错误以及json格式化错误. 在拼接json时,如果json中有对象,则以map的方式组装好所有参数.最后map转成json,不然 ...
- Qt 改变鼠标形状
Qt 改变鼠标形状(转载) 改变鼠标形状,在绘制坐标系的时候有用到,特此记下: 1 this->setMouseTracking(true); //设置为不按下鼠标键触发moveEvent 2 ...
- 微信小程序如何快速开通流量主
1.先开发小程序,小程序需要有亮点,毕竟新颖(这样别人才更好去点击查看) 2.条件是独立访客(UV)不低于1000,1000人说多不多,说少也不少,因为小程序是没有链接的,是不可以进行一个流量刷取的, ...
- 记录一次mybatis缓存和事务传播行为导致ut挂的排查过程
起因 rhea项目有两个ut一直都是挂的,之前也经过几个同事排查过,但是都没有找到解决办法,慢慢的这个问题就搁置了.因为之前负责rhea项目的同事离职,我临时接手了这个项目,刚好最近来了一个新同事在做 ...
- 如何成为一位合格的ScrumMaster
嗨,大家好,我是叶子 ScrumMaster的职责简单理解为:确保团队按照scrum的方式运行,团队的教练,帮助团队更好的工作,过程中的执行者,能够在team和po之间平衡.移除项目进度的障碍,保护团 ...
- Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 给大家介绍个东西,MarkDown真的超级超级好用.哈哈.好了, 正题内容如下: 先 ...
- 飞跃原野(三维bfs)
Problem Description 勇敢的法里奥出色的完成了任务之后,正在迅速地向自己的基地撤退.但由于后面有着一大群追兵,所以法里奥要尽快地返回基地,否则就会被敌人逮住. 终于,法里奥来到了最后 ...
- Brackets(括号最大匹配问题(区间dp))
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- Codeforces 1324E Sleeping Schedule DP
题意 给你一个长度为\(n\)的数组\(a\)和3个数字\(h,l和r\).\(t\)初始为0,每次可以使\(t=(t+a_i) \% h\)或者\(t=(t+a_i-1)\%h\),如果这时\(t\ ...
- Ajax提交数据判断员工编号是否存在,及自动填充与员工编号所对应的员工姓名。
JSP页面中所需要的JavaScript事件及Ajax <script type="text/javascript"> function checkEmpNo(id){ ...