首页 > 其他分享 >ES Restful操作

ES Restful操作

时间:2022-10-09 15:00:43浏览次数:40  
标签:name GET 查询 索引 user test 操作 Restful ES


一、restful风格介绍

ES Restful操作_elasticsearch

二、索引增删改查

1、增加索引

命令:put 索引名称/文档类型/文档id


PUT test/user/1
{
"name":"张三",
"age": 12,
"dec": "测试一下"
}


字段也是可以自己定义类型的 如果要添加字段类型,则如下操作

ES Restful操作_字段_02

常见类型

ES Restful操作_字段_03

说明:

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、多个值匹配的精确查询

ES Restful操作_elasticsearch_04

10、高亮查询

默认是em标签


GET test/_search
{
"query": {
"match": {
"desc": "测试"
}
},
"highlight": {
"fields": {
"desc": {}
}
}
}


ES Restful操作_java_05

如何自定义标签


GET test/_search
{
"query": {
"match": {
"desc": "测试"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"desc": {}
}
}
}

标签:name,GET,查询,索引,user,test,操作,Restful,ES
From: https://blog.51cto.com/u_11334685/5740486

相关文章

  • 「题解」Codeforces 1572B Xor of 3
    我知道你很急,但你先别急。我急了我急了我急了。如果直接上去莽构造的话,很可能就是像我一样大分讨的后果。先特判掉全是1,或者1的个数是奇数的情况。我的做法是考虑所......
  • Linux文件操作命令touch
    touchtouch[filename]创建文件,参数可以跟多个如果要创建50个有规律的文件,例如text1-text50利用参数扩展touchtest{1..50}touchtest{a..e}touchtest{a..e}_{......
  • Python非root用户启动python multiprocessing的semlock,提示没有权限的解决方法
    使用进程间通信的时候Python报错为<spanstyle="font-size:18px;">Traceback(mostrecentcalllast):File"web_game_sign.py",line483,in<module>count=mu......
  • kubernetes Tcp流量可视化
    kubernetesTcp流量可视化使用k8spacket和grafana的nodegraph插件可以查看kubernetespod的TCP相关信息,如connection、bytes、和duration。下面是接收和响应的字节数信息......
  • 微信小程序使用wx.request请求服务器json数据并渲染到页面操作示例
    wx.request({url:'http://www.likeyunba.com/test/test.json',headers:{'Content-Type':'application/json'},success:function(res){//......
  • wireshark网络封包抓包工具导入/导出pcap文件
    1、Wireshark导入文件打开Wiresharkwiki,点击SampleCaptures,可以看到Wireshark官方上传的一些pcap文件。点击SampleCaptures后,可以看到文件后缀名有cap,pcap,pcapng,pc......
  • requestIdleCallback方法
    概念window.requestIdleCallback() 方法插入一个函数,这个函数将在浏览器空闲时期被调用。这使开发者能够在主事件循环上执行后台和低优先级工作,而不会影响延迟关键事件,......
  • 码分复用CDM(Code Division Multiplexing);码分多址CDMA(Code Division Multiple Acces
    码分多址当码分复用信道为多个不同地址的用户所共享时,就称为码分多址。每一个用户可以在同样的时间使用同样的频带进行通信,码分复用集合了频分复用和时分复用的优点。且......
  • 探索ES6(ES2015)
    探索ES6(ES2015)本书HTML在线:ExploringES6https://exploringjs.com/es6/index.html是一本关于ECMA-2626thEdition(ECMAScript2015)的最全面的书。是一本为已经了解J......
  • es6 中文
    https://m.w3cschool.cn/escript6/escript6-cx4337fr.htmlhttps://es6-org.github.io/exploring-es6/#./16.7.mdhttps://w3ctech.com/topic/2045......