Mac下安裝ElasticSearch和Kibana

趙明威發表於2017-10-03

安裝Elasticsearch

1.下載地址

https://www.elastic.co/downloads/elasticsearch

2.下載zip包解壓得到

elasticsearch-5.6.2

3.修改配置檔案(elasticsearch-5.6.2/config/elasticsearch.yml):

配置資料目錄和日誌目錄

path.data: /es/data  
path.logs: /es/logs  
network.host: 127.0.0.1
http.port: 9200
http.cors.enabled: true        #這個引數設定head外掛連線es叢集
http.cors.allow-origin: ”*”#同上(如果不設定最後這兩個引數head外掛連線不到es叢集)

4.啟動elasticsearch

nohup bin/elasticsearch > logs/es.log 2>&1 &

5,訪問頁面http://localhost:9200/

{
  "name" : "zqUUXVm",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qO20K0uEQoyESn--nospAA",
  "version" : {
    "number" : "5.6.2",
    "build_hash" : "57e20f3",
    "build_date" : "2017-09-23T13:16:45.703Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

安裝kibana

1.下載kibana安裝包:

https://www.elastic.co/cn/downloads/kibana

2.解壓kibana

開啟config/kibana.yml , 配置elasticsearch.url: "http://localhost:9200”;

3.執行

bin/kibana

Elasticsearch基礎練習

ELasticsearch使用Javascript物件符號(JavaScript Object Notation),也就是JSON,作為文件序列化格式 以下使用JSON文件來表示一個使用者物件:

{
    "email":      "john@smith.com",
    "first_name": "John",
    "last_name":  "Smith",
    "info": {
        "bio":         "Eco-warrior and defender of the weak",
        "age":         25,
        "interests": [ "dolphins", "whales" ]
    },
    "join_date": "2014/05/01"
}

向Elasticsearch發出的請求的組成部分與其它普通的HTTP請求是一樣的:

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

  • VERB HTTP方法:GET, POST, PUT, HEAD, DELETE
  • PROTOCOL http或者https協議(只有在Elasticsearch前面有https代理的時候可用)
  • HOST Elasticsearch叢集中的任何一個節點的主機名,如果是在本地的節點,那麼就叫localhost
  • PORT Elasticsearch HTTP服務所在的埠,預設為9200
  • PATH API路徑(例如_count將返回叢集中文件的數量),PATH可以包含多個元件,例如_cluster/stats或者_nodes/stats/jvm
  • QUERY_STRING 一些可選的查詢請求引數,例如?pretty引數將使請求返回更加美觀易讀的JSON資料
  • BODY 一個JSON格式的請求主體(如果請求需要的話)

舉例:

curl 'localhost:9200/_cat/health?v'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign 
pending_tasks max_task_wait_time active_shards_percent
1506845232 16:07:12  elasticsearch yellow          1         1      1   1    0    0        1             0                  
                     -                 50.0%

curl 'localhost:9200/_cat/nodes?v'
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           10         100  15    2.30                  mdi       *      zqUUXVm

curl 'localhost:9200/_cat/indices?v'
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana Ay5LH_fLQyacGBnGaRrCIA   1   1          1            0      3.2kb          3.2kb

curl -XPUT 'localhost:9200/employee?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "employee"
}

curl 'localhost:9200/_cat/indices?v'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana  Ay5LH_fLQyacGBnGaRrCIA   1   1          1            0      3.2kb          3.2kb
yellow open   employee CghUPYq6RoC_oR8YAngYgg   5   1          0            0       810b           
810b

curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 327

{
  "name" : "zqUUXVm",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qO20K0uEQoyESn--nospAA",
  "version" : {
    "number" : "5.6.2",
    "build_hash" : "57e20f3",
    "build_date" : "2017-09-23T13:16:45.703Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

例如_count將返回叢集中文件的數量,QUERY_STRING 引數: ?pretty引數將使請求返回更加美觀易讀的JSON資料

curl -XGET 'http://localhost:9200/_count?pretty'; -d '
quote> {
quote>     "query": {
quote>         "match_all": {}
quote>     }
quote> }
quote>
quote> '
{
  "count" : 1,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  }
}

我們看不到HTTP頭是因為我們沒有讓curl顯示它們,如果要顯示,使用curl命令後跟-i引數:

curl -i -XGET 'http://localhost:9200';
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 327

{
  "name" : "zqUUXVm",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qO20K0uEQoyESn--nospAA",
  "version" : {
    "number" : "5.6.2",
    "build_hash" : "57e20f3",
    "build_date" : "2017-09-23T13:16:45.703Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

為了方便,使用kibana的DevTools進行執行

GET _search
{
  "query": {
    "match_all": {}
  }
}

GET _count
{
  "query": {
    "match_all": {

    }
  }
}



PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

GET /megacorp/employee/1

GET /megacorp/employee/_search

GET /megacorp/employee/_search?q=last_name:Smith

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

聚合,找出有共同興趣的人

# es進行聚合操作時提示Fielddata is disabled on text fields by default
PUT megacorp/_mapping/employee/
{
  "properties": {
    "interests": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

GET /megacorp/employee/_search
{

  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

GET /megacorp/employee/_search
{
"query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

相關文章