elasticsearch 安装 es从6以后版本一个index下面只能有一个type(即默认索引类型_doc),后期7版本中要把type去掉 elasticsearch7默认不在支持指定索引类型,默认索引类型是_doc,如果想改变,则配置include_type_name: true ,建议不要这么做,因为elasticsearch8后就不在提供该字段,官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html 一、系统环境 操作系统:CentOS Linux release 7.3 elasticsearch:elasticsearch-7.0.0 修改/etc/hosts 192.168.1.12 es1 node2 node2.xkq.com 192.168.1.13 es2 node3 node3.xkq.com 192.168.1.14 es3 node4 node4.xkq.com vi /etc/security/limits.conf es - nofile 65535 es soft nproc 65535 es hard nproc 65535 ulimit -a #查看当前用户的资源限制 ulimit #暂时修改,切换到该用户es,ulimit -n 65535 配置虚拟内存 sysctl -w vm.max_map_count=262144 #临时修改该值 vim /etc/sysctl.conf #永久修改 vm.max_map_count=262144 配置好java 二、安装配置 useradd es su - es tar -zxvf elasticsearch-7.0.0-linux-x86_64.tar.gz -C /usr/local/ cd /usr/local ln -s elasticsearch-7.0.0 es7 vi /usr/local/es7/config/elasticsearch.yml cluster.name: es-cluster node.name: es1 network.host: 0.0.0.0#允许外部访问的地址 discovery.seed_hosts: ["es1", "es2","es3"] cluster.initial_master_nodes: ["es1", "es2","es3"] http.cors.enabled: true http.cors.allow-origin: "*" 三、启动 su - es /usr/local/es/bin/elasticsearch -d 四、常用命令 (1)、_cat操作 _cat系列提供了一系列查询elasticsearch集群状态的接口。你可以通过执行 curl -XGET localhost:9200/_cat 获取所有_cat系列的操作 /_cat/allocation /_cat/shards /_cat/shards/{index} /_cat/master /_cat/nodes /_cat/indices /_cat/indices/{index} /_cat/segments /_cat/segments/{index} /_cat/count /_cat/count/{index} /_cat/recovery /_cat/recovery/{index} /_cat/health /_cat/pending_tasks /_cat/aliases /_cat/aliases/{alias} /_cat/thread_pool /_cat/plugins /_cat/fielddata /_cat/fielddata/{fields} (2):_cluster系列 1、查询设置集群状态 curl -XGET localhost:9200/_cluster/health?pretty=true pretty=true表示格式化输出 level=indices 表示显示索引状态 level=shards 表示显示分片信息 2、curl -XGET localhost:9200/_cluster/stats?pretty=true 显示集群系统信息,包括CPU JVM等等 3、curl -XGET localhost:9200/_cluster/state?pretty=true 集群的详细信息。包括节点、分片等。 3、curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true 获取集群堆积的任务 3、修改集群配置 举例: curl -XPUT localhost:9200/_cluster/settings -d ‘{ “persistent” : { “discovery.zen.minimum_master_nodes” : 2 } }’ transient 表示临时的,persistent表示永久的 4、curl -XPOST ‘localhost:9200/_cluster/reroute’ -d ‘xxxxxx’ 对shard的手动控制,参考http://zhaoyanblog.com/archives/687.html 5、关闭节点 关闭指定192.168.1.1节点 curl -XPOST ‘http://192.168.1.1:9200/_cluster/nodes/_local/_shutdown’ curl -XPOST ‘http://localhost:9200/_cluster/nodes/192.168.1.1/_shutdown’ 关闭主节点 curl -XPOST ‘http://localhost:9200/_cluster/nodes/_master/_shutdown’ 关闭整个集群 $ curl -XPOST ‘http://localhost:9200/_shutdown?delay=10s’ $ curl -XPOST ‘http://localhost:9200/_cluster/nodes/_shutdown’ $ curl -XPOST ‘http://localhost:9200/_cluster/nodes/_all/_shutdown’ delay=10s表示延迟10秒关闭 (3):_nodes系列 curl -XGET ‘http://localhost:9200/_nodes/stats?pretty=true’ curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2/stats?pretty=true’ curl -XGET ‘http://localhost:9200/_nodes/process’ curl -XGET ‘http://localhost:9200/_nodes/_all/process’ curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/jvm,process’ curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/info/jvm,process’ curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/_all curl -XGET ‘http://localhost:9200/_nodes/hot_threads (4)索引操作 http://localhost:9200/_cat/indices? curl -XGET ‘http://localhost:9200/{index}/{type}/{id}’ 1、创建索引 a.方法一 curl -XPUT http://192.168.1.12:9200/my_new_index1?pretty curl -H "Content-Type: application/json" -XPUT http://192.168.1.12:9200/my_new_index1/user/1?pretty -d '{"name":"张三","age":"23"}' http://192.168.1.12:9200/my_new_index1/user/1 http://192.168.1.12:9200/my_new_index1/user/_search?pretty b.方法二 curl -H "Content-Type: application/json" -XPUT http://192.168.1.12:9200/shop/ -d ' { "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }' elasticsearch7默认不在支持指定索引类型,默认索引类型是_doc,如果想改变,则配置include_type_name: true 建议不要这么做,因为elasticsearch8后就不在提供该字段,官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html curl -H "Content-Type: application/json" -XPUT http://192.168.1.12:9200/shop/_mapping/good1?include_type_name=true -d ' {"properties": { "title": { "type": "text" }, "price": { "type": "float" }, "stock": { "type": "integer" } } }' http://192.168.1.12:9200/shop/good1/1?pretty http://192.168.1.12:9200/shop/good1/_search?pretty c.方法三 curl -H "Content-Type: application/json" -XPUT http://192.168.1.12:9200/shop6?pretty -d ' {"settings": { "number_of_shards": 3, "number_of_replicas": 2 }, "mappings": { "properties" :{ "title": { "type": "text" }, "price": { "type": "float" }, "stock": { "type": "integer" } } } }' curl -H "Content-Type: application/json" -XPUT http://192.168.1.12:9200/shop6/_doc/1?pretty -d ' {"title":"qqqqqqqqq","price":12,"stock":11}' 批量增加 curl -H "Content-Type: application/json" -PUT http://192.168.1.12:9200/shop6/_bulk?pretty --data-binary "@/tmp/add.json" vi /tmp/add.json {"index":{}} {"title":"qqqqqqqq12","price":12,"stock":11} {"index":{}} {"title":"qqqqqqqqq23","price":12,"stock":11}' {"index":{}} http://192.168.1.12:9200/shop6/_doc/1?pretty http://192.168.1.12:9200/shop6/_doc/_search?pretty 3、删除索引 curl -XDELETE ‘http://localhost:9200/{index}/{type}/{id}’ DELETE /my_index DELETE /index_one,index_two DELETE /index_* DELETE /_all 4、设置mapping 5、获取mapping curl -XGET http://localhost:9200/{index}/{type}/_mapping?pretty 6、搜索