curl -X GET "http://localhost:9200/_cat/health?v"
查看节点的详细信息
curl -X GET "http://localhost:9200/_cat/nodes?v"
查看索引信息
curl -X GET "http://localhost:9200/_cat/indices?v"
创建索引(索引会自动创建)
curl -X PUT "http://localhost:9200/customer?pretty"
curl -XPUT "http://localhost:9200/customer33?pretty" -H 'Content-Type: application/json' -d '{
"settings":{
"number_of_shards":5, //每个索引的主分片数,默认值是5,这个配置在索引创建后不能修改。
"number_of_replicas":2, //设置副本数量
//自定义索引默认分析器
"index":{
"analysis":{
"analyzer":{
"default":{
"tokenizer":"standard", //分词器
"filter":[ //过滤器
"asciifolding",
"lowercase",
"ourEnglishFilter"
]
}
},
"filter":{
"ourEnglishFilter":{
"type":"kstem"
}
}
}
}
},
"mapping": {
"_doc": {
"properties": {
"commodity_id": {
"type": "long"
},
"commodity_name": {
"type": "text"
},
"picture_url": {
"type": "keyword"
},
"price": {
"type": "double"
}
}
}
}
}'
查看索引设置
curl "http://127.0.0.1:9200/customer/_settings?pretty"
更新索引配置
curl -X PUT "http://127.0.0.1:9200/customer/_settings?pretty" -H 'Content-Type: application/json' -d '{"number_of_replicas": 3}'
查看索引字段信息
curl http://192.168.253.206:9200/xinhua_en?pretty
curl "http://127.0.0.1:9200/customer/_mapping?pretty"
curl "http://127.0.0.1:9200/videos/fulltext/_mapping?pretty"
新增/更新数据
# PUT会将新的json值完全替换掉旧的;而POST方式只会更新相同字段的值,其他数据不会改变,新提交的字段若不存在则增加
# PUT和DELETE操作是幂等的
# POST操作不是幂等的,比如常见的POST重复加载问题:当我们多次发出同样的POST请求后,其结果是创建了若干的资源
# 创建操作可以使用POST,也可以使用PUT,区别就在于POST是作用在一个集合资源(/articles)之上的,而PUT操作是作用在一个具体资源之上的(/articles/123)。
curl -X PUT 'http://localhost:9200/customer/person/2' -H 'Content-Type: application/json' -d '{"user": "dennis","title": "工程师","desc": "数据库管理aaa"}'
# 更新带版本号
curl -X POST 'http://localhost:9200/customer/person/2/update?version=3' -H 'Content-Type: application/json' -d '{"user": "dennis","title": "工程师","desc": "数据库管理aaa"}'
查询数据
curl "http://127.0.0.1:9200/videos/_search"
curl "http://127.0.0.1:9200/customer/person/_search"
curl "http://127.0.0.1:9200/customer/person/_search?q=title:%E6%9D%8E%E9%B9%8F"
# 获取source数据
curl "http://127.0.0.1:9200/videos/fulltext/578406/_source?pretty"
删除数据
删除后不会马上删除,只是标记状态,会在添加索引的时候删除
curl -X DELETE "http://192.168.0.252:9200/customer/person/1"
删除索引
curl -X DELETE "http://localhost:9200/customer?pretty"
分词分析
curl "http://127.0.0.1:9200/customer/person/_analyze" -H 'Content-Type: application/json' -d '{"text":"搜索引擎","analyze":"ik_max_word"}'
curl "http://127.0.0.1:9200/customer/_analyze" -H 'Content-Type: application/json' -d '{"text":"搜索引擎","analyze":"ik_max_word"}'
相关配置
cluster.name: es_cluster
默认为节点的主机名
node.name: node-206 path.data: /data0/elasticsearch/data path.logs: /data0/elasticsearch/log network.host: 192.168.253.206 http.port: 9200 bootstrap.memory_lock: false bootstrap.system_call_filter: false
以文件的方式提供主机列表,可以动态修改,而不用重启节点
discovery.seed_providers
discovery.zen.ping.unicast.resolve_timeout
最小节点数量,基本的原则是这里需要设置成 N/2+1
discovery.zen.minimum_master_nodes: 2
两个节点的通讯超时时间 主节点-从节点
discovery.zen.ping.timeout: 3
加入集群超时时间
discovery.zen.join_timeout: 3
设置全新群集中符合主机要求的节点的初始集合. 默认情况下,该列表为空,这意味着该节点希望加入已经被引导的集群
cluster.initial_master_nodes
选定主节点发现时间间隔,默认1S
discovery.find_peers_interval
是否有资格成为master ,如果服务器配置不一致 建议设置高配服务器为true
node.master: true
设置为false的话这个节点就只能做master的候选 而不会去做索引的读写等操作,让master专心做领导(前提是集群节点比较多)
node.data: false
静态配置主机列表
discovery.seed_hosts 新配置,静态设置设置主机列表
discovery.zen.ping.unicast.hosts: [“192.168.253.207”, “192.168.253.206”,”192.168.253.205″]
自动发现
组播地址
discovery.zen.ping.multicast.group: 224.2.2.4
组播端口
discovery.zen.ping.multicast.port: 54328
广播消息ttl
discovery.zen.ping.multicast.ttl: 3
绑定的地址,null表示绑定所有可用的网络接口
discovery.zen.ping.multicast.address:null
多播自动发现禁用开关
discovery.zen.ping.multicast.enabled:true
一个集群必须有一个主节点(master node)。用来处理请求、索引的创建、修改、节点管理等。当有了master节点,该节点就要对各子节点进行周期性(心跳机制)的探测,保证整个集群的健康
es有两种集群故障探查机制
第一种是通过master进行的,master会ping集群中所有的其他node,确保它们是否是存活着的. 第二种,每个node都会去ping master node来确保master node是存活的,否则就会发起一个选举过程. discovery.zen.fd.ping_interval:每隔多长时间会ping一次node,默认是1s discovery.zen.fd.ping_timeout:每次ping的timeout等待时长是多长时间,默认是30s discovery.zen.fd.ping_retries:如果一个node被ping多少次都失败了,就会认为node故障,默认是3次
mater挂掉后策略
all:一旦master当即,那么所有的操作都会被拒绝 write:这是默认的选项,所有的写操作都会被拒绝,但是读操作是被允许
每个node都会接收publish message,然后ack这个message,但是不会应用这个更新. 如果master没有在discovery.zen.commit_timeout指定的时间内(默认是30s),从至少discovery.zen.minimum_master_nodes个节点获取ack响应,那么这次cluster state change事件就会被reject,不会应用.
但是一旦在指定时间内,指定数量的node都返回了ack消息,那么cluster state就会被commit,然后一个message会被发送给所有的node. 所有的node接收到那个commit message之后,接着才会将之前接收到的集群状态应用到自己本地的状态副本中去. 接着master会等待所有节点再次响应是否更新自己本地副本状态成功,在一个等待超时时长内,如果接收到了响应,那么就会继续处理内存queue中保存的下一个更新状态. discovery.zen.publish_timeout默认是30s,这个超时等待时长是从plublish cluster state开始计算的
https://zhouze-java.github.io/2019/01/25/Elasticsearch-103-%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2%E5%8F%8Azen-discovery%E9%9B%86%E7%BE%A4%E5%8F%91%E7%8E%B0%E6%9C%BA%E5%88%B6/
几乎所有的API操作,比如index,delete,search,等等,都不是说client跟master通信,而是client跟任何一个node进行通信,那个node再将请求转发给对应的node来进行执行
discovery.zen.hosts_provider:file
然后以$ES_PATH_CONF/unicast_hosts.txt下面描述的格式创建文件。每当对unicast_hosts.txt文件进行更改时,Elasticsearch都会选择新的更改,并使用新的主机列表。
基于文件的发现插件会增强单播主机列表 elasticsearch.yml:如果存在有效的单播主机条目, discovery.zen.ping.unicast.hosts则除了提供的那些之外,还将使用它们 unicast_hosts.txt。
该discovery.zen.ping.unicast.resolve_timeout设置还适用于通过基于文件的发现由地址指定的节点的DNS查找。同样需要指定时间单位,默认为5秒
插件: bigdesk 对集群状态监控 head:可视化操作集群 kibana:用于读取es中的索引库type信息,并使用可视化的方式展示出来。包含柱状图、饼状图、折线图、仪表盘等
shards:分片,每个索引默认5个分片 0,1,2,3,4 默认每个分片有一个副本 replicas:副本 recovery:恢复,重启数据引擎 gateway:网关 discovery.zen:集群管理 transport:客户端
https://learnku.com/docs/elasticsearch73/7.3/index-some-documents/6450 https://www.elastic.co/guide/en/elasticsearch/reference/7.9/getting-started-index.html