curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
tar -xvf elasticsearch-6.5.4.tar.gz
若是須要設置外網訪問須要修改配置文件綁定外網ip(修改elasticsearch-6.5.4/config/elasticsearch.yml):html
network.host: 192.168.60.201
sh bin/elasticsearch -d
檢查:瀏覽器請求http://xxx:9200/_cat/health?v&pretty
java
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1568168189 02:16:29 elasticsearch yellow 1 1 11 11 0 0 10 0 - 52.4%
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-linux-x86_64.tar.gz
tar -xvf kibana-6.5.4-linux-x86_64.tar.gz
修改配置文件kibana.yml:node
# 綁定外網ip,提供外網訪問 server.host: "192.168.60.201" # 綁定elasticsearch地址 elasticsearch.url: "http://192.168.60.201:9200"
sh kibana
http://192.168.60.201:5601
Elastic 本質上是一個分佈式數據庫,容許多臺服務器協同工做,每臺服務器能夠運行多個 Elastic 實例。單個 Elastic 實例稱爲一個節點(node)。一組節點構成一個集羣(cluster)。linux
Elastic 會索引全部字段,通過處理後寫入一個反向索引(Inverted Index)。查找數據的時候,直接查找該索引。因此,Elastic 數據管理的頂層單位就叫作 Index(索引)。它是單個數據庫的同義詞。每一個 Index (即數據庫)的名字必須是小寫。git
//查詢當前節點的全部Index GET _cat/indices
Index 裏面單條的記錄稱爲 Document(文檔)。許多條 Document 構成了一個 Index。Document 使用 JSON 格式表示,下面是一個例子。github
{ "_index" : "bank", "_type" : "account", "_id" : "10", "_score" : null, "_source" : { "account_number" : 10, "balance" : 46170, "firstname" : "Dominique", "lastname" : "Park", "age" : 37, "gender" : "F", "address" : "100 Gatling Place", "employer" : "Conjurica", "email" : "dominiquepark@conjurica.com", "city" : "Omar", "state" : "NJ" }
//列出全部index GET _cat/indices ----------------------- yellow open customer 8F4rLcOeQBG3URa0Qtslew 5 1 2 0 7.6kb 7.6kb green open .kibana_1 KBZkmY9gRAmy4V8WjvAsUQ 1 0 3 0 11.9kb 11.9kb yellow open bank 0WgLknyiSjiVe5XFj9UrSg 5 1 1000 0 483.4kb 483.4kb
//建立索引 PUT /customer ----------------------- { "acknowledged" : true, "shards_acknowledged" : true, "index" : "customer" }
//刪除索引 DELETE /customer ----------------------- { "acknowledged" : true }
數據準備 導入測試數據 這裏 加載它到咱們集羣中,以下所示 :數據庫
curl -H "Content-Type: application/json" -XPOST '192.168.60.201:9201/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
Search APIjson
根據account_number升序排序查詢全部的數據瀏覽器
GET /bank/_search?q=*&sort=account_number:desc
DSL(domain-specific language 領域特定語言)服務器
一樣是執行上面的邏輯,使用DSL,相似http請求請求體,更加直觀
GET /bank/_search { "query": {"match_all": {}}, "sort": [ { "account_number": { "order": "asc" } } ] }
更加複雜的查詢
// 全文檢索獲取全部balance在20000到30000之間的用戶 GET /bank/_search { "query":{ "bool":{ "must": [ {"match_all": {}} ], "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }
// 全文檢索全部地址包含Crosby或Avenue的用戶(全文匹配) GET /bank/_search { "query": { "match": { "address": "Crosby Avenue" } } }
// 短語匹配 match_phrase 短語匹配"Crosby Avenue" GET /bank/_search { "query": { "match_phrase": { "address": "Crosby Avenue" } } }
高亮搜索
//高亮匹配地址爲"Crosby Avenue" GET /bank/_search { "query": { "match_phrase": { "address": "Crosby Avenue" } }, "highlight":{ "fields": { "address": {} } } } -----------結果帶有<em>標籤的樣式--------- "highlight" : { "address" : [ "364 <em>Crosby</em> <em>Avenue</em>" ] }
聚合查詢(aggregations)
// 根據城市聚合查詢居住最多的城市 GET /bank/_search { "size": 10, "aggs": { "group_by_city": { "terms": { "field": "city.keyword" } } } } //---------結果--------- "aggregations" : { "group_by_city" : { "doc_count_error_upper_bound" : 5, "sum_other_doc_count" : 989, "buckets" : [ { "key" : "Belvoir", "doc_count" : 2 }, { "key" : "Aberdeen", "doc_count" : 1 }, { "key" : "Abiquiu", "doc_count" : 1 }, { "key" : "Abrams", "doc_count" : 1 } ] } }
本文是本人學習elasticsearch的學習總結,主要參考 Elasticsearch權威指南 和官網,只是一個關於Elasticsearch 基礎描述的教程。