一、指令部分:
1.search管理界面地址:
2.集群状态查看命令:
curl -XGET 'localhost:9200/_cluster/health?pretty'
curl -XGET 'localhost:9200/_cat/health?v'
检查shards unsigned原因
curl 'localhost:9200/_cat/shards?h=index,shard,prirep,state,docs,store,ip,node,un*&v' | grep UNAS
检查shards unsigned原因
curl -XGET localhost:9200/_cat/shards?v | grep UNA
3.查看集群的节点数目和主节点等信息,如下:
curl -XGET 'localhost:9200/_cat/nodes?v'
4.查看index的索引信息:
curl -XGET 'localhost:9200/lxj.test_es_comm/_settings?pretty'
curl -XGET 'localhost:9200/_clus ter/health/employee?pretty' (查看索引健康状态)
curl -XGET 'localhost:9200/_cluster/health/?level=indices&pretty'(查看全部索引的健康状态)
curl -XGET 'localhost:9200/_cluster/health/?level=shards&pretty' (查看分片级别的健康状态)
curl -XPOST 'localhost:9200/lxj.test_es_comm/_open?pretty'(索引打开)
curl -XPOST 'localhost:9200/lxj.test_es_comm/_close?pretty' (索引关闭)
curl -s 'localhost:9200/_cat/indices?v' |grep red (查看red状态的索引)
5.查看index的表结构:
curl -XGET 'localhost:9200/default.test_people_es/_mappings?pretty'
6.通过Curl使用RestAPI的格式:
curl -X<VERB> '<HOST>:9200/<PATH>/[<API>]/[?<PARAMETERS>]' [-d '{<BODY>}']
7.创建index:
curl -XPUT '172.20.230.110:9200/test/?pretty'
删除index:
curl -XDELETE 'localhost:9200/test/?pretty'
8.编入文档:(如果有会覆盖)
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/employee/dev/1?pretty' -d '{
"firstname": "San",
"lastname": "Zhang",
"age": 26,
"on_board_date": "2015-10-31",
"hometown": "Beijing",
"school": "Nanjing University",
"married": false,
"about": "I love Beijing Opera"
}'
输出:
{
"_index" : "employee", index名。
"_type" : "dev", Type名。
"_id" : "1", 这条Document的ID。。
"_version" : 1, 版本号,每执行依次命令就加1。
"_shards" : { shard数目及状态。
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true true表示第一次创建。
}
9.查看/employee/dev/1下是否存在Document
curl -i -XHEAD 'localhost:9200/employee/dev/1?pretty'
10.获取/employee/dev/1下的Document
curl -XGET 'localhost:9200/employee/dev/1?pretty'
11.更新文档
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/employee/dev/1?pretty' -d '{
"firstname": "aa",
"lastname": "s",
"age": 27,
"on_board_date": "1995-02-02",
"hometown": "南京",
"school": "28所老年大学",
"married": false,
"about": "铁肩担大任,冲上山顶论英雄,联合起来办大事,做就做到最好,让创新成为习惯,共享才能共赢,创造幸福而有尊严的生活"
}'
12.获取/employee/dev/1中的 name 和 age 字段,多个字段用“,”隔开
curl -XGET 'localhost:9200/employee/dev/1?_source=firstname,lastname,age&pretty'
13.获取source部分的数据
curl -XGET 'localhost:9200/employee/dev/1/_source?pretty'
14.新建一个document,会自动生成rowkey:
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/employee/sales?pretty' -d '{
"firstname": "Lei",
"lastname": "Li",
"age": 28,
"on_board_date": "2013-10-03",
"hometown": "Hangzhou",
"school": "Zhejiang University",
"married": true,
"about": "I appear in your English textbook."
}'
15.查询一条记录;
curl -XGET 'localhost:9200/employee/_search?pretty&q=lastname:"wang"'
16.设置副本数:
curl -XPUT 'localhost:9200/employee/_settings?pretty' -d '{
"number_of_replicas": 2
}'
16.设置刷新间隔(入库30秒后才能查询到数据,原本是入库一秒就可以查询到入库的数据,
es要做很多合并的操作,会占用系统资源,降低入库速度)
curl -XPUT 'localhost:9200/employee/_settings' -d '
{
"index" :{
"refresh_interval" : "30s"
}
}
'
17.设置segment大小
curl -XPOST 'localhost:9200/employee/_optimize?max_num_segments=3&pretty'
18.设置批量插入的队列大小
curl -XPUT 'localhost:9200/_cluster/settings' -d '
{
"persistent":{
"indices.store.throttle.max_bytes_per_sec" : "100mb",
"threadpool.bulk.queue_size" : "10000"
}
}
'
19.查看es集群nodes状态,是否挂掉
curl -XGET 'localhost:9200/_cat/nodes?pretty'
20.查看所有节点的统计数据
curl -XGET 'localhost:9200/_nodes/stats?pretty'
curl -XGET 'localhost:9200/_nodes/gz230-110/stats?pretty' (查看指定节点的状态)
21.空检索
curl -XGET 'localhost:9200/_search?pretty'
22.指定条件检索
curl -XGET 'localhost:9200/employee/sales/_search'
curl -XGET 'localhost:9200/employee/sales,dev/_search'
curl -XGET 'localhost:9200/employee/dev/_search?pretty&q=lastname:Li' (uri检索)
23.请求体检索:
curl -XPOST 'localhost:9200/employee/dev/_search?pretty' -d '
{
"query" : {
"match_phrase" : {
"lastname" : "Li"
}
}
}
'
24.指定条件URI检索
curl -XGET 'localhost:9200/employee/_search?q=Beijing&df=school'(默认查询字段为school)
curl -XGET 'localhost:9200/employee/_search?q=school:(Beijing%20AND%20University)'(AND查询)
curl -XGET 'localhost:9200/employee/_search?q=school:(Beijing%20OR%20University)'(OR查询)
curl -XGET 'localhost:9200/employee/_search?q=*&from=1&size=10'(指定返回从第几条开始,几条结果)
curl -XGET 'localhost:9200/default.test_people_es_1/_search?q=*&sort=id:asc&pretty' (排序)
curl -XGET 'localhost:9200/employee/_search?q=(lastname:li)%20AND%20(school:beijing)'(查询条件AND)
curl -XGET 'localhost:9100/employee/_search?q=+school:nanjing+school:beijing' (+操作符)
curl -XGET 'localhost:9100/employee/_search?q=-school:university' (-操作符)
curl -XGET 'localhost:9200/employee/_search?q=school=(beijing%20AND%20university)%20OR%20(na%20AND%20university)' (括号的使用)
curl -XGET 'localhost:9200/employee/_search?q=??d*' (?匹配任意一个字符,*匹配任意个数的字符)
curl -XGET 'localhost:9200/employee/_search?q=on_board_date:(>2014-01-01%20AND%20<2015-12-31)' (日期范围查询)
25.按照querybody检索:
curl -XGET '172.20.230.110:9200/default.test_people_es/_search?pretty' -d '{
"query": {
"bool" : {
"must" : {
"term":{
"sex": "女"
}
}
}
}
}'
26.移动分片
假设我们有两个节点:es_node_one和es_node_two,ElasticSearch在es_node_one节点上分配了ops索引的两个分片,
我们现在希望将第二个分片移动到es_node_two节点上。可以如下操作实现:
# curl -XPOST "http://ESnode:9200/_cluster/reroute' -d '{
"commands" : [ {
"move" : {
"index" : "ops",
"shard" : 1,
"from_node" : "es_node_one",
"to_node" : "es_node_two"
}
}]
}'
27.分配分片
我们还可以将一个未分配的分片分配到一个指定的节点上。假设ops索引上有一个编号为0的分片尚未分配,
并且我们希望ElasticSearch将其分配到es_node_two上,可以运行如下命令操作:
# curl -XPOST "http://ESnode:9200/_cluster/reroute' -d '{
"commands" : [ {
"allocate" : {
"index" : "ops",
"shard" : 0,
"node" : "es_node_two"
}
} ]
}'
28.取消分配
如果希望取消一个正在进行的分配过程,我们通过运行cancel命令来指定我们希望取消分配的索引、节点以及分片,如下所示:
# curl -XPOST "http://ESnode:9200/_cluster/reroute" -d '{
"commands" : [ {
"cancel" : {
"index" : "ops",
"shard" : 0,
"node" : "es_node_one"
}
} ]
}'
创建索引:
# curl -XPOST "http://ESnode:9200/katoey"
分配索引在某一节点:
# curl -XPUT "http://ESnode:9200/katoey/_settings" -d '{
"index.routing.allocation.include.zone": "zone_one,zone_two"
}'
# curl -XPUT "http://ESnode:9200/_cluster/settings" -d '{
"transient" : {
"cluster.routing.allocation.include._ip" "10.0.1.112,10.0.1.114"
}
}'
设置每个节点上的分片为1:
# curl -XPUT "http://ESnode:9200/ops/_settings" -d '{
"index.routing.allocation.total_shards_per_node" : 1
}'
29.测试分词,analyzer=ik :用于指定分词器名;上述的四个分词器(english,standard,ik,mmseg)都可以指定
curl -XPOST ‘localhost:9200/_analyze?analyzer=ik&&pretty’ -d '南京火车站'
30、新建索引时设定shards和replicas
curl -XPUT http://localhost:9200/blog/ -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
}
}'
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
}
}'
31、内容修改
curl -H "Content-Type: application/json" -XPOST '192.168.200.100:9200/employee/dev/1/_update?pretty' -d '{"hometown":"上海"}'' 报错:
"上海"}'' { "error" : { "root_cause" : [ { "type" : "action_request_validation_exception", "reason" : "Validation Failed: 1: script or doc is missing;" } ], "type" : "action_request_validation_exception", "reason" : "Validation Failed: 1: script or doc is missing;" }, "status" : 400 }
解决:
curl -H "Content-Type: application/json" -XPOST '192.168.200.100:9200/employee/dev/1/_update?pretty' -d '{"doc":{"hometown":"上海"}}'
二、知识点说明部分:
1.边框为深色的分片是index的主分片,其他为主分片的副本;
2.方框中的数字表示同一索引的不同分片,相同数字表示同一分片的不同副本;
3.索引的主分片的数目,建表后不可改;
4.transwarp Search的含有三个特殊的数据对象:Index,Type,Document,Field,它们与传统二维表的映射关系如下:
Index(索引)-->Table(表)
Document-->Row(行)
Field(字段)-->Column(列)
Type 是Index的逻辑上的分类,不映射为传统二维表中的数据对象。
5.如果一个Index的 Replica 数大于或等于集群中节点数量,这个Index中将会有分片无法分配到节点上;
6.如果您需要在 <QUERY_STRING> 中包含以下Transwarp Search保留字符,您需要使用 进行转译:
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : /
三、注意事项:
elasticsearch 6.x一个index下不支持创建多个type,并且官方说是在接下来的7.0版本中会删掉type
如果在一个index下已经存在一个type会报错:
[root@master ~]# curl -H "Content-Type: application/json" -XPOST 'localhost:9200/books/solr/1?pretty' -d '{"title":"Apache Solr 4 Cookbook", > "published": 2012}' { "error" : { "root_cause" : [ { "type" : "illegal_argument_exception", "reason" : "Rejecting mapping update to [books] as the final mapping would have more than 1 type: [solr, es]" } ], "type" : "illegal_argument_exception", "reason" : "Rejecting mapping update to [books] as the final mapping would have more than 1 type: [solr, es]" }, "status" : 400 }
四、一些参数
1、关闭自动创建索引
在elasticsearch.yml配置文件中添加以下指令来关闭自动创建索引:
action.auto_create_index: false
curl -H "Content-Type: application/json" -XPOST '192.168.200.100:9200/employee/dev/1/_update?pretty' -d '{"doc":{"hometown":"上海"}}'