Elasticsearch常用查询

1.查看集群|节点|索引基本情况

集群|节点信息

//查看集群节点健康状态
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" : #{副本数量} 
        }
    }
}


二.常用查询

1) 常用的匹配(Query)查询

基本的match匹配

POST bookdb_index/_search
{
  "query": {
    "match": {
      "title": "elasticsearch"
    }
  }
}
  1. 多字段(Multi-filed)查询

根据多个字段检索

POST bookdb_index/_search
{
    "query": {
        "multi_match" : {
            "query" : "elasticsearch guide",
            "fields": ["title", "summary"]
        }
    }
}
  1. Boosting

提高某一个字段的分值

POST bookdb_index/_search
{
    "query": {
        "multi_match" : {
            "query" : "elasticsearch guide",
            "fields": ["title", "summary^3"]
        }
    },
    "_source": ["title", "summary", "publish_date"]
}
  1. Bool查询
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"
				}
			}
		}
	}
}
  1. 模糊(Fuzzy)查询
POST bookdb_index/_search
{
    "query": {
        "multi_match" : {
            "query" : "comprihensiv guide",
            "fields": ["title", "summary"],
            "fuzziness": "AUTO"
        }
    },
    "_source": ["title", "summary", "publish_date"],
    "size": 1
}
  1. 通配符(Wildcard)查询
POST bookdb_index/_search
{
    "query": {
        "wildcard" : {
            "authors" : "t*"
        }
    },
    "_source": ["title", "authors"],
    "highlight": {
        "fields" : {
            "authors" : {}
        }
    }
}
  1. 正则(Regexp)查询
POST bookdb_index/_search
{
    "query": {
        "regexp" : {
            "authors" : "t[a-z]*y"
        }
    },
    "_source": ["title", "authors"],
    "highlight": {
        "fields" : {
            "authors" : {}
        }
    }
}
  1. 词条(Term)/多词条(Terms)查询

POST bookdb_index/_search
{
    "query": {
        "term" : {
            "publisher": "manning"
        }
    },
    "_source" : ["title","publish_date","publisher"]
}
  1. 词条(Term)查询 - 排序(Sorted)
POST bookdb_index/_search
{
    "query": {
        "term" : {
            "publisher": "manning"
        }
    },
    "_source" : ["title","publish_date","publisher"],
    "sort": [
        { "publish_date": {"order":"desc"}},
        { "title": { "order": "desc" }}
    ]
}
  1. 范围查询
POST /bookdb_index/_search
{
    "query": {
        "range" : {
            "publish_date": {
                "gte": "2015-01-01",
                "lte": "2015-12-31"
            }
        }
    },
    "_source" : ["title","publish_date","publisher"]
}
  1. 过滤(Filtered)查询
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"]
}
  1. 函数分值: 脚本评分

​ 可用于动态调整结果的分值


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

2) 通过条件更新索引数据

POST ${索引名}/_update_by_query 
{
    "query": {
        "match": {
            "status": "UP_SHELF"
        }
    },
    "script": {
        "inline": "ctx._source['status'] = 'DOWN_SHELF'"
    }
}

3) 通过条件删除索引数据

POST ${索引名}/_delete_by_query
{
  "query": {
    "match": {
      "user.id": "elkbee"
    }
  }
}

4) 聚合查询

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

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值