Cluster,集群,多个节点(服务器)的组合,存储了大量的数据,提供联合索引和搜索室友节点的能力
Node,节点,节点是集群的一部分,是一个单独的服务器,用来存储数据,并且参与到集群的索引和搜索中来
Index,索引,是很多相似的文档的集合。必须使用小写
Type,类型,是索引的逻辑分类
Document,文档,是被索引的信息的基础单位
Shard,索引的一部分,防止超过单个节点的硬件限制
Replication,副本,防止网络奔溃,节点掉线等
检查节点健康:
curl -XGET 'localhost:9200/_cat/health?v&pretty'
看我们集群中的节点列表:
GET /_cat/nodes?v
所有索引:
GET /_cat/indices?v
创建一个名字为customer的索引,并且列出来:
PUT /customer?pretty
GET /_cat/indices?v
在customer里索引一个external类型的,ID为1的记录:
PUT /customer/external/1?pretty
{
"name": "John Doe"
}
删除这个索引,再次列出列表
DELETE /customer?pretty
GET /_cat/indices?v
固定格式:
<REST Verb> /<Index>/<Type>/<ID>
插入一个没有ID的索引:
POST /customer/external?pretty
{
"name": "Jane Doe"
}
更新一个先前已经创建的:
POST /customer/external/1/_update?pretty
{
"doc": { "name": "Jane Doe" }
}
也可以做加法:
POST /customer/external/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
ctx._source指的就是更新当前文件
删除Costomer中ID为2的:
DELETE /customer/external/2?pretty
同时插入两个索引:
POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
更新ID1并且删除ID2
POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
Bulk操作不会因为一个操作的失败而全部失败
使用search API,匹配所有文档(q=*),并且以account_number递增排序:
GET /bank/_search?q=*&sort=account_number:asc&pretty
几个相关解释
完整版的代码
GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
从第十个开始,索引10个
GET /bank/_search
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
默认返回top 10,desc为降序
GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
仅仅返回a_n和b
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
仅仅返回a_n为20的:
GET /bank/_search
{
"query": { "match": { "account_number": 20 } }
}
所有address含有mill的:
GET /bank/_search
{
"query": { "match": { "address": "mill" } }
}
含有mill或者lane的:
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
同时含有mill lane的:
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
同上:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
含有mill或者lane:
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
同时不含有:
GET /bank/_search?pretty
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
必须ape为40并且state不是ID:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
过滤器,balance在20000到30000之间的所有的:
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
聚合,定义“group_by_state”,在state域中统计,降序(默认)返回前十(默认):
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
类似于SQL中的:
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
按state降序并且显示balance的平均值:
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
age分类并且性别降序,显示平均balance:
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
网友评论