Elastic search 基本使用

1. elasticsearch 命令的基本格式

RESTful接口URL的格式:html

http://localhost:9200/<index>/<type>/[<id>]node

其中index、type是必須提供的。id是可選的,不提供es會自動生成。index、type將信息進行分層,利於管理。index能夠理解爲數據庫;type理解爲數據表;id至關於數據庫表中記錄的主鍵,是惟一的。python

:在url網址後面加"?pretty",會讓返回結果以工整的方式展現出來,適用全部操做數據類的url。"?"表示引出條件,"pretty"是條件內容。linux

2. elasticsearch基本的增刪改

2.1 elasticSearch增長

向store索引中添加一些書籍數據庫

1
2
3
4
5
6
7
8
9
curl  - "Content-Type: application/json"  - XPUT  'http://192.168.187.201:9200/store/books/1?pretty'  - d '{
   "title" "Elasticsearch: The Definitive Guide" ,
   "name"  : {
     "first"  "Zachary" ,
     "last"  "Tong"
   },
   "publish_date" : "2015-02-06" ,
   "price" : "49.99"
}'

注:curl是linux下的http請求,-H "Content-Type: application/json"須要添加,不然會報錯{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}json

加"pretty"瀏覽器

不加"pretty"緩存

加"pretty"與不加"pretty"的區別就是返回結果工整與不工整的差異,其餘操做相似。爲了使返回結果工整,如下操做都在url後添加"pretty"數據結構

2.2 elasticSearch刪除

 刪除一個文檔app

1
curl  - XDELETE  'http://hadoop1:9200/store/books/1?pretty'

 

2.3 elasticSearch更新(改)

①能夠經過覆蓋的方式更新

1
2
3
4
5
6
7
8
9
curl  - "Content-Type:application/json"  - XPUT  'http://hadoop1:9200/store/books/1?pretty'  - d '{
   "title" "Elasticsearch: The Definitive Guide" ,
   "name"  : {
     "first"  "Zachary" ,
     "last"  "Tong"
   },
   "publish_date" : "2016-02-06" ,
   "price" : "99.99"
}'

② 經過_update API的方式單獨更新你想要更新的

1
2
3
4
5
curl  - "Content-Type: application/json"  - XPOST  'http://hadoop1:9200/store/books/1/_update?pretty'  - d '{
   "doc" : {
      "price"  88.88
   }
}'

3. elasticSearch查詢

elasticSearch查詢分三種,一是瀏覽器查詢,二是curl查詢,三是請求體查詢GET或POS。

:採用_search的模糊查詢(包括bool過濾查詢、 嵌套查詢、range範圍過濾查詢等等),url能夠沒必要指定type,只用指定index查詢就行,具體例子看"2.1.4 elasticSearch查詢 ③query基本匹配查詢"節點的具體查詢實例

3.1 瀏覽器查詢

經過瀏覽器IP+網址查詢

1
http: / / hadoop1: 9200 / store / books / 1 ?pretty

3.2 在linux經過curl的方式查詢

3.2.1 經過ID得到文檔信息

1
curl  - XGET  'http://hadoop1:9200/store/books/1?pretty'

3.2.2 經過_source獲取指定的字段

1
2
3
curl  - XGET  'http://hadoop1:9200/store/books/1?_source=title&pretty'
curl  - XGET  'http://hadoop1:9200/store/books/1?_source=title,price&pretty'
curl  - XGET  'http://hadoop1:9200/store/books/1?_source&pretty'

3.2.3 query基本匹配查詢

查詢數據前,能夠批量導入1000條數據集到elasticsearch裏,具體參考"4 elasticSearch批處理命令 4.1 導入數據集"節點,以便數據查詢方便。

① "q=*"表示匹配索引中全部的數據,通常默認只返回前10條數據。

1
2
3
4
5
6
7
curl  'hadoop1:9200/bank/_search?q=*&pretty'
 
#等價於:
curl  - "Content-Type:applicatin/json"  - XPOST  'localhost:9200/bank/_search?pretty'  - d '
{
    "query" : {  "match_all" : {} }
}'

② 匹配全部數據,但只返回1個

1
2
3
4
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
    "query" : { "match_all" : {}},
    "size" 1
}'

注:若是size不指定,則默認返回10條數據。

③ 返回從11到20的數據(索引下標從0開始)

1
2
3
4
5
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
    "query" : {「match_all」: {}},
    "from" 10 ,
    "size" 10
}

④ 匹配全部的索引中的數據,按照balance字段降序排序,而且返回前10條(若是不指定size,默認最多返回10條)

1
2
3
4
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
    "query" : { "match_all" : {}},
    "sort" : { "balance" :{ "order" "desc" }}
}'

⑤ 返回特定的字段(account_number balance) ,與②經過_source獲取指定的字段相似

1
2
3
4
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
    "query" : { "match_all" : {}},
    "_source" : [ "account_number" "balance" ]
}'

⑥ 返回account_humber爲20的數據

1
2
3
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
    "query" : { "match" : { "account_number" : 20 }}
}'

⑦ 返回address中包含mill的全部數據

1
2
3
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
      "query" : { "match" :{ "address" "mill" }}
}'

⑧ 返回地址中包含mill或者lane的全部數據

1
2
3
curl  - "Content_Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
     「query ": {" match ": {" address ": " mill lane"}}
}'

⑨ 與第8不一樣,多匹配(match_phrase是短語匹配),返回地址中包含短語"mill lane"的全部數據

1
2
3
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
     "query" : { "match_phrase" : { "address" "mill lane" }}
}'

3.2.4 bool過濾查詢,能夠作組合過濾查詢、嵌套查詢等

SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND (publish_date != "2016-02-06")

相似的,Elasticsearch也有 and, or, not這樣的組合條件的查詢方式,格式以下:

1
2
3
4
5
6
7
8
{
       bool 「 : {
              "filter" :   [],
              "must"  :    [],
              "should" :   [],
              "must_not" : []
        }
}

說明: 
         filter:過濾
         must:條件必須知足,至關於and
         should:條件能夠知足也能夠不知足,至關於or
         must_not:條件不須要知足,至關於not

3.2.4.1 filter查詢

①filter指定單個值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# SELECT * FROM books WHERE price = 35.99
# filtered 查詢價格是35.99的
curl  - "Content-Type:application/json"  - XGET  'http://hadoop1:9200/store/books/_search?pretty'  - d '{
     "query"  : {
         "bool"  : {
             "must"  : {
                 "match_all"  : {}
             },
             "filter"  : {
                 "term"  : {
                     "price"  35.99
                   }
              }
         }
     }
}'

注:帶有key-value鍵值對的都須要加 -H 「Content-Type: application/json」

②filter指定多個值

1
2
3
4
5
6
7
8
9
10
11
curl  - XGET  'http://hadoop1:9200/store/books/_search?pretty'  - d '{
        "query"  : {
              "bool"  : {
                   "filter"  : {
                        "terms"  : {
                               "price"  : [ 35.99 99.99 ]
                         }
                   }
              }
         }
}'

3.2.4.2 must、should、must_not查詢

①must、should、must_not與term結合使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
curl  - "Content-Type:application/json"  - XGET  'http://hadoop1:9200/store/books/_search?pretty'  - d '{
     "query"  : {
          "bool"  : {
               "should"  : [
                    "term"  : { "price"  35.99 }},
                    "term"  : { "price"  99.99 }}
                ],
                "must_not"  : {
                      "term"  : { "publish_date"  "2016-06-06" }
                 }
          }
     }
}'

②must、should、must_not與match結合使用

bool表示查詢列表中只要有任何一個爲真則認爲匹配:

1
2
3
4
5
6
7
8
9
10
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
     "query" : {
          "bool" : {
               "must_not" : [
                     { "match" : { "address" "mill" }},
                     { "match" : { "address" "lane" }}
                ]
          }
      }
}'

返回age年齡大於40歲、state不是ID的全部數據:

1
2
3
4
5
6
7
8
9
10
11
12
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
      "query" : {
           "bool" : {
                "must" : [
                      { "match" : { "age" "40" }}
                 ],
                 "must_not" : [
                       { "match" : { "state" "ID" }}
                 ]
           }
      }
}'

3.2.4.3 bool嵌套查詢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 嵌套查詢
# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 )
curl  - "Content-Type:application/json"  - XGET  'http://hadoop1:9200/store/books/_search?pretty'  - d '{
     "query"  : {
          "bool"  : {
               "should"  : [
                    "term"  : { "price"  35.99  }},
                    "bool"  : {
                            "must"  : [
                                 "term"  : { "publish_date"  "2016-06-06" }},
                                 "term"  : { "price"  99.99 }}
                             ]
                        }
                     }
               ]
          }           
     }
}'       

3.2.4.4 filter的range範圍過濾查詢

第一個示例,查找price價錢大於20的數據:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# SELECT * FROM books WHERE price >= 20 AND price < 100
# gt :  > 大於
# lt :  < 小於
# gte :  >= 大於等於
# lte :  <= 小於等於
 
curl  - "Content-Type:application/json"  - XGET  'http://hadoop1:9200/store/books/_search?pretty'  - d '{
     "query"  : {
          "bool"  : {
               "filter"  : {
                    "range"  : {
                         "price"  : {
                              "gt"  20.0 ,
                              "boost"  4.0
                          }
                    }
               }
          }
     }
}'

注:boost:設置boost查詢的值,默認1.0

第二個示例,使用布爾查詢返回balance在20000到30000之間的全部數據:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
      "query" : {
          "bool" : {
              "must" : { "match_all" : {}},
              "filter" : {
                    "range" : {
                          "balance" : {
                               "gte" 20000 ,
                                "lte" 30000
                          }
                     }
              }
          }
      }
}'

3.2.4 elasticSearch聚合查詢

第一個示例,將全部的數據按照state分組(group),而後按照分組記錄數從大到小排序(默認降序),返回前十條(默認)

1
2
3
4
5
6
7
8
9
10
curl  - "Content-Type:application/json"  - XPOST   'hadoop1:9200/bank/_search?pretty'  - d '{
    "size" 0 ,
    "aggs" : {
        "group_by_state" : {
             "terms" : {
                  "field" "state"
             }
        }
    }
}'

可能遇到的問題:elasticsearch 進行排序的時候,咱們通常都會排序數字、日期,而文本排序則會報錯:Fielddata is disabled on text fields by default. Set fielddata=true on [state] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.如:

解決方案:5.x後對排序,聚合這些操做,用單獨的數據結構(fielddata)緩存到內存裏了,須要單獨開啓,官方解釋在此fielddata。聚合前執行以下操做,用以開啓fielddata:

1
2
3
4
5
6
7
8
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_mapping/account?pretty'  - d '{
"properties" : {
     "state" : {
         "type" "text" ,
         "fielddata" : true
      }                          
  }          
}'

說明:bank爲index,_mapping爲映射,account爲type,這三個要素爲必須,」state「爲聚合"group_by_state"操做的對象字段

聚合查詢成功示例:

第二個示例,將全部的數據按照state分組(group),降序排序,計算每組balance的平均值並返回(默認)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/_search?pretty'  - d '{
     "size" 0 ,
     "aggs" : {
         "group_by_state" : {
              "terms" : {
                   "field" "state"
              },
               "aggs" : {
                    "average_balance" : {
                         "avg" : {
                               "field" : "balance"
                          }
                     }
               }
         }
     }
}'

4. elasticSearch批處理命令

4.1 導入數據集

你能夠點擊這裏下載示例數據集:accounts.json

導入示例數據集:

1
2
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/account/_bulk?pretty'  - - data - binary  "@accounts.json"
curl  - "Content-Type:application/json"  - XPOST  'hadoop1:9200/bank/account/_bulk?pretty'  - - data - binary  "@/home/hadoop/accounts.json"

:_bulk表示批量處理,"@accounts.json"或者"@/home/hadoop/accounts.json"能夠用相對路徑或者絕對路徑表示。

查看accounts.json導入狀況,使用 curl 'hadoop1:9200/_cat/indices?v' 

能夠看到已經成功導入1000條數據記錄

4.2 批量建立索引

  

5. elasticSearch其餘經常使用命令

注:url後面的"v"表示 verbose 的意思,這樣能夠更可讀(有表頭,有對齊),若是不加v,不會顯示錶頭

5.1. 查看全部index索引,輸入命令   curl 'hadoop1:9200/_cat/indices?v'

說明:index:索引爲store,pri:5個私有的分片,rep:1個副本,docs.count:store索引裏面有2個文檔(即裏面有2條數據記錄),docs.deleted:刪除了0條記錄,store.size:數據存儲總大小(包括副本),pri.store.size:分片數據存儲的大小。

不加v,不會顯示錶頭,可讀性差

5.2. 檢測集羣是否健康,確保9200端口號可用 curl 'hadoop1:9200/_cat/health?v'

5.3. 獲取集羣的節點列表  curl 'hadoop1:9200/_cat/nodes?v'

 

轉載自https://www.cnblogs.com/swordfall/p/8923087.html

相關文章
相關標籤/搜索