1. 程式人生 > >TDH-Search常用命令

TDH-Search常用命令

let one move 保留 ear %20 做到 ati dex

一、指令部分:

1.search管理界面地址: http://172.20.230.110:9200/_plugin/head/

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?v‘(索引打開) curl -XPOST ‘localhost:9200/lxj.test_es_comm/_close?v‘ (索引關閉) 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 -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 -XPUT ‘localhost:9200/employee/dev/1?pretty‘ -d ‘{ "firstname": "小江", "lastname": "雷", "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 -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 ‘南京火車站‘ 二、知識點說明部分: 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保留字符,您需要使用 \ 進行轉譯: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

TDH-Search常用命令