您的位置:首页 > 其它

Elasticsearch 安装使用

2015-02-16 21:56 369 查看
Elasticsearch是一个全文搜索引擎。
安装Elasticsearch时需要先安装Java
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.zipunzip elasticsearch-1.4.2.zipcd elasticsearch-1.4.2./bin/elasticsearch

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

查看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

可以通过以下方式关闭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 Elasticsearch集群中的任意一个node的主机名,如果是在node本机就直接使用localhost

PORT Elasticsearch运行HTTP服务的端口,默认是9200

QUERY_STRING 查询参数
BODY JSON格式的请求数据
$ 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的Sense接口访问Elasticsearch
http://xxxx.com:9200/_plugin/marvel/sense/index.html
以下直接使用GET或PUT的简略形式,都是直接使用Marvel的Sense接口访问Elasticsearch,可以点击“Copy as cURL”查看对应的curl命令写法





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


在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以上的数据。

参考文档
http://www.elasticsearch.com/guide/en/elasticsearch/guide/current/index.html
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html

本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1614701
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: