iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >Elastic Search 命令详解-索引操作
  • 405
分享到

Elastic Search 命令详解-索引操作

搜索引擎java服务器 2023-08-31 12:08:52 405人浏览 独家记忆
摘要

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

关于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": "操作成功"

        }

      }

    ]

  }

}

来源地址:https://blog.csdn.net/Yu_Yangjun/article/details/129852592

--结束END--

本文标题: Elastic Search 命令详解-索引操作

本文链接: https://www.lsjlt.com/news/385523.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作