首页 > 其他分享 >es请求方式调用

es请求方式调用

时间:2024-05-09 21:23:02浏览次数:13  
标签:index 调用 shopping 请求 doc price id es name

Es基础

关系:

ElasticSearch-> mysql

index (索引)-> 数据库

Documents(文档) -> row(行)

Fileds(字段)-> column

正排索引 id 内容,类似表格

倒排索引 :keywords : ids

Postman访问实例

创建索引:创建库

ip/索引名

请求路径:PUT http://127.0.0.1:9200/shopping
请求体:none

成功:

{
	"acknowledged": true,
	"shards_acknowledged": true,
	"index": "shopping"
}
查询当前存在索引:

ip/_cat/indices?v=

请求路径:GET  http://127.0.0.1:9200/_cat/indices?v=
请求体:none

成功:

health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .geoip_databases 5vZtZiLXTw-ZnE-gxFK4RA   1   0         33           35     34.1mb         34.1mb
yellow open   user1            XvuPXH4GR3qu9kYgI1vMTg   1   1          0            0       226b           226b
yellow open   product          OuJtZ2GNQjaANql9jHIhdw   1   1          0            0       226b           226b
yellow open   user             84VHenNTTtaJyKUQasAZXA   1   1          3            0      4.8kb          4.8kb
yellow open   shopping         vqraISHNSFioVa4h58y_4w   1   1         10            6       28kb           28kb

创建文档:添加行数据

ip/索引名/_doc

请求路径: POST  http://127.0.0.1:9200/shopping/_doc
请求体:
{
    "name": "小米",
    "price": 1999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "0i0_WI8Bs7gKHbbSH-sS",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 20,
	"_primary_term": 7
}

指定id创建文档:

ip/索引名/_doc/id

请求路径:POST http://127.0.0.1:9200/shopping/_doc/1006
请求体:
{
    "name": "魅族21",
    "price": 2999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1006",
	"_version": 3,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 21,
	"_primary_term": 7
}
查询单挑索引:查询单条数据

ip/索引/_doc/id

请求路径: GET  http://127.0.0.1:9200/shopping/_doc/1001
请求体:none

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 6,
	"_seq_no": 7,
	"_primary_term": 1,
	"found": true,
	"_source": {
		"name": "小米",
		"price": 3999,
		"url": "htp123"
	}
}
查询文档列表:列表查询数据

ip/索引/_search

请求路径: GET  http://127.0.0.1:9200/shopping/_search
请求体:none

成功:

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
条件查询:拆词查询
请求路径: GET  http://127.0.0.1:9200/shopping/_search

{
  "query": {
 "match": { //拆词单个查询
   "name": "华"
   }
  }
}

成功:

{
	"took": 6,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1.3340157,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1.3340157,
				"_source": {
					"name": "华为",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1.3340157,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1.1120224,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
全词匹配查询高亮查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search
请求体:
{
    "query": {
        "match_phrase": { //全词匹配查询
            "name": "华为1"
        }
    },
    "highlight": { //高亮显示这个字段
        "fields": {
            "name": {}
        }
    }
}

成功:

{
	"took": 58,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 4.0541162,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 4.0541162,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				},
				"highlight": {
					"name": [
						"<em>华</em><em>为</em><em>1</em>"
					]
				}
			}
		]
	}
}
多条件范围查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search

请求体:
{
    "query": {
        "bool": {
            "must": [ //should表示或,must表示并
                {
                    "match": {
                        "name": "小米"
                    }
                },
                {
                    "match": {
                        "price": 3999
                    }
                }
            ],
            "filter": {
                "range": {
                    "price": {
                        "lt": 5000
                    }
                }
            }
        }
    }
}

成功:

{
	"took": 18,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.4093566,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			}
		]
	}
}
聚合查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search

请求体:
{
    "aggs": {
        "price_group": { //随意取名
            "terms": { //分组
                "field": "price" //分组字段
            }
        }
    }
}
{
	"took": 38,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 10,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "0i0_WI8Bs7gKHbbSH-sS",
				"_score": 1,
				"_source": {
					"name": "魅族",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1002",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1003",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1006",
				"_score": 1,
				"_source": {
					"name": "魅族21",
					"price": 2999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	},
	"aggregations": {
		"price_group": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": [
				{
					"key": 1999,
					"doc_count": 3
				},
				{
					"key": 3999,
					"doc_count": 3
				},
				{
					"key": 9999,
					"doc_count": 2
				},
				{
					"key": 999,
					"doc_count": 1
				},
				{
					"key": 2999,
					"doc_count": 1
				}
			]
		}
	}
}
文档修改:修改行数据

ip/索引/_doc/id

请求路径: PUT  http://127.0.0.1:9200/shopping/_doc/1001
请求体:
{
    "name": "小米",
    "price": 9999,
    "url": "htp123"
}

成功:

{
  "_index": "shopping",
  "_type": "_doc",
  "_id": "1001",
  "_version": 7,
  "result": "updated",
  "_shards": {
   "total": 2,
   "successful": 1,
  "failed": 0
  },
  "_seq_no": 22,
  "_primary_term": 7
}
局部修改文档(某列)

ip/索引/_update/id

请求路径:PUT  http://127.0.0.1:9200/shopping/_update/1001
请求体:
{
    "doc": {
        "name": "华为1"
    }
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 8,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 23,
	"_primary_term": 7
}

删除文档:

请求路径:DELETE  http://127.0.0.1:9200/shopping/_doc/1001
请求体:none

标签:index,调用,shopping,请求,doc,price,id,es,name
From: https://www.cnblogs.com/cyqf/p/18183098

相关文章

  • PCI-Express-Technology(二)
    第一代PCIe(称为Gen1或者PCIe协议规范版本1.x)中,比特率为2.5GT/s,将它除以10即可得知一个通道的速率将可以达到0.25GB/s。因为链路可以在同一时刻进行发送和接收,因此聚合带宽可以达到这个数值的两倍,即每个通道达到0.5GB/s。第二代PCIe(称为Gen2或者PCIe2.x)中将总线频......
  • RequestBodyAdvice用法详解-参数加解密示例
     在实际项目中,我们常常需要在请求前后进行一些操作,比如:参数解密/返回结果加密,打印请求参数和返回结果的日志等。这些与业务无关的东西,我们不希望写在controller方法中,造成代码重复可读性变差。这里,我们讲讲使用@ControllerAdvice和RequestBodyAdvice、ResponseBodyAdvice来对请......
  • post请求下载文件,"Content-Type": "application/x-www-form-urlencoded",
    importaxiosfrom"axios";importqsfrom"qs";if(item.resourceName=="导出"){const[startTime="",endTime=""]=this.rangeTime||[];letparams={carNumber:this.carNu......
  • Airtest部署IOS测试
    **!17一、Airtest部署IOS测试:!**https://github.com/AirtestProject/iOS-Tagent/blob/master/Introduction/README_zh.mdps:”若访问http://127.0.0.1:8100/inspector报错unknown...........,不用管直接跳过这一步操作,不影响airtest的连接,只要http://127.0.0.1:8100/status......
  • JAVA_WEB复习之请求响应
    简单参数请求:原始的方法,我们需要通过servlet中提供的api,HttpServletRequest(请求对象),获取请求的相关信息。比如获取请求参数:当tomcat接收到请求时,它会把请求的信息封装httpservletrequest到对象中。而在Springboot的环境,原始的API进行了封装,接收参数的形式更加简单。如果是简单......
  • 批量删除WordPress文章和页面的数据库命令和从后台直接删除
    批量删除wordpress的方法有两种:1.从wp后台可以调整展示:最多999条2.选择“Bulk”--“Apply”  通过批量删除wordpress文章和页面的数据库命令的步骤:备份数据库:在执行任何数据库操作之前,务必备份您的数据库以防万一。进入数据库:使用您的数据库管理工具(例如phpMyAdmin)登......
  • docker_test
    WelcometoHexo!Thisisyourveryfirstpost.Checkdocumentationformoreinfo.IfyougetanyproblemswhenusingHexo,youcanfindtheanswerintroubleshootingoryoucanaskmeonGitHub.QuickStartCreateanewpost$hexonew"MyNewPost&q......
  • PCI-Express-Technology(一)
    https://github.com/ljgibbslf/Chinese-Translation-of-PCI-Express-Technology-/blob/main/1%20%E8%83%8C%E6%99%AF.md1.3.2PCI总线发起方(Initiator)与目标方(Target)在PCI层次结构中,总线上的每个设备(device)可以包含多达8个功能(function),这些功能都共享该设备的总线接口,功能......
  • lesson2
    单词equalv等于Nobodyequalshiminstrength.Nobodymatcheshiminstrength.equaln相提并论的人或物InEnglishshehasnoequalinherclass.beheadandshouldersabovesb鹤立鸡群equalityn平等racialequalityraise及物v募集/举起/饲养n涨工资rai......
  • 要获取线程池中任务的返回值,可以使用submit()方法返回的Future对象。你可以通过调用Fu
    importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;publicclassMain{publicstaticvoidmain(String[]args){//设置线程池参数intcorePoolSize=5;//......