Elasticsearch(es)

Elasticsearch官网下载地址
产品
用途
端口
Elasticsearch
数据存储和搜索
9200
APM Server
接收应用性能数据
8200
Kibana
Web 管理界面(可视化、看日志、APM 等)
5601
 
 
工具在。elasticsearch-reset-password 就是干这个的。ES 正在运行,直接重置一个密码即可。
两种方式选一个
方式一(自动生成密码,推荐):
cd D:\javaTool\elasticsearch-9.4.3\bin\elasticsearch-reset-password.bat -u elastic

 

运行后会在控制台打印新密码,复制下来就行。
方式二(自己设一个密码):
D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-reset-password.bat -u elastic -i

 

如果执行D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-reset-password.bat -u elastic -i报错
可能是 IP 变了
解决办法
运行命令时指定 --url,强制用 localhost 或 127.0.0.1 来连接(这两个在证书里是允许的):
D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-reset-password.bat -u elastic -i --url "https://localhost:9200"
java.security.cert.CertificateException: No subject alternative names matching IP address 10.12.184.85 found
        at java.base/sun.security.util.HostnameChecker.matchIP(HostnameChecker.java:160)
        at java.base/sun.security.util.HostnameChecker.match(HostnameChecker.java:101)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:508)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:474)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:237)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:135)
        at org.elasticsearch.common.ssl.DiagnosticTrustManager.checkServerTrusted(DiagnosticTrustManager.java:81)
        at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.checkServerCerts(CertificateMessage.java:1285)
        at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.onConsumeCertificate(CertificateMessage.java:1172)
        at java.base/sun.security.ssl.CertificateMessage$T13CertificateConsumer.consume(CertificateMessage.java:1115)
        at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:421)
        at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:477)
        at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:448)
        at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:199)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:172)
        at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1506)
        at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1421)
        at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:455)
        at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
        at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:483)
        at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:187)
        at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:141)
        at org.elasticsearch.xpack.core.security.CommandLineHttpClient.execute(CommandLineHttpClient.java:171)
        at org.elasticsearch.xpack.core.security.CommandLineHttpClient.execute(CommandLineHttpClient.java:109)
        at org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand.checkClusterHealthWithRetries(BaseRunAsSuperuserCommand.java:214)
        at org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand.execute(BaseRunAsSuperuserCommand.java:127)
        at org.elasticsearch.common.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:55)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:107)
        at org.elasticsearch.cli.Command.main(Command.java:54)
        at org.elasticsearch.launcher.CliToolLauncher.main(CliToolLauncher.java:74)
会提示你输入一个新密码,记得设一个你能记住的。
0
 
0
 
服务端下载地址
0
从下载解压后的初始状态算起,实际改动的只有两处:
1. ES 密码
重置了 elastic 用户的密码,你最后改成了 123456。
2. apm-server.yml 中的 output.elasticsearch 块
改动前(原始解压状态):
hosts: ["localhost:9200"]
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"
改动后:
hosts: ["https://localhost:9200"]
  protocol: "https"
  username: "elastic"
  password: "123456"
  ssl:
    certificate_authorities: ['D:\javaTool\Elasticsearch\elasticsearch-9.4.3\config\certs\http_ca.crt']
    verification_mode: certificate
现在试试启动:
cd D:\javaTool\Elasticsearch\apm-server-9.4.3-windows-x86_64
.\apm-server.exe -e
 
0
kibana-9.4.3 官网下载地址
如果您确实需要寻找特定的历史版本,可以前往官方的 Past Releases(历史版本)页面进行查找:
 
0
修改Kibana.yml
不是的,kibana_system 不是 Kibana 新加的,它是 Elasticsearch 自带的系统内置用户,跟 elastic 一样,在你安装 ES 时就存在了。
区别在这:
用户
能不能登录浏览器
用途
elastic
你打开
时,用这个登录操作界面
kibana_system
不能
Kibana 服务程序在后台连 ES 时用的内部账号,是"程序对程序"的通信
简单说:
  • elastic — 给人用的,浏览器登录操作 Kibana
  • kibana_system — 给 Kibana 程序自己用的,像一把后台钥匙
给kibana_system用户生成制定密码
指定你自己的密码
用 交互模式 -i,会在 CMD 里提示你输入密码:
D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-reset-password.bat -u kibana_system -i --url "https://localhost:9200"
运行后会让你输入两次密码,比如你想设成 123456,就直接打进去。
vim config/kibana.yml 
server.port: 5601 
#服务器ip 
server.host: "localhost"  
#elasticsearch的访问地址 
elasticsearch.hosts: ["http://localhost:9200"]  
#Kibana汉化
i18n.locale: "zh-CN"  
# 用户名和密码,填写自己设置的用户名和密码
elasticsearch.username: "kibana_system"
elasticsearch.password: "pass"
windows下启动
0
 
linux运行Kibana
bin/kibana #后台启动
nohup bin/kibana &
 
0
ik_smart 分词器安装
ik_smart 是 IK Analysis 插件提供的分词器,Elasticsearch 默认不包含这个插件,需要手动安装
正确的安装方式
新版 analysis-ik 提供了统一的安装源,对于 Elasticsearch 9.4.3,运行:
& "D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-plugin.bat" install https://get.infini.cloud/elasticsearch/analysis-ik/9.4.3
 
0
替换掉原来指向 GitHub 的链接即可。
如果上面这个也失败(取决于网络环境能否到达 get.infini.cloud),还可以从官网页面手动下载 zip 包后本地安装:
https://release.infinilabs.com/analysis-ik/stable/
找到对应 9.4.3 版本的 zip,下载后用离线方式安装:
& "D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-plugin.bat" install file:///D:/path/to/elasticsearch-analysis-ik-9.4.3.zip
安装完成后重启es
# Create an index 创建索引
PUT /my-index


# Add a document to my-index 添加数据
POST /my-index/_doc
{
    "id": "park_rocky-mountain",
    "title": "Rocky Mountain",
    "description": "Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra."
}


#查看ES节点
GET /_cat/nodes?v

GET /_cat/health?v
GET /_cat/shards?v
GET /_cat/master?v
GET /_cat/shards/blogs?v

#查看索引列表信息
GET /_cat/indices?v

#查看指定索引信息
GET /_cat/indices/es_db?v

#
GET _cluster/health

#ES的默认分词设置是standard,会单字拆分
POST _analyze
{
    "analyzer":"standard",
    "text":"中华人民共和国"
}
#ik_smart:会做最粗粒度的拆分
POST _analyze
{
    "analyzer": "ik_smart",
    "text": "中华人民共和国"
}

#ik_max_word:会将文本做最细粒度的拆分
POST _analyze
{
    "analyzer":"ik_max_word",
    "text":"中华人民共和国"
}
 
0
 
 
/_cat/allocation         #查看单节点的shard分配整体情况
/_cat/shards          #查看各shard的详细情况
/_cat/shards/{index}     #查看指定分片的详细情况
/_cat/master          #查看master节点信息
/_cat/nodes           #查看所有节点信息
/_cat/indices         #查看集群中所有index的详细信息
/_cat/indices/{index}      #查看集群中指定index的详细信息
/_cat/segments        #查看各index的segment详细信息,包括segment名, 所属shard, 内存(磁盘)占用大小, 是否刷盘
/_cat/segments/{index}#查看指定index的segment详细信息
/_cat/count           #查看当前集群的doc数量
/_cat/count/{index}   #查看指定索引的doc数量
/_cat/recovery        #查看集群内每个shard的recovery过程.调整replica。
/_cat/recovery/{index}#查看指定索引shard的recovery过程
/_cat/health          #查看集群当前状态:红、黄、绿
/_cat/pending_tasks   #查看当前集群的pending task
/_cat/aliases         #查看集群中所有alias信息,路由配置等
/_cat/aliases/{alias} #查看指定索引的alias信息
/_cat/thread_pool     #查看集群各节点内部不同类型的threadpool的统计信息,
/_cat/plugins         #查看集群各个节点上的plugin信息
/_cat/fielddata       #查看当前集群各个节点的fielddata内存使用情况
/_cat/fielddata/{fields}     #查看指定field的内存使用情况,里面传field属性对应的值
/_cat/nodeattrs              #查看单节点的自定义属性
/_cat/repositories           #输出集群中注册快照存储库
/_cat/templates              #输出当前正在存在的模板信息
Elasticsearch安装插件
Elasticsearch提供插件机制对系统进行扩展
以安装analysis-icu这个分词插件为例
 
windows安装
& "D:\javaTool\Elasticsearch\elasticsearch-9.4.3\bin\elasticsearch-plugin.bat" install analysis-icu
linux在线安装
#查看已安装插件
bin/elasticsearch-plugin list
#安装插件
bin/elasticsearch-plugin install analysis-icu
#删除插件
bin/elasticsearch-plugin remove analysis-icu
注意:安装和删除完插件后,需要重启ES服务才能生效。
测试分词效果
#unicode支持,中文分词良好 analysis-icu分词器 icu_analyzer对中文是按单字(character)切分的
POST _analyze
{
    "analyzer":"icu_analyzer",
    "text":"中华人民共和国"
}
创建索引时可以指定IK分词器作为默认分词器
PUT /es_db
{
    "settings" : {
        "index" : {
            "analysis.analyzer.default.type": "ik_max_word"
        }
    }
}
ElasticSearch索引操作
创建索引索引命名必须小写,不能以下划线开头
格式: PUT /索引名称
#创建索引
PUT /es_db
#创建索引时可以设置分片数和副本数
PUT /es_db
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

#修改索引配置
PUT /es_db/_settings
{
    "index" : {
        "number_of_replicas" : 1
    }
}
索引 (Index)
一个索引就是一个拥有几分相似特征的文档的集合。比如说,可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。
一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。
# Welcome to the Dev Tools Console!
#
# You can use Console to explore the Elasticsearch API. See the Elasticsearch API reference to learn more:
# https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html
#
# Here are a few examples to get you started.


# 创建索引
PUT /my-index


# 添加数据到指定索引
POST /my-index/_doc
{
    "id": "park_rocky-mountain",
    "title": "Rocky Mountain",
    "description": "Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra."
}


# Perform a search in my-index
GET /my-index/_search?q="rocky mountain"

#查看ES节点
GET /_cat/nodes?v

GET /_cat/health?v
GET /_cat/shards?v
GET /_cat/master?v
GET /_cat/shards/blogs?v

#查看索引列表信息
GET /_cat/indices?v

#查看指定索引信息
GET /_cat/indices/es_db?v

#
GET _cluster/health

#ES的默认分词设置是standard,会单字拆分
POST _analyze
{
    "analyzer":"standard",
    "text":"中华人民共和国"
}
#ik_smart:会做最粗粒度的拆分
POST _analyze
{
    "analyzer": "ik_smart",
    "text": "中华人民共和国"
}

#ik_max_word:会将文本做最细粒度的拆分
POST _analyze
{
    "analyzer":"ik_max_word",
    "text":"中华人民共和国"
}
# unicode支持,中文分词良好 analysis-icu分词器 
#icu_analyzer对中文是按单字(character)切分的 analysis-icu分词器 icu_analyzer对中文是按单字(character)切分的
POST _analyze
{
    "analyzer":"icu_analyzer",
    "text":"中华人民共和国"
}

#创建索引
PUT /es_db

#查询索引
GET /es_db

#es_db是否存在
HEAD /es_db

#删除索引
DELETE /es_db

#关闭索引
POST /es_db/_close
#打开索引
POST /es_db/_open

#创建索引时可以设置分片数和副本数
PUT /es_db
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

#修改索引配置
PUT /es_db/_settings
{
    "index" : {
        "number_of_replicas" : 1
    }
}

# 以下是图片中的原文内容:创建索引时可以指定IK分词器作为默认分词器
PUT /es_db
{
    "settings" : {
        "index" : {
            "analysis.analyzer.default.type": "ik_max_word"
        }
    }
}
#重建索引
POST _reindex
{
  "source": {
    "index": "es_db"
  },
  "dest": {
    "index": "es_db2",
    "version_type": "external"
  }
}

# 查询前10条文档
GET /es_db/_search

# 创建文档,指定id
POST /es_db/_doc
{    "id":"66523",
    "name": "张三",
    "sex": 1,
    "age": 25,
    "address": "广州天河公园",
    "remark": "java developer"
}

# 查找
GET /es_db/_search?q="张三"
#用 bool + must 组合查询,把精确匹配(sex、age)和文本匹配(name、address)分开写:
#用 bool + must 组合查询,把精确匹配(sex、age)和文本匹配(name、address)分开写:
POST es_db/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "张三" }},
        { "term": { "sex": 1 }},
        { "term": { "age": 25 }},
        { "match": { "address": "广州天河公园" }}
      ]
    }
  }
}
更新指定数据
方式一:部分更新(推荐)——只改指定字段,其他不变
POST your_index/_update/文档ID
{
  "doc": {数据
    "name": "李四",
    "age": 26
  }
}
这种方式只更新列出的字段,没写的字段保持原值。
方式二:全量覆盖——替换整条文档
PUT your_index/_doc/文档ID
{
  "name": "李四",
  "sex": 1,
  "age": 26,
  "address": "广州海珠公园"
}
这种方式会把整个文档替换掉,没传的字段会被清空(不会保留旧数据)。
方式三:脚本更新——适用于字段值的计算逻辑
POST your_index/_update/文档ID
{
  "script": {
    "source": "ctx._source.age += 1"
  }
}
如果要先用查询找到文档 ID,可以:
GET your_index/_search
{
  "query": {
    "match": { "name": "张三" }
  }
}
从结果里拿到 _id,再用 _update 修改。
创建指定文档id的数据
问题在这里:你用 POST /es_db/_doc 不带 ID,ES 就会自动生成随机 _id(像 afOZRZ8BM9_PAK2MJJhq 那样)。请求体里的 "id":"66523" 只是一个普通字段,不是 ES 的文档 _id。
要指定文档的 _id,把 ID 放在 URL 路径里:
方式一:POST + URL 路径 ID
POST /es_db/_doc/66523
{
    "name": "张三",
    "sex": 1,
    "age": 25,
    "address": "广州天河公园",
    "remark": "java developer"
}
方式二:PUT + URL 路径 ID(效果一样)
PUT /es_db/_doc/66523
{
    "name": "张三",
    "sex": 1,
    "age": 25,
    "address": "广州天河公园",
    "remark": "java developer"
}

 

posted @ 2026-07-11 11:19  红尘沙漏  阅读(12)  评论(0)    收藏  举报