安装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" }
    }
  }
}