Elasticsearch查询
批量插入
_bulk
# 批量插入数据
POST student_info_index/_bulk
{"index":{}}
{"student_name":"张三","address":"中国 北京","age":35}
{"index":{}}
{"student_name":"李四","address":"中国 上海","age":32}
{"index":{}}
{"student_name":"王五","address":"美国 华盛顿","age":25}
{"index":{}}
{"student_name":"赵六","address":"英国 伦敦","age":28}
{"index":{}}
{"student_name":"田七","address":"中国 台湾","age":40}
POST student_info_index/_search
结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : 1.0,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "王五",
"address" : "美国 华盛顿",
"age" : 25
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "ewDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "赵六",
"address" : "英国 伦敦",
"age" : 28
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
}
}
]
}
}
查询所有数据
{
“query”: {
“match_all”: {}
}
}
语句:
GET student_info_index/_search
{
"query": {
"match_all": {}
}
}
结果:
{
"took" : 2, # 查询耗时,单位毫秒
"timed_out" : false, # 是否超时
"_shards" : { # 查询使用的分片信息
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5, # 查询到匹配文档数量
"relation" : "eq"
},
"max_score" : 1.0, # 查询文档的最高分,分数越高,匹配度越高
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : 1.0, # 当前文档和查询内容的匹配度
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "王五",
"address" : "美国 华盛顿",
"age" : 25
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "ewDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "赵六",
"address" : "英国 伦敦",
"age" : 28
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
}
}
]
}
}
排序查询
“sort”: [
{
“age”: {
“order”: “desc”
}
}
]排序字段不可以是text等特殊类型,一般是整数或keyword类型
语句:
# 按照年级倒序
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
排序前的结果,查看以上结果
排序后的结果:
{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
},
"sort" : [
40
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : null,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
},
"sort" : [
35
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
},
"sort" : [
32
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "ewDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "赵六",
"address" : "英国 伦敦",
"age" : 28
},
"sort" : [
28
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "王五",
"address" : "美国 华盛顿",
"age" : 25
},
"sort" : [
25
]
}
]
}
}
多字段排序
# 多字段排序
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
},
"student_name.keyword": {
"order": "desc"
}
}
]
}
结果:
{
"took" : 168,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
},
"sort" : [
40,
"田七"
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : null,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
},
"sort" : [
35,
"张三"
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
},
"sort" : [
32,
"李四"
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "ewDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "赵六",
"address" : "英国 伦敦",
"age" : 28
},
"sort" : [
28,
"赵六"
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "王五",
"address" : "美国 华盛顿",
"age" : 25
},
"sort" : [
25,
"王五"
]
}
]
}
}
查询指定字段信息
“_source”: [“student_name”,“age”]
语句:
# 查询指定字段
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"_source": ["student_name","age"],
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
结果:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "田七",
"age" : 40
},
"sort" : [
40
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : null,
"_source" : {
"student_name" : "张三",
"age" : 35
},
"sort" : [
35
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "李四",
"age" : 32
},
"sort" : [
32
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "ewDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "赵六",
"age" : 28
},
"sort" : [
28
]
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : null,
"_source" : {
"student_name" : "王五",
"age" : 25
},
"sort" : [
25
]
}
]
}
}
分页查询
“from”:0
“size”:3
语句:
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"_source": [
"student_name",
"address"
],
"from": 0,
"size": 3
}
结果:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : 1.0,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京"
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海"
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "egDUw4YBnnKcIM3DJ_jS",
"_score" : 1.0,
"_source" : {
"student_name" : "王五",
"address" : "美国 华盛顿"
}
}
]
}
}
查询指定字段的特定内容
语句:
GET student_info_index/_search
{
"query": {
"match": {
"address": "中"
}
}
}
结果:
{
"took" : 124,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.549705,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : 0.549705,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 0.549705,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : 0.549705,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
}
}
]
}
}
段落查询
语句:
GET student_info_index/_search
{
"query": {
"match_phrase": {
"address": "中国 上海"
}
}
}
结果:
{
"took" : 344,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 3.4661183,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 3.4661183,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
}
}
]
}
}
段落前缀查询
语句:
GET student_info_index/_search
{
"query": {
"match_phrase_prefix": {
"address": "中国"
}
}
}
结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.63844514,
"hits" : [
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eADUw4YBnnKcIM3DJ_jF",
"_score" : 0.63844514,
"_source" : {
"student_name" : "张三",
"address" : "中国 北京",
"age" : 35
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "eQDUw4YBnnKcIM3DJ_jS",
"_score" : 0.63844514,
"_source" : {
"student_name" : "李四",
"address" : "中国 上海",
"age" : 32
}
},
{
"_index" : "student_info_index",
"_type" : "_doc",
"_id" : "fADUw4YBnnKcIM3DJ_jS",
"_score" : 0.63844514,
"_source" : {
"student_name" : "田七",
"address" : "中国 台湾",
"age" : 40
}
}
]
}
}
精确查询
数据准备
# 三国人物信息索引映射
PUT person_info_index
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"nationality": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "standard"
},
"age":{
"type": "integer"
}
}
}
}
# 批量插入
POST person_info_index/_bulk
{"index":{}}
{"name":"曹操","nationality":"魏国","age":45}
{"index":{}}
{"name":"夏侯淳","nationality":"魏国","age":36}
{"index":{}}
{"name":"曹昂","nationality":"魏国","age":32}
{"index":{}}
{"name":"刘备","nationality":"蜀国","age":42}
{"index":{}}
{"name":"关羽","nationality":"蜀国","age":41}
{"index":{}}
{"name":"张飞","nationality":"蜀国","age":39}
{"index":{}}
{"name":"孙权","nationality":"吴国","age":26}
{"index":{}}
{"name":"周瑜","nationality":"吴国","age":30}
match查询
GET person_info_index/_search
{
"query": {
"match": {
"nationality": "吴国"
}
}
}
结果:返回结果没有匹配到文档数据。
{
"took" : 218,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
分析:nationality进过ik_max_word分词后,依旧是原来的词汇,而查询时使用standard分词,会把单个汉字作为term,所以查不到。
term查询
精准查询不对查询内容分词
语句:
GET person_info_index/_search
{
"query": {
"term": {
"nationality": "吴国"
}
}
}
结果:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.2809337,
"hits" : [
{
"_index" : "person_info_index",
"_type" : "_doc",
"_id" : "gwAHxIYBnnKcIM3D3_jm",
"_score" : 1.2809337,
"_source" : {
"name" : "孙权",
"nationality" : "吴国",
"age" : 26
}
},
{
"_index" : "person_info_index",
"_type" : "_doc",
"_id" : "hAAHxIYBnnKcIM3D3_jm",
"_score" : 1.2809337,
"_source" : {
"name" : "周瑜",
"nationality" : "吴国",
"age" : 30
}
}
]
}
}
bool多条件查询
语句:
GET person_info_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"age": 30
}
}
],
"must_not": [
{
"term": {
"nationality": "吴国"
}
}
]
}
}
}
结果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
bool和filter组合查询
语句
GET person_info_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"nationality": "蜀国"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 40,
"lte": 50
}
}
}
]
}
}
}
结果:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9444616,
"hits" : [
{
"_index" : "person_info_index",
"_type" : "_doc",
"_id" : "gAAHxIYBnnKcIM3D3_jm",
"_score" : 0.9444616,
"_source" : {
"name" : "刘备",
"nationality" : "蜀国",
"age" : 42
}
},
{
"_index" : "person_info_index",
"_type" : "_doc",
"_id" : "gQAHxIYBnnKcIM3D3_jm",
"_score" : 0.9444616,
"_source" : {
"name" : "关羽",
"nationality" : "蜀国",
"age" : 41
}
}
]
}
}
简单聚合
数据准备:
# 学员成绩
POST stu_score_index/_bulk
{"index":{}}
{"stu_name":"张三","class_name":"高三一班","age":10,"score":58}
{"index":{}}
{"stu_name":"李四","class_name":"高三一班","age":10,"score":68}
{"index":{}}
{"stu_name":"王五","class_name":"高三二班","age":10,"score":78}
{"index":{}}
{"stu_name":"赵六","class_name":"高三二班","age":10,"score":88}
{"index":{}}
{"stu_name":"田七","class_name":"高三三班","age":10,"score":98}
{"index":{}}
{"stu_name":"钱八","class_name":"高三三班","age":10,"score":48}
分组统计各组总条数-count
语句:
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword"
}
}
}
}
结果:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_class_name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高三一班",
"doc_count" : 2
},
{
"key" : "高三三班",
"doc_count" : 2
},
{
"key" : "高三二班",
"doc_count" : 2
}
]
}
}
}
分组统计每组平均值-avg
语句:
# avg
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword"
},
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
}
}
结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_class_name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高三一班",
"doc_count" : 2,
"avg_score" : {
"value" : 63.0
}
},
{
"key" : "高三三班",
"doc_count" : 2,
"avg_score" : {
"value" : 73.0
}
},
{
"key" : "高三二班",
"doc_count" : 2,
"avg_score" : {
"value" : 83.0
}
}
]
}
}
}
分组统计每组最大值-max
# max
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
},
"aggs": {
"max_score": {
"max": {
"field": "score"
}
}
}
}
}
}
分组统计每组最小值-min
# min
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
},
"aggs": {
"min_score": {
"min": {
"field": "score"
}
}
}
}
}
}
结果
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_class_name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高三一班",
"doc_count" : 2,
"min_score" : {
"value" : 58.0
}
},
{
"key" : "高三三班",
"doc_count" : 2,
"min_score" : {
"value" : 88.0
}
},
{
"key" : "高三二班",
"doc_count" : 2,
"min_score" : {
"value" : 78.0
}
}
]
}
}
}
脚本总结:
# 批量插入数据
POST student_info_index/_bulk
{"index":{}}
{"student_name":"张三","address":"中国 北京","age":35}
{"index":{}}
{"student_name":"李四","address":"中国 上海","age":32}
{"index":{}}
{"student_name":"王五","address":"美国 华盛顿","age":25}
{"index":{}}
{"student_name":"赵六","address":"英国 伦敦","age":28}
{"index":{}}
{"student_name":"田七","address":"中国 台湾","age":40}
POST student_info_index/_search
GET student_info_index/_search
{
"query": {
"match_all": {}
}
}
GET student_info_index/_mapping
# 按照年级倒序
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
# 多字段排序
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
},
"student_name.keyword": {
"order": "desc"
}
}
]
}
# 查询指定字段
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"_source": ["student_name","age"],
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET student_info_index/_search
{
"query": {
"match_all": {}
},
"_source": [
"student_name",
"address"
],
"from": 0,
"size": 3
}
GET student_info_index/_search
{
"query": {
"match": {
"address": "北京"
}
}
}
GET student_info_index/_search
{
"query": {
"match_phrase": {
"address": "中国 上海"
}
}
}
GET student_info_index/_search
{
"query": {
"match_phrase": {
"address.": "中国"
}
}
}
# 三国人物信息索引映射
PUT person_info_index
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"nationality": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "standard"
},
"age":{
"type": "integer"
}
}
}
}
# 批量插入
POST person_info_index/_bulk
{"index":{}}
{"name":"曹操","nationality":"魏国","age":45}
{"index":{}}
{"name":"夏侯淳","nationality":"魏国","age":36}
{"index":{}}
{"name":"曹昂","nationality":"魏国","age":32}
{"index":{}}
{"name":"刘备","nationality":"蜀国","age":42}
{"index":{}}
{"name":"关羽","nationality":"蜀国","age":41}
{"index":{}}
{"name":"张飞","nationality":"蜀国","age":39}
{"index":{}}
{"name":"孙权","nationality":"吴国","age":26}
{"index":{}}
{"name":"周瑜","nationality":"吴国","age":30}
GET person_info_index/_search
{
"query": {
"term": {
"nationality": "吴国"
}
}
}
GET person_info_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"age": 30
}
}
],
"must_not": [
{
"term": {
"nationality": "吴国"
}
}
]
}
}
}
GET person_info_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"nationality": "蜀国"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 40,
"lte": 50
}
}
}
]
}
}
}
# 学员成绩
DELETE stu_score_index
POST stu_score_index/_bulk
{"index":{}}
{"stu_name":"张三","class_name":"高三一班","age":16,"score":58}
{"index":{}}
{"stu_name":"李四","class_name":"高三一班","age":16,"score":68}
{"index":{}}
{"stu_name":"王五","class_name":"高三二班","age":17,"score":78}
{"index":{}}
{"stu_name":"赵六","class_name":"高三二班","age":18,"score":88}
{"index":{}}
{"stu_name":"田七","class_name":"高三三班","age":15,"score":98}
{"index":{}}
{"stu_name":"钱八","class_name":"高三三班","age":14,"score":88}
# count
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
}
}
}
}
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"total_count": {
"value_count": {
"field": "stu_name.keyword"
}
}
}
}
# avg
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
},
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
}
}
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
# max
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
},
"aggs": {
"max_score": {
"max": {
"field": "score"
}
}
}
}
}
}
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"max_score": {
"max": {
"field": "score"
}
}
}
}
# min
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"group_by_class_name": {
"terms": {
"field": "class_name.keyword",
"size": 10
},
"aggs": {
"min_score": {
"min": {
"field": "score"
}
}
}
}
}
}
GET stu_score_index/_search
{
"size": 0,
"aggs": {
"min_score": {
"min": {
"field": "score"
}
}
}
}