ElasticSearch配置
概念
-
节点 (node), 集群 (cluster)
单台服务器,存储数据并参与集群的索引和搜索,服务器在启动时自动分配ID ,节点默认会加入ID为ElasticSearch的集群中,除非自己配置,如果只有一个节点,那么该节点也作为一个集群工作。
多个节点组成的一个集群,集群可跨节点,进行联合索引和搜索。集群默认ID为ElasticSearch -
索引(index) ,类型(type) ,文档(document)
例:
index可为blogSystem;
type可为blogs,comments,user;
document存储基本信息元; -
分片 (shard) 和 副本(replica)
可为一个索引指定n个分片,分片可以存储在集群中任一个节点上。
一个索引默认分配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, //记录条数
"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" }
}
}
}
网友评论