目录
前言:
Rest Api
查看全部索引:
请求URL:
响应内容:
创建索引:
请求URL:
body内容:
查看索引:
请求URL:
响应内容:
查询索引全部数据:
请求URL:
响应内容:
或者:
请求方式:POST
请求URL:
Body内容:
新增索引数据:
请求URL:
body内容:
响应内容:
修改索引数据:
请求URL:
body内容:
响应内容:
普通匹配查询:
请求URL:
body内容:
响应内容:
新增索引字段:
请求URL:
body内容:
响应内容:
删除索引:
请求URL:
删除索引具体数据:
请求URL:
body内容:
body内容:
Json批量新增Es数据
文件格式:
请求方式:POST
请求URL:
Es全局更新字段值:
请求URL:
请求方式:POST
请求体:
Es索引新加某字段:
请求URL:
请求方式:PUT
请求体:
Es筛选数据
请求URL:
请求方式:GET
请求体:
Es多字段匹配数据
请求URL:
请求方式:GET
请求体:
Es全量更新特定值
请求URL:
请求方式:POST
请求体:
前言:
Elasticsearch是一个非常强大的搜索引擎,它可以帮我们对数据进行存储,并快速地搜索及分析数据
Elasticsearch在某种程度上我们都是作为数据库使用,那么它肯定是和数据库有着相似之处的。Elasticsearch最关键的就是索引、类型和映射了,其中:
索引_index:(相当于数据库名称);类型 _type:(相当于库中的表,默认为_doc);映射_mapping:(相当于数据库中表的字段)。
Rest Api
查看全部索引:
请求URL:
GET http://localhost:9200/_cat/indices?v
如果开启了登陆密码,记得在postman中配置Authorization
响应内容:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .security-7 iB-5JsanSAG4hVvMkg9EIw 1 0 7 0 25.4kb 25.4kb
green open books WrAL0CMrQuimG7dnMqy0Rg 1 0 5 1 22.6kb 22.6kb
解释返回的数据体内容:
health
: 集群的健康状态,可以是green
(健康)、yellow
(可能存在问题)或red
(严重问题)。status
: 索引的状态,可以是open
(开放搜索)、close
(关闭搜索)或hidden
(不可用于搜索,可能是因为设置了index.hidden
属性)。index
: 索引的名称。uuid
: 索引的唯一标识符。pri
: 主分片的数量。rep
: 副本分片的数量。docs.count
: 索引中文档的数量。docs.deleted
: 自上次刷新以来已删除的文档数量。store.size
: 索引存储的大小(以字节为单位)。pri.store.size
: 主分片存储的大小(以字节为单位)。
创建索引:
请求URL:
PUT http://localhost:9200/books
body内容:
{
"settings":{
"number_of_shards":3,
"number_of_replicas":2
},
"mappings":{
"log_doc":{
"properties":{
"id": {
"type": "keyword"
},
"language": {
"type": "keyword"
},
"price": {
"type": "keyword"
},
"remark": {
"type": "keyword"
},
"title": {
"type": "keyword"
},
"description": {
"type": "keyword"
}
}
}
}
}
查看索引:
请求URL:
GET http://localhost:9200/books
响应内容:
{
"books": {
"aliases": {},
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"description": {
"type": "text"
},
"id": {
"type": "keyword"
},
"language": {
"type": "keyword"
},
"price": {
"type": "float"
},
"remark": {
"type": "keyword"
},
"title": {
"type": "text"
}
}
},
"settings": {
"index": {
"refresh_interval": "1s",
"number_of_shards": "1",
"provided_name": "books",
"creation_date": "1691123775089",
"store": {
"type": "fs"
},
"number_of_replicas": "0",
"uuid": "WrAL0CMrQuimG7dnMqy0Rg",
"version": {
"created": "7090099"
}
}
}
}
}
查询索引全部数据:
请求URL:
GET http://localhost:9200/books/_search
响应内容:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "books",
"_type": "esbook",
"_id": "1",
"_score": 1.0,
"_source": {
"id": "1",
"title": "Java 程序设计",
"language": "汉语",
"author": "盖伦",
"price": 18.88,
"description": "哈哈嗨"
}
},
{
"_index": "books",
"_type": "esbook",
"_id": "2",
"_score": 1.0,
"_source": {
"id": "2",
"title": "Python程序设计",
"language": "英语",
"author": "赵信",
"price": 66.88,
"description": "陷阵之至有死无生"
}
},
{
"_index": "books",
"_type": "esbook",
"_id": "3",
"_score": 1.0,
"_source": {
"id": "3",
"title": "PHP 程序设计",
"language": "test",
"author": "宝石",
"price": 88.88,
"description": "我曾踏足山巅,也曾跌入低谷"
}
},
{
"_index": "books",
"_type": "esbook",
"_id": "TWoQy4oBtacv4iJ4HMye",
"_score": 1.0,
"_source": {
"id": "6",
"title": "新增title",
"language": "language",
"author": "xg",
"price": 2799,
"description": "K60Utrl"
}
}
]
}
}
或者:
请求方式:POST
请求URL:
http://localhost:9200/sdwan_device/_search
Body内容:
{
"query": {
"match_all": {}
}
}
新增索引数据:
请求URL:
POST http://localhost:9200/books/_doc
body内容:
{
"id": "7",
"title": "新增title",
"language": "新增language",
"author": "xg",
"price": 2999,
"description": "Redmi K60Ultra"
}
响应内容:
{
"_index": "books",
"_type": "_doc",
"_id": "UWony4oBtacv4iJ46czs",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 3
}
修改索引数据:
请求URL:
POST http://localhost:9200/books/_doc/UWony4oBtacv4iJ46czs/_update
body内容:
{
"doc": {
"author": "卢十瓦"
}
}
响应内容:
{
"_index": "books",
"_type": "_doc",
"_id": "UWony4oBtacv4iJ46czs",
"_version": 2,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 3
}
普通匹配查询:
请求URL:
POST http://localhost:9200/books/_search
body内容:
{
"query": {
"term": {
"id": 7
}
}
}
响应内容:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.1631508,
"hits": [
{
"_index": "books",
"_type": "esbook",
"_id": "UWony4oBtacv4iJ46czs",
"_score": 1.1631508,
"_source": {
"id": "7",
"title": "新增title",
"language": "新增language",
"author": "卢十瓦",
"price": 2999,
"description": "Redmi K60Ultra"
}
}
]
}
}
新增索引字段:
请求URL:
PUT http://localhost:9200/books/_mapping
body内容:
{
"properties": {
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
响应内容:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "books",
"_type": "esbook",
"_id": "UWony4oBtacv4iJ46czs",
"_score": 1.540445,
"_source": {
"id": "7",
"title": "新增title",
"language": "新增language",
"author": "卢十瓦",
"price": 2999,
"description": "Redmi K60Ultra",
"createTime": "2023-09-25 15:18:53"
}
}
]
}
}
删除索引:
请求URL:
DELETE http://localhost:9200/books
删除索引具体数据:
请求URL:
POST http://localhost:9200/books/_doc/_delete_by_query
body内容:
{
"query": {
"match": {
"id": "1"
}
}
}
按照日期删除数据:
body内容:
{
"query": {
"bool": {
"filter": [
{
"range": {
"createTime": {
"from": "2023-08-01 00:00:00",
"to": "2023-08-31 23:59:59"
}
}
}
]
}
}
}
2023/11/15新增:
Json批量新增Es数据
文件格式:
{"index":{"_index":"books","_type":"_doc"}}
{"id":"d980674","title":"三国演义","language":"汉语","author":"罗贯中","price":158,"description":"四大名著","create Time":"2023-11-15 09:00"}
{"index":{"_index":"books","_type":"_doc"}}
{"id":"d9sad462","title":"海底两万里","language":"English","author":"儒勒·凡尔纳","price":19,"description":"是一本小说","create Time":"2023-11-15 09:00"}
请求方式:POST
请求URL:
http://localhost:9200/_bulk
Body中选择binary格式,选中文件发送请求即可。
------------------------------分割线-------------------------------------------
2024/04/16更新
Es全局更新字段值:
目的:把索引下全部的siteId值更新为"1234567"。
请求URL:
http://localhost:9200/index/_update_by_query
请求方式:POST
请求体:
{
"script": {
"source": "ctx._source.siteId = \"1234567\""
},
"query": {
"match_all": {}
}
}
Es索引新加某字段:
请求URL:
http://localhost/index/_mapping
请求方式:PUT
请求体:
{
"properties": {
"name": {
"type": "keyword"
}
}
}
注意:name为新加的字段,keyword为字段类型
Es筛选数据
目的:筛选deviceNum为"2e0ea896-123ferwa"的数据,并根据timestamp倒序排列。
请求URL:
http:localhost:9200/index/_search
请求方式:GET
请求体:
{
"size": 500,
"query": {
"match": {
"deviceNum": "2e0ea896-123ferwa"
}
},
"sort": [
{
"timestamp": {
"order": "desc"
}
}
]
}
---------------------------------------------------分割线-------------------------------------------------------------------
2024/04/16更新
Es多字段匹配数据
请求URL:
http:localhost:9200/index/_search
请求方式:GET
请求体:
{
"size": "500",
"query": {
"bool": {
"must": [
{
"term": {
"tenantId": "103245"
}
},
{
"terms": {
"cityId": [
101,
202,
303
]
}
}
]
}
},
"sort": {
"dateTime": "desc"
}
}
注意:查询tenantId为103245,且cityId是101、202、303的数据,并且按照dateTime进行倒叙排列。
---------------------------------------------------分割线-------------------------------------------------------------------
2024/05/28更新
Es全量更新特定值
目的:把siteId值为"00000"的数据全量更新为"6666"。
请求URL:
http:localhost:9200/index/_search
请求方式:POST
请求体:
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "siteId"
}
},
{
"term": {
"siteId": "00000"
}
}
]
}
},
"script": {
"source": "ctx._source.siteId= \"6666\""
}
}