elasticsearch基本查询语法笔记
基础使用
1. 启动状态
# 查看es是否正常启动
curl 'http://localhost:9200/?pretty'
2. 数据总量
使用_cout获取总数量 GET /_count
# 查看总数量
GET /_count?pretty
{
"query": {
"match_all": {}
}
}
3. 根据ID查询
根据ID获取,以下方式都可以取得相同的值
GET /megacorp/employee/_search?q=_id:1
GET megacorp/employee/1
4. 查询所有的数据
GET megacorp/employee/_search
{
"query": {
"match_all": {}
}
}
GET megacorp/employee/_search
5. 分词搜索及短语搜索
分将后面的条件进行分词,如果是"rock climbing" 有引号,就不会进行分词
如为match_phrase 即为短词搜索
GET /megacorp/employee/_search?q=about:rock climbing
GET megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
}
}
6. 搜索结果高亮显示
GET megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}
7. 聚合-分组数量
按兴趣爱好的关键词进行聚合展示出其数量,类似于group by
GET /megacorp/employee/_search
{
"aggs": {
"test123": {
"terms": {
"field": "interests.keyword",
"size": 10
}
}
}
}
8. 聚合-平均值
GET /megacorp/employee/_search
{
"aggs": {
"age_avg": {
"avg": {
"field": "age"
}
}
}
}
9. 聚合-多层分组并求组内平均
需要多层aggs进行聚合,下层在上层的基础上进行处理
GET /megacorp/employee/_search
{
"aggs": {
"inters": {
"terms": {
"field": "interests.keyword",
"size": 10
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}