ElasticSearch
| Name | ElasticSearch |
|---|---|
| Language | 关系/非关系型数据库 |
| Id | 30 |
| Ability | 85 |
| Sort | 8 |
| Created | 12/11/25, 8:22 PM |
| Modified | 12/11/25, 8:22 PM |
| Is Show | Yes |
Related Projects
Related Notes
| Id | Title | Content | Published | Framework Id | User Id | Created | Modified | Actions |
|---|---|---|---|---|---|---|---|---|
| 25 | ElasticSearch基本操作 | <p>1、创建一个名为note的空索引</p> <pre class=""><code class="hljs language-bash">PUT http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note</code></pre> <p> </p> <p>2、查询一个名为note的索引</p> <pre class=""><code class="hljs language-bash">GET http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note</code></pre> <p> </p> <p>3、删除一个名为note的索引</p> <pre class=""><code class="hljs language-bash">DELETE http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note</code></pre> <p> </p> <p>4、创建note索引映射并指定分词器</p> <pre class=""><code class="hljs language-css">PUT http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note { "mappings":{ "properties":{ "id":{ "type":"keyword" }, "title":{ "type":"text", "analyzer":"ik_max_word", "copy_to":"all" }, "content":{ "type":"text", "analyzer":"ik_max_word", "copy_to":"all" }, "framework_id":{ "type":"keyword" }, "framework_name":{ "type":"text", "analyzer":"ik_max_word", "copy_to":"all" }, "published": { "type": "boolean" }, "user_id": { "type": "keyword" }, "created": { "type": "date" }, "modified": { "type": "date" }, "all":{ "type":"text", "analyzer":"ik_max_word" } } } }</code></pre> <p> </p> <p>5、查看索引映射</p> <pre class=""><code class="hljs language-bash">GET http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_mapping</code></pre> <p> </p> <p>6、创建文档数据</p> <pre class=""><code class="hljs language-bash">POST http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_doc { "title":"今天天气不错4", "content":"今天天气确实不错4,风和日丽", "framework_id":1, "framework_name":"测试数据" }</code></pre> <p> </p> <p>7、自定义id创建note的文档数据</p> <pre class=""><code class="hljs language-bash">POST http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_doc/1 { "title":"今天天特别气不错", "content":"这是自定义id创建的天气不错", "framework_id":1, "framework_name":"测试数据" }</code></pre> <p> </p> <p>8、根据id修改note的文档数据</p> <pre class=""><code class="hljs language-bash">POST http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_update/1 { "doc":{ "content":"这是自定义id创建的天气不错2222" } }</code></pre> <p> </p> <p>9、根据id查询note的文档数据</p> <pre class=""><code class="hljs language-bash">GET http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_doc/1 </code></pre> <p> </p> <p>10、根据id删除note的文档数据</p> <pre class=""><code class="hljs language-bash">DELETE http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_doc/c-hgDZsBGizCsGCMESuU </code></pre> <p> </p> <p>11、查询所有note的文档数据</p> <pre class=""><code class="hljs language-bash">GET http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_search </code></pre> <p> </p> <p>12、根据特定条件查询note的文档数据</p> <pre class=""><code class="hljs language-bash">POST http://elastic:A3MZa=H5sjtyIyE+7aSr@localhost:9201/note/_search { "query": { "bool": { "must": [ { "term": { "published": true } }, { "term": { "framework_id": 4 } }, { "match": { "all": "古" } } ] } }, "size": 20 }</code></pre> | 1 | 30 | 1 | 1/13/26, 1:09 PM | 1/21/26, 2:23 PM | View Edit Delete |
| 70 | ElasticSearch数据迁移 | <p>1、创建文件备份路径</p> <pre class=""><code class="language-bash hljs"> mkdir -p /backup/elasticsearch #给权限 sudo chown -R elasticsearch:elasticsearch /mnt/elasticsearch/backups sudo chmod -R 750 /mnt/elasticsearch/backups</code></pre> <p> </p> <p>2、编辑配置文件</p> <pre class=""><code class="language-bash hljs"># Ubuntu24.04 sudo vim /etc/elasticsearch/elasticsearch.yml # Windows11 D:\Elasticsearch\config\elasticsearch.yml</code></pre> <p> </p> <p>3、在elasticsearch.yml 文件末尾添加仓库路径:</p> <pre class=""><code class="language-less hljs">path.repo: ["/backup/elasticsearch"]</code></pre> <p> </p> <p>4、重启es</p> <pre class=""><code class="hljs language-undefined">sudo systemctl restart elasticsearch</code></pre> <p> </p> <p>5、重置es用户密码(如果忘记),如果记得则略过该步骤</p> <pre class=""><code class="hljs language-bash">sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic</code></pre> <p> </p> <p>6、 创建快照仓库,这里用curl获取,如果有工具也可以直接用apifox或者postman获取</p> <pre class=""><code class="language-bash hljs">curl -u elastic -X PUT "http://localhost:9200/_snapshot/my_repo" -H 'Content-Type: application/json' -d' { "type": "fs", "settings": { "location": "/backup/elasticsearch", "compress": true } } '</code></pre> <p> </p> <p>7、创建快照(导出数据,例如叫 snapshot_ubuntu)</p> <pre class=""><code class="language-bash hljs">curl -u elastic -X PUT "http://localhost:9200/_snapshot/my_repo/snapshot_ubuntu?wait_for_completion=true"</code></pre> <p> </p> <p>8、打包快照文件(准备拷贝到 Windows)</p> <pre class=""><code class="hljs language-bash"># 进入 backup 目录 cd /backup # 打包整个 elasticsearch 目录 sudo tar -czvf es_snapshot.tar.gz elasticsearch # 查看 ls -lh es_snapshot.tar.gz</code></pre> <p> </p> <p>9、将打包好的文件复制到windows上</p> <pre class=""><code class="language-ruby hljs"># 用sz直接下载 sz es_snapshot.tar.gz # 或者scp传输 scp your_user@ubuntu_ip:/backup/es_snapshot.tar.gz D:\Backup\Elasticsearch</code></pre> <p> </p> <p>10、 在windows上设置备份文件的存放路径</p> <pre class=""><code class="language-bash hljs"># 这是windowspower shell的操作命令,其实和mkdir没区别。简单粗暴点就直接手动鼠标右键新建文件夹 New-Item -Path "D:\Backup\Elasticsearch" -ItemType Directory -Force</code></pre> <p> </p> <p>11、解压快照文件</p> <pre class=""><code class="language-bash hljs"># 解压到 D:\Backup\Elasticsearch,简单粗暴点就直接winrar解压,都上了windows了也没必要都用命令行了 tar -xzf D:\Backup\Elasticsearch\es_snapshot.tar.gz -C D:\Backup\Elasticsearch\</code></pre> <p> </p> <p>12、修改windows上ES的yml配置文件</p> <pre class=""><code class="hljs language-lua">C:\ProgramData\Elastic\Elasticsearch\config\elasticsearch.yml</code></pre> <p> </p> <p>13、同样最后添加仓库路径,要用/不能用windows的\,会报错无法启动</p> <pre class=""><code class="language-less hljs">path.repo: ["D:/Backup/Elasticsearch"]</code></pre> <p> </p> <p>12、重启ES</p> <pre class=""><code class="language-bash hljs">#这个也一样,手动吧elasticsearch.bat启动的命令行对话框关掉再也行 Restart-Service elasticsearch</code></pre> <p> </p> <p>13、重置windows上es的密码</p> <pre class=""><code class="hljs language-bash">cd "C:\Program Files\Elastic\Elasticsearch\9.3.1\bin" .\elasticsearch-reset-password.bat -u elastic</code></pre> <p> </p> <p>14、注册相同的快照仓库</p> <pre class=""><code class="language-bash hljs">curl -u elastic -X PUT "http://localhost:9200/_snapshot/my_repo" -H "Content-Type: application/json" -d "{`"type`":`"fs`",`"settings`":{`"location`":`"D:\Backup\Elasticsearch`",`"compress`":true}}"</code></pre> <p> </p> <p>15、恢复快照</p> <pre class=""><code class="hljs language-bash">curl -u elastic -X POST "http://localhost:9200/_snapshot/my_repo/snapshot_ubuntu/_restore?wait_for_completion=true"</code></pre> <p> </p> | 1 | 30 | 1 | 3/15/26, 5:35 PM | 3/15/26, 8:01 PM | View Edit Delete |