赞
踩
目录
Elasticsearch是一个非常强大的搜索引擎,它可以帮我们对数据进行存储,并快速地搜索及分析数据,如果对Elasticsearch感兴趣的话,可以看一下ElasticSearch7.9.0安装和开启登录密码_Java王小怪的博客-CSDN博客
Elasticsearch在某种程度上我们都是作为数据库使用,那么它肯定是和数据库有着相似之处的。Elasticsearch最关键的就是索引、类型和映射了,其中:
索引_index:(相当于数据库名称);类型 _type:(相当于库中的表,默认为_doc);映射_mapping:(相当于数据库中表的字段)。
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
: 主分片存储的大小(以字节为单位)。
PUT http://localhost:9200/books
- {
- "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"
- }
-
- }
- }
- }
- }
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"
- }
- }
- }
- }
- }
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"
- }
- }
- ]
- }
- }
http://localhost:9200/sdwan_device/_search
- {
- "query": {
- "match_all": {}
- }
- }
POST http://localhost:9200/books/_doc
- {
- "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
- }
POST http://localhost:9200/books/_doc/UWony4oBtacv4iJ46czs/_update
- {
- "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
- }
POST http://localhost:9200/books/_search
- {
- "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"
- }
- }
- ]
- }
- }
PUT http://localhost:9200/books/_mapping
- {
- "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"
- }
- }
- ]
- }
- }
DELETE http://localhost:9200/books
POST http://localhost:9200/books/_doc/_delete_by_query
- {
- "query": {
- "match": {
- "id": "1"
- }
- }
- }
按照日期删除数据:
- {
- "query": {
- "bool": {
- "filter": [
- {
- "range": {
- "createTime": {
- "from": "2023-08-01 00:00:00",
- "to": "2023-08-31 23:59:59"
- }
- }
- }
- ]
- }
- }
- }
2023/11/15新增:
- {"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"}
http://localhost:9200/_bulk
Body中选择binary格式,选中文件发送请求即可。
------------------------------分割线-------------------------------------------
2024/04/16更新
目的:把索引下全部的siteId值更新为"1234567"。
http://localhost:9200/index/_update_by_query
- {
- "script": {
- "source": "ctx._source.siteId = \"1234567\""
- },
- "query": {
- "match_all": {}
- }
- }
http://localhost/index/_mapping
- {
- "properties": {
- "name": {
- "type": "keyword"
- }
- }
- }
注意:name为新加的字段,keyword为字段类型
目的:筛选deviceNum为"2e0ea896-123ferwa"的数据,并根据timestamp倒序排列。
http:localhost:9200/index/_search
- {
- "size": 500,
- "query": {
- "match": {
- "deviceNum": "2e0ea896-123ferwa"
- }
- },
- "sort": [
- {
- "timestamp": {
- "order": "desc"
- }
- }
- ]
- }
---------------------------------------------------分割线-------------------------------------------------------------------
2024/04/16更新
http:localhost:9200/index/_search
- {
- "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更新
目的:把siteId值为"00000"的数据全量更新为"6666"。
http:localhost:9200/index/_search
- {
- "query": {
- "bool": {
- "must": [
- {
- "exists": {
- "field": "siteId"
- }
- },
- {
- "term": {
- "siteId": "00000"
- }
- }
- ]
- }
- },
- "script": {
- "source": "ctx._source.siteId= \"6666\""
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。