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 上图是项目的 ...
随机推荐
- java实现邮箱发送信息--验证码的发送(163邮箱)
1.maven环境 <!-- 发送邮件 --> <dependency> <groupId>javax.mail</groupId> <artif ...
- openCV - 1. 加载、修改、保存图像
加载 cv::imread 修改 cv::cvtColor 保存 cv::imwrite 加载图像cv::imread imread功能是加载图像文件成为一个Mat对象,其中第一个参数表示图像文件名称 ...
- php实现无限极分类(多维数组 / 二维数组)形式
<?php // 测试数组数据$array = array( array('id'=>'1','title'=>'父级分类1','pid'=>'0'), array('id'= ...
- iOS 64位静态链接库
https://www.jianshu.com/p/486e3b737707 https://stackoverflow.com/questions/44635297/setting-an-ios-s ...
- [LeetCode题解]79. 单词搜索
题目描述 题目:79. 单词搜索 解题思路 遍历 首先找重复性,题目说给定单词是否存在于二维数组中,可以简化为从 (x, y) 走 n 步(n 表示单词长度),查看给定单词是否存在.然后再遍历二维数组 ...
- 以jar包为容器的java程序访问一同打到jar包里的配置文件的方法
Java程序有时会被打到jar包里执行,同时src/main/resources里一些配置文件也会被打进去. 比如,src/main/resources下有一个box目录,里面有几个json文件,用m ...
- 原生JDK网络编程- NIO
什么是NIO? NIO 库是在 JDK 1.4 中引入的.NIO 弥补了原来的 I/O 的不足,它在标准 Java 代码中提供了高速的.面向块的 I/O.NIO翻译成 no-blocking io 或 ...
- spring cloud 通过zuul网关去请求的时候报404的几个原因。
spring cloud 中 zuul 网关的那些坑: 1.检查你的服务是否正常启动. 2.检查你的服务是否正常注册到注册中心. 3.zuul网关的路由规则是会把你注册在注册中心的serviceId ...
- IDEA项目左侧目录看不到target
点击如下图所示图标,然后选择Show Excluded Files后即可出现
- PyCharm怎样添加Qt designer
cmd命令检查是否安装一下工具包 不存在,用pip 命令安装 添加环境变量; QT_QPA_PLATFORM_PLUGIN_PATH ---D:\VNConda\Lib\site-package ...