elasticsearch基础操作02
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch基础操作02相关的知识,希望对你有一定的参考价值。
安装ES集群
准备三台机器: 192.168.1.10 192.168.1.11 192.168.1.12 三台机器分别部署elasticsearch 部署前,应该先检查机器是否有安装jdk,如果没有请先安装JDK,三台机器时间同步
安装方式非常简单,不会的请参考官方文档,下面我以elasticsearch-1.7.2来示例
rpm -ivh elasticsearch-1.7.2.noarch.rpm 安装完成修改配置文件: vim /etc/elasticsearch/elasticsearch.yml cluster.name: myes node.name: "linux-node1" #这里每台机器写对应的名字 修改此两项即可,保存退出,并启动 /etc/init.d/elasticsearch start
验证elasticsearch是否启动成功:
(1)ss -tanl 查看9200、9300端口是否开启 (2)使用curl命令 # curl -XGET 'http://192.168.1.10:9200' { "status" : 200, "name" : "linux-node1", "cluster_name" : "myes", "version" : { "number" : "1.7.2", "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec", "build_timestamp" : "2015-09-14T09:49:53Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline" : "You Know, for Search" } 显示"You Know, for Search"说明启动成功啦! (3)也可以使用浏览器测试 输入" 和curl命令结果应该是一样的
ES的插件plugins:
插件扩展ES的功能:
添加自定义的映射类型、自定义分析器、本地脚本、自定义发现方式;
安装方式:
1).直接将插件解压放置于plugins目录中即可
/usr/share/elasticsearch/plugins
2).使用plugins脚本安装
/usr/share/elasticsearch/bin/plugin
-h : 帮助
-l : 列出已安装的插件
-i,--install: 后面跟插件名
-r,--remove : 卸载
示例:
以192.168.1.10这台机器为主节点,比如我将网上下载的插件放置于/root/目录下,常用的几个插件kopf,head,bigdes,marvel /usr/share/elasticsearch/bin/plugin -i marvel -u /usr/share/elasticsearch/bin/plugin -i bigdesk -u /usr/share/elasticsearch/bin/plugin -i kopf -u /usr/share/elasticsearch/bin/plugin -i head -u [[email protected] ~]# /usr/share/elasticsearch/bin/plugin -l Installed plugins: - head - kopf - bigdesk - marvel 查看我们安装好的插件
浏览器输入"http://192.168.1.10:9200/_plugin/head"回车;
只需要更改_plugin/{bigdesk|kopf|marvel}即可,就不一一贴图了;
CURL操作相关的API:
_cat API:
查看帮助相关: [[email protected] ~]# curl -XGET '192.168.1.10:9200/_cat/?help' =^.^= /_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} 查看nodes相关: [[email protected] ~]# curl -XGET '192.168.1.10:9200/_cat/nodes' localhost 127.0.0.1 8 29 0.00 d m linux-node2 localhost 127.0.0.1 4 16 0.00 d * linux-node1 localhost 127.0.0.1 3 29 0.00 d m linux-node3 后面加?v,会更详细: [[email protected] ~]# curl -XGET '192.168.1.10:9200/_cat/nodes?v' host ip heap.percent ram.percent load node.role master name localhost 127.0.0.1 8 29 0.00 d m linux-node2 localhost 127.0.0.1 4 16 0.00 d * linux-node1 localhost 127.0.0.1 3 29 0.00 d m linux-node3 还可以自定义: [[email protected] ~]# curl -XGET '192.168.1.10:9200/_cat/nodes?h=name,ip,port,uptime,heap,current' linux-node2 127.0.0.1 9300 37.1m linux-node1 127.0.0.1 9300 37.8m linux-node3 127.0.0.1 9300 10.5m
_cluster API:
[[email protected] ~]# curl -XGET 'http://192.168.1.12:9200/_cluster/health' [[email protected] ~]# curl -XGET 'http://192.168.1.12:9200/_cluster/health?v' [[email protected] ~]# curl -XGET 'http://192.168.1.10:9200/_cluster/health?pretty' [[email protected] ~]# curl -XGET 'http://192.168.1.10:9200/_cluster/state/nodes?pretty' [[email protected] ~]# curl -XGET 'http://192.168.1.10:9200/_cluster/stats?pretty' curl -XGET 'http://192.168.1.10:9200/_nodes/stats?pretty'
看字面意思就能明白查看的是什么,结果就不一一贴图了......
下面看一下文档相关的
创建文档:
[[email protected] ~]# curl -XPUT 'http://192.168.1.10:9200/students/class1/1?pretty' -d ' > { > "first_name":"Jing", > "laste_name":"Guo", > "Gender":"Male", > "Age":25, > "courses":"Xianglong Shiba Zhang" > }' [[email protected] ~]# curl -XPUT 'http://192.168.1.10:9200/students/class1/2?pretty' -d ' { "first_name":"Rong", "laste_name":"Huang", "Gender":"Female", "Age":23, "courses":"Luoying Shenjian" }' 创建第二个文档时,注意看class1/2?pretty千万别和第一个重复了,不然就把第一个覆盖了
获取文档:
[[email protected] ~]# curl -XGET 'http://192.168.1.10:9200/students/class1/1?pretty' { "_index" : "students", "_type" : "class1", "_id" : "1", "_version" : 2, "found" : true, "_source": { "first_name":"Rong", "laste_name":"Huang", "Gender":"Female", "Age":23, "courses":"Luoying Shenjian" } }
更新文档:
PUT方法会覆盖原有文档;
如果只更新部分内容,得使用_update API:
[[email protected] ~]# curl -XPOST 'http://192.168.1.10:9200/students/class1/1/_update?pretty' -d ' > { > "doc":{"age":22} > }' { "_index" : "students", "_type" : "class1", "_id" : "1", "_version" : 3 } 再次获取,age已经改成22了: [[email protected] ~]# curl -XGET 'http://192.168.1.10:9200/students/class1/1?pretty' { "_index" : "students", "_type" : "class1", "_id" : "1", "_version" : 3, "found" : true, "_source":{"first_name":"Rong","laste_name":"Huang","Gender":"Female","Age":23,"courses":"Luoying Shenjian","age":22} }
删除文档:
操作不可逆,慎操作:
[[email protected] ~]# curl -XDELETE 'http://192.168.1.10:9200/students/class1/1'
删除索引:
[[email protected] ~]# curl -XDELETE 'localhost:9200/students'
查看索引:
[[email protected] ~]# curl -XGET 'localhost:9200/_cat/indices?v'
以上是关于elasticsearch基础操作02的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch代码片段,及工具类SearchEsUtil.java
ElasticSearch学习问题记录——Invalid shift value in prefixCoded bytes (is encoded value really an INT?)(代码片段