ElasticSearch配置
概念
節(jié)點 (node)毯焕, 集群 (cluster)
單臺服務(wù)器觉吭,存儲數(shù)據(jù)并參與集群的索引和搜索,服務(wù)器在啟動時自動分配ID ,節(jié)點默認會加入ID為ElasticSearch的集群中钠怯,除非自己配置,如果只有一個節(jié)點曙聂,那么該節(jié)點也作為一個集群工作晦炊。
多個節(jié)點組成的一個集群,集群可跨節(jié)點宁脊,進行聯(lián)合索引和搜索刽锤。集群默認ID為ElasticSearch索引(index) ,類型(type) 朦佩,文檔(document)
例:
index可為blogSystem并思;
type可為blogs,comments,user;
document存儲基本信息元语稠;分片 (shard) 和 副本(replica)
可為一個索引指定n個分片宋彼,分片可以存儲在集群中任一個節(jié)點上。
一個索引默認分配5個分片和一個副本
安裝運行
//普通用戶身份操作(解壓)curl -L -O
https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.0.0/elasticsearch-2.0.0.tar.gz
...
...
tar -xvf elasticsearch-2.0.0.tar.gzcd elasticsearch-2.0.0/bin./elasticsearch
...
...
占用9200端口仙畦,對文檔操作提供REST API
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
//exp
curl -XGET 'localhost:9200/index/type/id?pretty'
curl -XPUT 'localhost:9200/index/type/1' -d '{ "name": "John Doe"}'
curl -XDELETE 'localhost:9200/customer'
CRUD語法- 查詢
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '{ "query": { "match-all": { } }, //所有文檔
"query": { "match": { "address": "mill" } }, //特定文檔
"query": { "match": { "address": "aaa bbb" } }, //含有aaa或bbb "query": { "match_phrase": { "address": "aaa bbb" } }, //含有"aaa bbb"
"size":10, //記錄條數(shù)
"from":10, //記錄跳頁
"sort": { "balance": { "order": "desc" } },//排序
//與
"query": {
"bool": {
"must": [ { "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
},
//或
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
},
//非
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
},
//多條件
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } } ],
"must_not": [
{ "match": { "state": "ID" } }
]
}
},
//范圍選擇
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {"gte": 20000,"lte": 30000}
}
}
}
},
//排序
"aggs": {
"group_by_state": {
"terms": { "field": "state" }
}
}
}