//查看集群节点健康状态
GET /_cat/health?v
GET /_cluster/health
//查看集群设置信息 persistent永久设置信息 transient临时设置信息
GET /_cluster/settings
//查看集群状态
GET /_cluster/state
//查看集群统计
GET /_cluster/stats
// 查询ES节点情况
GET /_cat/nodes?v
// 查询ES节点的统计信息
GET /_nodes/stats?human=true
// 查询ES节点的使用情况
GET /_nodes/usage?human=true
// 查询ES节点的热线程(用于排查ES查询缓慢等问题)
GET /_nodes/hot_threads
// 查询ES安装的插件情况(用于Elasticsearch节点安装后,确认IK/HanLp/pinyin等插件是否正常安装)
GET /_cat/plugins?v
// 查询所有索引情况
GET /_cat/indices?v
// 查询所有别名情况
GET /_cat/aliases?v
// 查询索引的settings信息
GET /${索引名}/_settings
// 查询索引的映射mapping信息
GET /${索引名}/_mapping
// 查询索引的别名信息
GET /${索引名}/_alias
//修改别名 es没有修改别名的操作,只能先删除后添加
POST _aliases
{
"actions" : [{"remove" : {"index" : "student" , "alias" : "in1"}}],
"actions" : [{"add" : {"index" : "student" , "alias" : "in2"}}]
}
//添加索引映射 ES创建后的mapping不可修改# 只能添加新字段
PUT ${索引名}/_mapping
{
"properties": {
"${字段名}": {
"type": "#{字段类型}"
}
}
}
#修改副本数
PUT ${索引名}/_settings
{
"settings" : {
"index" : {
"number_of_replicas" : #{副本数量}
}
}
}
基本的match匹配
POST bookdb_index/_search
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}
根据多个字段检索
POST bookdb_index/_search
{
"query": {
"multi_match" : {
"query" : "elasticsearch guide",
"fields": ["title", "summary"]
}
}
}
提高某一个字段的分值
POST bookdb_index/_search
{
"query": {
"multi_match" : {
"query" : "elasticsearch guide",
"fields": ["title", "summary^3"]
}
},
"_source": ["title", "summary", "publish_date"]
}
POST bookdb_index/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"title": "Elasticsearch"
}
},
{
"match": {
"title": "Solr"
}
}
]
}
},
{
"match": {
"authors": "clinton gormely"
}
}
],
"must_not": {
"match": {
"authors": "radu gheorge"
}
}
}
}
}
POST bookdb_index/_search
{
"query": {
"multi_match" : {
"query" : "comprihensiv guide",
"fields": ["title", "summary"],
"fuzziness": "AUTO"
}
},
"_source": ["title", "summary", "publish_date"],
"size": 1
}
POST bookdb_index/_search
{
"query": {
"wildcard" : {
"authors" : "t*"
}
},
"_source": ["title", "authors"],
"highlight": {
"fields" : {
"authors" : {}
}
}
}
POST bookdb_index/_search
{
"query": {
"regexp" : {
"authors" : "t[a-z]*y"
}
},
"_source": ["title", "authors"],
"highlight": {
"fields" : {
"authors" : {}
}
}
}
POST bookdb_index/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"]
}
POST bookdb_index/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"],
"sort": [
{ "publish_date": {"order":"desc"}},
{ "title": { "order": "desc" }}
]
}
POST /bookdb_index/_search
{
"query": {
"range" : {
"publish_date": {
"gte": "2015-01-01",
"lte": "2015-12-31"
}
}
},
"_source" : ["title","publish_date","publisher"]
}
POST /bookdb_index/_search
{
"query": {
"filtered": {
"query" : {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","summary"]
}
},
"filter": {
"range" : {
"num_reviews": {
"gte": 20
}
}
}
}
},
"_source" : ["title","summary","publisher", "num_reviews"]
}
可用于动态调整结果的分值
POST /bookdb_index/book/_search
{
"query": {
"function_score": {
"query": {
"multi_match" : {
"query" : "search engine",
"fields": ["title", "summary"]
}
},
"functions": [
{
"script_score": {
"params" : {
"threshold": "2015-07-30"
},
"script": "publish_date = doc['publish_date'].value; num_reviews = doc['num_reviews'].value; if (publish_date > Date.parse('yyyy-MM-dd', threshold).getTime()) { return log(2.5 + num_reviews) }; return log(1 + num_reviews);"
}
}
]
}
},
"_source": ["title", "summary", "publish_date", "num_reviews"]
}
POST ${索引名}/_update_by_query
{
"query": {
"match": {
"status": "UP_SHELF"
}
},
"script": {
"inline": "ctx._source['status'] = 'DOWN_SHELF'"
}
}
POST ${索引名}/_delete_by_query
{
"query": {
"match": {
"user.id": "elkbee"
}
}
}
ES官方文档
词条聚合查询
POST ${索引名}/_search
{
"aggs": {
"genres": {
"terms": { "field": "genre" }
}
}
}
聚合时以 ES中某个字段作为 排序依据
#实际用到的查询
# 消息查询中, 根据groupId分组聚合,以sendTime正序|_key倒序
# 子聚合 以 消息类型type进行分组聚合
POST message_alias/_search
{
"size":"0",
"aggregations": {
"groupId": {
"terms": {
"field": "groupId",
"size": 500,
"order": [
{
"sendTime": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"type": {
"terms": {
"field": "type",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"sendTime": {
"max": {
"field": "sendTime"
}
}
}
}
}
}