一、Elasticsearch安装使用教程

环境准备:
1、预先安装jdk7(Elasticsearch 官方推荐使用 Oracle JDK 7)

Elasticsearch是一个全文搜索引擎。

2、安装
第一步:安装es
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip
unzip elasticsearch-1.7.2.zip
cd elasticsearch

测试正常启动:./bin/elasticsearch
haow@Lenovo:/haow/es/elasticsearch-1.7.2$ ./bin/elasticsearch
[2015-09-16 16:58:48,128][INFO ][node                     ] [Ebon Seeker] version[1.7.2], pid[19814], build[e43676b/2015-09-14T09:49:53Z]
[2015-09-16 16:58:48,129][INFO ][node                     ] [Ebon Seeker] initializing ...
[2015-09-16 16:58:48,226][INFO ][plugins                  ] [Ebon Seeker] loaded [], sites []
[2015-09-16 16:58:48,420][INFO ][env                      ] [Ebon Seeker] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [337.1gb], net total_space [450.5gb], types [ext4]
[2015-09-16 16:58:50,850][INFO ][node                     ] [Ebon Seeker] initialized
[2015-09-16 16:58:50,850][INFO ][node                     ] [Ebon Seeker] starting ...
[2015-09-16 16:58:51,036][INFO ][transport                ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.20.5.14:9300]}
[2015-09-16 16:58:51,063][INFO ][discovery                ] [Ebon Seeker] elasticsearch/vpM2w98XRr-IXA5FttPVGA
[2015-09-16 16:58:54,834][INFO ][cluster.service          ] [Ebon Seeker] new_master [Ebon Seeker][vpM2w98XRr-IXA5FttPVGA][Lenovo][inet[/172.20.5.14:9300]], reason: zen-disco-join (elected_as_master)
[2015-09-16 16:58:54,846][INFO ][http                     ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.20.5.14:9200]}
[2015-09-16 16:58:54,847][INFO ][node                     ] [Ebon Seeker] started
[2015-09-16 16:58:55,865][INFO ][gateway                  ] [Ebon Seeker] recovered [0] indices into cluster_state

没报错表示安装成功,Ctrl+c 停掉

elasticsearchservicewrapper安装
这个是对elasticsearch执行命令的包装服务,安装之后,方便elasticsearch的启动,停止等等操作。
(1)下载elasticsearchservicewrapper
git clone https://github.com/elasticsearch/elasticsearch-servicewrapper,然后将目录下的service目录拷贝至ES_HOME/bin目录下。
(2)简单配置jvm的内存
修改ES_HOME/bin/service/elasticsearch.conf,set.default.ES_HEAP_SIZE=1024,该值根据机器的配置可自定义。
(3)安装启动服务
执行命令:ES_HOME/bin/service/elasticsearch install
(4)启动/停止/重启服务
执行命令:ES_HOME/bin/service/elasticsearch start/stop/restart

第二步、安装plugin(部分可选)
集群安装成功之后,需要对集群中的索引数据、运行情况等信息进行查看,索引需要安装一些插件,方面后续工作。工欲善其事必先利其器,下面介绍几个实用的插件:

Marvel
Marvel是一个管理和监控Elasticsearch的工具。它提供一个叫Sense的交互式接口方便通过浏览器与Elasticsearch交互。(建议安装)
命令:bin/plugin -i elasticsearch/marvel/latest

如果不想使用Marvel监控本地集群,可以使用如下方式关闭Marvel监控
echo 'marvel.agent.enabled: false' >> ./config/elasticsearch.yml可以通过前台的方式启动Elasticsearch

bin]$ sudo ./elasticsearch
使用-d参数可以将Elasticsearch放到后台运行
bin]$ sudo ./elasticsearch -d

head
和插件名字一样,首推这个插件。通过head,可以查看集群几乎所有信息,还能进行简单的搜索查询,观察自动恢复的情况等等。
执行下面的命令开始安装:sudo ./bin/plugin -install mobz/elasticsearch-head
记得两台机器都安装一下哦。安装完成之后,
可以通过浏览器输入:http://ip:9200/_plugin/head/ ,可以查看显示效果。如下图:

还有很多功能,请读者自己去体验。
bigdesk
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等。项目git地址: https://github.com/lukas-vlcek/bigdesk。和head一样,它也是个独立的网页程序,使用方式和head一样。
插件安装运行:
方法1:
1.bin/plugin -install lukas-vlcek/bigdesk
2.运行es
3.打开http://localhost:9200/_plugin/bigdesk/
当然,也可以直接下载源码运行index.html
方法2:
1.https://github.com/lukas-vlcek/bigdesk下载zip 解压
2.建立elasticsearch-1.0.0\plugins\bigdesk\_site文件
3.将解压后的bigdesk-master文件夹下的文件copy到_site
4.运行es
5.打开http://localhost:9200/_plugin/bigdesk/

kopf
Kopf是一个ElasticSearch的管理工具,它也提供了对ES集群操作的API。
./elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf/{branch|version}
open http://localhost:9200/_plugin/kopf

./plugin --install lmenezes/elasticsearch-kopf/v1.6.1

3、简要配置和访问命令
通过config/elasticsearch.yml设置cluster.name和node.name
可以通过以下方式关闭Elasticsearch
curl -XPOST 'http://localhost:9200/_shutdown'

Talking to Elasticsearch
根据是否使用Java语言,与Elasticsearch交互有几种方法,如果是Java API参见文档
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
如果使用其他语言,则使用Elasticsearch提供的RESTFUL API,或者可以直接使用linux命令curl访问

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/?<QUERY_STRING>' -d '<BODY>'
VER        HTTP请求方式,GET,POST,PUT,HEAD或DELETE
PROTOCOL   使用HTTP或者HTTPS
HOST       es集群中的任意一个node的主机名,如果是在node本机就直接使用localhost
PORT       Elasticsearch运行HTTP服务的端口,默认是9200
QUERY_STRING  查询参数
BODY       JSON格式的请求数据

4、测试

检查es:
查看Elasticsearch中的数据
$ curl "http://localhost:9200/?pretty"
{
  "status" : 200,
  "name" : "xxx",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.2",
    "build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
    "build_timestamp" : "2014-12-16T14:11:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}

通过config/elasticsearch.yml设置cluster.name和node.name

$ curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
{
  "count" : 22692,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  }
}

$ curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 334
 
{
  "status" : 200,
  "name" : "jidong",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.2",
    "build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
    "build_timestamp" : "2014-12-16T14:11:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}

Relational DB  Databases    Tables  Rows    Columns
Elasticsearch  Indices    Types  Documents  Fields

测试Marvel
通过Marvel的Sense接口访问Elasticsearch
http://xxxx.com:9200/_plugin/marvel/sense/index.html
以下直接使用GET或PUT的简略形式,都是直接使用Marvel的Sense接口访问Elasticsearch,可以点击“Copy as cURL”查看对应的curl命令写法

Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。

a、存放数据
PUT /megacorp/employee/1
{
    "first_name":"John",
    "last_name":  "Smith",
    "age":        25,
    "about":      "I love to go rock climbing",
    "interests":["sports","music"]
}
/megacorp/employee/1
这个路径包含三个信息
megacorp 索引名称,类似关系型数据库的数据库名称
employee 类型名称,类似关系型数据库的表名称
1    特定employee的ID

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"]
}

2、查询数据
在Sense中输入
GET /megacorp/employee/1
显示结果

{
  "_index":   "megacorp",
  "_type":    "employee",
  "_id":      "1",
  "_version":1,
  "found":    true,
  "_source":  {
      "first_name":  "John",
      "last_name":   "Smith",
      "age":         25,
      "about":       "I love to go rock climbing",
      "interests":  ["sports","music"]
  }
}

GET /megacorp/employee/_search
{
   "took":      6,
   "timed_out":false,
   "_shards":{...},
   "hits":{
      "total":      3,
      "max_score":  1,
      "hits":[
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "3",
            "_score":         1,
            "_source":{
               "first_name":  "Douglas",
               "last_name":   "Fir",
               "age":         35,
               "about":       "I like to build cabinets",
               "interests":["forestry"]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "1",
            "_score":         1,
            "_source":{
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests":["sports","music"]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "2",
            "_score":         1,
            "_source":{
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests":["music"]
            }
         }
      ]
   }
}


GET /megacorp/employee/_search?q=last_name:Smith
{
   ...
   "hits":{
      "total":      2,
      "max_score":  0.30685282,
      "hits":[
         {
            ...
            "_source":{
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests":["sports","music"]
            }
         },
         {
            ...
            "_source":{
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests":["music"]
            }
         }
      ]
   }
}

Elasticsearch提供了一个丰富的,灵活的查询语言,叫做DSL.Domain-specific language(DSL)使用特定的JSON请求。
GET /megacorp/employee/_search
{
    "query":{
        "match":{
            "last_name":"Smith"
        }
    }
}
这里没有使用查询参数,使用match匹配查询条件。输出结果和上个例子相同。



查找所有last name为Smith,年龄大于30的员工
[object Object][object Object]
{
   ...
   "hits":{
      "total":      1,
      "max_score":  0.30685282,
      "hits":[
         {
            ...
            "_source":{
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests":["music"]
            }
         }
      ]
   }
}


Full-text search 全文搜索

搜索所有喜欢rock climbing的员工
在Sense中输入
GET /megacorp/employee/_search
{
    "query":{
        "match":{
            "about":"rock climbing"
        }
    }
}


查看查询结果

[object Object][object Object]
默认情况下,Elasticsearch根据匹配结果的relevance score进行排序,表示匹配程度。可以看到第二个匹配结果只包含rock也被显示出来。



如果想要完全匹配查询条件,可以使用短语搜索phrase search

使用match_phrase进行条件匹配

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


{
   ...
   "hits":{
      "total":      1,
      "max_score":  0.23013961,
      "hits":[
         {
            ...
            "_score":         0.23013961,
            "_source":{
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests":["sports","music"]
            }
         }
      ]
   }
}
现在就只有一条搜索结果





Highlight our searches高亮显示查询结果

GET /megacorp/employee/_search
{
    "query":{
        "match_phrase":{
            "about":"rock climbing"
        }
    },
    "highlight":{
        "fields":{
            "about":{}
        }
    }
}
{
   ...
   "hits":{
      "total":      1,
      "max_score":  0.23013961,
      "hits":[
         {
            ...
            "_score":         0.23013961,
            "_source":{
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests":["sports","music"]
            },
            "highlight":{
               "about":[
                  "I love to go <em>rock</em> <em>climbing</em>"
               ]
            }
         }
      ]
   }
}




使用Elasticsearch的聚合函数可以对数据进行复杂的分析。类似SQL语言的GROUP BY语句。



GET /megacorp/employee/_search
{
  "aggs":{
    "all_interests":{
      "terms":{"field":"interests"}
    }
  }
}
{
   ...
   "hits":{...},
   "aggregations":{
      "all_interests":{
         "buckets":[
            {
               "key":       "music",
               "doc_count":2
            },
            {
               "key":       "forestry",
               "doc_count":1
            },
            {
               "key":       "sports",
               "doc_count":1
            }
         ]
      }
   }
}


GET /megacorp/employee/_search
{
  "query":{
    "match":{
      "last_name":"smith"
    }
  },
  "aggs":{
    "all_interests":{
      "terms":{
        "field":"interests"
      }
    }
  }
}
  ...
  "all_interests":{
     "buckets":[
        {
           "key":"music",
           "doc_count":2
        },
        {
           "key":"sports",
           "doc_count":1
        }
     ]
  }
Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。

猜你喜欢

转载自horsemen.iteye.com/blog/2244034