当前位置:网站首页>ES Restful操作
ES Restful操作
2022-07-17 10:41:00 【程序三两行】
一、restful风格介绍

二、索引增删改查
1、增加索引
命令:put 索引名称/文档类型/文档id
PUT test/user/1
{
"name":"张三",
"age": 12,
"dec": "测试一下"
}字段也是可以自己定义类型的 如果要添加字段类型,则如下操作

常见类型

说明:
1、put 索引名称/文档类型/文档id 中的文档类型在8版本废弃掉 默认就是 _doc类型
2、在es 2.*版本里面是没有这两个字段,只有string字段。5.*之后,把string字段设置为了过时字段,引入text,keyword字段
这两个字段都可以存储字符串使用,但建立索引和搜索的时候是不太一样的
keyword:存储数据时候,不会分词建立索引
text:存储数据时候,会自动分词,并生成索引(这是很智能的,但在有些字段里面是没用的,所以对于有些字段使用text则浪费了空间)。
一切文本类型的字符串可以定义成 “text”或“keyword”两种类型。区别在于,text类型会使用默认分词器分词,当然你也可以为他指定特定的分词器。如果定义成keyword类型,那么默认就不会对其进行分词。
2、查询
命令 GET 索引名
3、修改
方式1:使用增加命令覆盖原来内容
方式2:
POST 索引/文档类型/文档id/_update
POST test/user/2/_update
{
"doc":{
"name":"李四2",
"age": 12,
"dec": "测试一下",
"tags":["唱歌","rap","旅游"]
}
}4、删除
通过DELETE 命令判断删除的是索引还是文档记录
三、文档操作
1、增加文档
PUT test/user/1
{
"name":"张三",
"age": 12,
"dec": "测试一下"
}
PUT test/user/2
{
"name":"李四四",
"age": 12,
"dec": "测试一下",
"tags":["唱歌","rap","旅游"]
}2、修改文档
//put方式更新 每有值得会被置为空
PUT test/user/2
{
"name":"李四四"
}
//推荐post _update更新
POST test/user/2/_update
{
"doc":{
"name":"李四2",
"age": 12,
"dec": "测试一下",
"tags":["唱歌","rap","旅游"]
}
}3、删除文档
DELETE test/user/1
4、查询文档
通过id查询
GET test/user/2
条件查询
GET test/user/_search?q=name:张三
复杂操作查询
1、模糊匹配查询
GET test/user/_search
{
"query":{
"match": {
"name": "张三"
}
}
}结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.847103,
"hits" : [
{
"_index" : "test",
"_type" : "user",
"_id" : "3",
"_score" : 0.847103,
"_source" : {
"name" : "张三三",
"age" : 12,
"dec" : "测试一下"
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "1",
"_score" : 0.8259841,
"_source" : {
"name" : "张三",
"age" : 12,
"dec" : "测试一下"
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "4",
"_score" : 0.62774795,
"_source" : {
"name" : "张三学java",
"age" : 12,
"dec" : "测试一下"
}
}
]
}
}
hist就是索引和文档的信息,是一个json,通过java可以遍历查询相关的信息
2、查询指定的字段即字段过滤
只查询name
GET test/user/_search
{
"query":{
"match": {
"name": "张三"
}
},
"_source":["name"]
}结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.847103,
"hits" : [
{
"_index" : "test",
"_type" : "user",
"_id" : "3",
"_score" : 0.847103,
"_source" : {
"name" : "张三三"
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "1",
"_score" : 0.8259841,
"_source" : {
"name" : "张三"
}
},
{
"_index" : "test",
"_type" : "user",
"_id" : "4",
"_score" : 0.62774795,
"_source" : {
"name" : "张三学java"
}
}
]
}
}3、结果排序
GET test/user/_search
{
"query":{
"match": {
"name": "张三"
}
},
"sort":[
{
"age":{
"order":"desc"
}
}
]
}4、分页插叙
from第几页开始 size返回多少条数据
GET test/user/_search
{
"query":{
"match": {
"name": "张三"
}
},
"from":0,
"size":3
}说明
数据下标还是从0开始的
5、布尔值查询
must所有的条件都是符合类似and
should类似or
must_not类似not
GET test/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "张三"
}
},
{
"match": {
"age": 22
}
}
]
}
}
}6、使用filte过滤
GET test/user/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"name": "张三"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 18
}
}
}
]
}
}
}gt大于
lt小于
gte大于等于
lte小于等于
7、匹配多条件
GET test/user/_search
{
"query":{
"match": {
"tags": "唱歌 rap"
}
}
}多个条件使用空格分隔,只要满足其中一个就可以查询
8、term精确查询
1. term&match
- term: 精确查询,对查询的值不分词,直接进倒排索引去匹配。
- match; 模糊查询,对查询的值分词,对分词的结果一一进入倒排索引去匹配
2. text&keyword
- text: 在写入时,对写入的值进行分词,然后一一插入到倒排索引。
- keyword: 在写入时,将整个值插入到倒排索引中,不进行分词。
3. 实例分析
- 写入值为 hello world,
- 查询值为 hello world
查询类型 | 写入类型 | 结果 |
term | text | 无 |
term | keyword | 有 |
match | text | 有 |
match | keyword | 有 |
//创建索引
PUT /test
{
"mappings": {
"properties": {
"name":{
"type": "keyword"
},
"desc":{
"type": "text"
}
}
}
}
//增加数据
PUT test/_doc/1
{
"name":"测试 java一号",
"desc":"测试 java一号"
}
//name是keyword类型的 可以精确匹配查询到
GET test/_search
{
"query": {
"term": {
"name": "测试 java一号"
}
}
}
//desc是text类型无法精确匹配查询
GET test/_search
{
"query": {
"term": {
"desc":"测试 java一号"
}
}
}
9、多个值匹配的精确查询

10、高亮查询
默认是em标签
GET test/_search
{
"query": {
"match": {
"desc": "测试"
}
},
"highlight": {
"fields": {
"desc": {}
}
}
}
如何自定义标签
GET test/_search
{
"query": {
"match": {
"desc": "测试"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"desc": {}
}
}
}
边栏推荐
- Fundamentals of C language -- 2-1 pointer and wild pointer
- 第十二章 STL 之 list
- Leetcode 197 Rising temperature (July 16, 2022)
- [performance optimization methodology series] VI. summary
- Add - before the command in makefile to ignore the error caused by the command and continue to execute the next command
- SAP Fiori 的附件处理(Attachment handling)
- Componentized advanced -- slot
- 关于基础模块中的依赖由微服务中的子模块继承的时候依赖失效的问题
- 第一部分—C语言基础篇_1. C语言概述
- 第4章-一阶多智体系统一致性 -> 领航跟随系统一致性【程序代码】
猜你喜欢
随机推荐
将视频格式转换为gif图片格式
Chapter 13 set/ multiset of STL
Codeworks 5 questions per day (average 1500) - day 17
mysql进阶(六)模糊查询的四种常见用法介绍
Mux256to1v,Hadd,Fadd
组件化高级--插槽
【ACWing】947. text editor
C语言力扣第25题之k个一组反转链表。多指针遍历
Part I - Fundamentals of C language_ 1. Overview of C language
Interview questions - design test cases for:: memcpy function
多租户 SaaS 的数据库设计模式,你学废了吗?
Un7.16: how to deploy projects on code cloud and invite team members?
Part I - Fundamentals of C language_ 3. Operators and expressions
[C language] user defined type elementary knowledge points
如何在监控主机上单独部署agent——WGCLOUD
研究发现DNA纳米设备注射液可安全用于医疗用途
【Flink】Flink 设置检查点失败一次就报错 setTolerableCheckpointFailureNumber 不起作用
EBay searches eBay products by keyword API return value description
【网络研究院】机器学习系统的威胁是时候该认真对待了
实验1:使用Matlab工具箱进行相机标定实验









