公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

关于Elastic Search安装可以参考《Elastic Search 8.6.2集群安装部署》及Kibana安装可以参考《Elastic Search 8.6.2简单操作》。相关命令将在Kibana工具的Console平台上执行。

 

Elastic Search索引操作主要包含:创建、删除、关闭和打开索引,以及索引别名的操作。其中,索引别名的操作在生产环境中使用比较广泛,可以和关闭或删除索引配合使用。在生产环境中使用索引时,都应该特别注意操作不当引起数据丢失或异常的问题。

1.创建索引

使用Elastic Search构建搜索引擎的第一步就是创建索引。创建索引以PUT方式发起请求,命令 PUT /indexName

PUT /customer

{

"settings":{

"number_of_shards": 5,

"number_of_replicas": 2

},

"mAppings":{

"properties":{

"name":{

"type":"text"

},

"age":{

"type": "integer"

}

}

}

}

{

"acknowledged": true,

"shards_acknowledged": true,

"index": "customer"

}


 

2.删除索引

删除索引使用 DELETE /indexName

DELETE /customer

{

"acknowledged": true

}

3.关闭索引

有些索引可能在暂时不使用,但未来可能还会使用时,就可以关闭该索引。索引关闭时,只能使用Elastic Search的Api或者监控工具来查看该索引的信息。此时对索引的读写操作都会报错:索引关闭异常。

POST /customer/_close

{

"acknowledged": true,

"shards_acknowledged": true,

"indices": {

"customer": {

"closed": true

}

}

}

4.打开索引

索引关闭了,想重新使用可以再次打开索引。

POST /customer/_open

{

"acknowledged": true,

"shards_acknowledged": true

}

5.索引别名

索引别名一般是通过一个别名关联一个或多个索引,让别名与索引之间建立逻辑关系,以地区、时间等划分索引的场景会使用到。例如日志场景:

 

PUT /log-20230329

{

"mappings": {

"properties": {

"title": {

"type": "text"

},

"content": {

"type": "text"

},

"code": {

"type": "keyword"

},

"info": {

"type": "text"

}

}

}

}

POST /log-20230329/_doc/001

{

"title": "日志29",

"content":"内容29",

"code": "0001",

"info":"操作失败"

}

PUT /log-20230328

POST /log-20230328/_doc/001

{

"title": "日志28",

"content":"内容28",

"code": "0000",

"info":"操作成功"

}

PUT /log-20230327

POST /log-20230327/_doc/001

{

"title": "日志27",

"content":"内容27",

"code": "0000",

"info":"操作成功"

}

创建索引别名

POST /_aliases

{

"actions": [

{

"add":{

"index": "log-20230329",

"alias": "last_three_day_log"

}

},{

"add":{

"index": "log-20230328",

"alias": "last_three_day_log"

}

},{

"add":{

"index": "log-20230327",

"alias": "last_three_day_log"

}

}

]

}

{

"acknowledged": true

}

这个时候查询last_three_day_log就会查询到关联的三个索引:

GET /last_three_day_log/_search

{

"query":{

"match":{

"title":"日志"

}

}

}

{

"took": 6,

"timed_out": false,

"_shards": {

"total": 3,

"successful": 3,

"skipped": 0,

"failed": 0

},

"hits": {

"total": {

"value": 3,

"relation": "eq"

},

"max_score": 0.5753642,

"hits": [

{

"_index": "log-20230327",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志27",

"content": "内容27",

"code": "0000",

"info": "操作成功"

}

},

{

"_index": "log-20230328",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志28",

"content": "内容28",

"code": "0000",

"info": "操作成功"

}

},

{

"_index": "log-20230329",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志29",

"content": "内容29",

"code": "0001",

"info": "操作失败"

}

}

]

}

}

数据是不能直接写到别名的索引,如果需要写入数据则需要在创建别名时添加参数 is_write_index。

别名除了关联多个索引实现组合查询外,还可以用来替换索引。实现原理很简单,例如:当天的日志已经生成了,现在近三天日志,应该将log-20230327删除,重新添加log-20230330的索引,就实现了替换索引,先创建log-20230330索引:

PUT /log-20230330

{

"mappings": {

"properties": {

"title": {

"type": "text"

},

"content": {

"type": "text"

},

"code": {

"type": "keyword"

},

"info": {

"type": "text"

}

}

}

}

POST /log-20230330/_doc/001

{

"title": "日志30",

"content":"内容30",

"code": "0000",

"info":"操作成功"

}

再重新定义索引别名

POST /_aliases

{

"actions": [

{

"add":{

"index": "log-20230330",

"alias": "last_three_day_log"

}

},{

"add":{

"index": "log-20230329",

"alias": "last_three_day_log"

}

},{

"add":{

"index": "log-20230328",

"alias": "last_three_day_log"

}

},{

"remove":{

"index": "log-20230327",

"alias": "last_three_day_log"

}

}

]

}

{

"acknowledged": true

}

重新查询last_three_day_log就会发现没有log-20230327索引的内容了:

GET /last_three_day_log/_search

{

"query":{

"match":{

"title":"日志"

}

}

}

{

"took": 11,

"timed_out": false,

"_shards": {

"total": 3,

"successful": 3,

"skipped": 0,

"failed": 0

},

"hits": {

"total": {

"value": 3,

"relation": "eq"

},

"max_score": 0.5753642,

"hits": [

{

"_index": "log-20230328",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志28",

"content": "内容28",

"code": "0000",

"info": "操作成功"

}

},

{

"_index": "log-20230329",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志29",

"content": "内容29",

"code": "0001",

"info": "操作失败"

}

},

{

"_index": "log-20230330",

"_id": "001",

"_score": 0.5753642,

"_source": {

"title": "日志30",

"content": "内容30",

"code": "0000",

"info": "操作成功"

}

}

]

}

}

分享到:
标签:Elastic Search
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定