1、高亮查询
- pre_tags:前置标签
- post_tags:后置标签
- fields:需要高亮的字段
- title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空
GET /my_index_data/_search
{
"query": {
"match": {
"first_name": "tom6"
}
},
"highlight": {
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fields": {
"first_name": {}
}
} }
####结果
{
"took" : 266,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.7155422,
"hits" : [
{
"_index" : "my_index_data",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.7155422,
"_source" : {
"first_name" : "tom6",
"last_name" : "Smith tom1",
"age" : 39
},
"highlight" : {
"first_name" : [
"<font color='red'>tom6</font>"
]
}
}
]
}
}
2、分页查询
GET /my_index_data/_search
{
"query": {
"fuzzy": {
"first_name": {
"value": "tom"
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 2
}
########结果
{
"took" : 44,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "my_index_data",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"first_name" : "tom3",
"last_name" : "Smith",
"age" : 39
},
"sort" : [
39
]
},
{
"_index" : "my_index_data",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : {
"first_name" : "tom6",
"last_name" : "Smith tom1",
"age" : 39
},
"sort" : [
39
]
}
]
}
}
3、聚合查询
- 对age取最大值 max
GET /my_index_data/_search
{
"aggs": {
"max_age【别名】": {
"max": {
"field": "age"
}
}
},
"size": 0
}
#结果
{
"took" : 44,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"max_age" : {
"value" : 39.0
}
}
}
- 对age取最小值min
GET /my_index_data/_search
{
"aggs": {
"min_age": {
"min": {
"field": "age"
}
}
},
"size": 0
}
###结果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"min_age" : {
"value" : 20.0
}
}
}
- 求和
GET /my_index_data/_search
{
"aggs": {
"ageSum": {
"sum": {
"field": "age"
}
}
},
"size": 0
}
#####求和结果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"ageSum" : {
"value" : 255.0
}
}
}
- 平均值
GET /my_index_data/_search
{
"aggs": {
"ageAvg": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
#平均值
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"ageAvg" : {
"value" : 31.875
}
}
}
- 数据总数count
GET /my_index_data/_search
{
"aggs": {
"count": {
"value_count": {
"field": "age"
}
}
},
"size": 0
}
####结果
{
"took" : 35,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"count" : {
"value" : 8
}
}
}
- 数据总数count (去重之后)
GET /my_index_data/_search
{
"aggs": {
"distinct_age": {
"cardinality": {
"field": "age"
}
}
},
"size": 0
}
####结果
{
"took" : 35,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"distinct_age" : {
"value" : 6
}
}
}
4、State 聚合
GET /my_index_data/_search
{
"aggs":{
"stats_age":{
"stats":{"field":"age"}
}
},
"size":0
}
####结果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"stats_age" : {
"count" : 8,
"min" : 20.0,
"max" : 39.0,
"avg" : 31.875,
"sum" : 255.0
}
}
}
5、桶聚合查询
GET /my_index_data/_search
{
"aggs":{
"age_groupby":{
"terms":{"field":"age"}
}
},
"size":0
}
########结果
{
"took" : 26,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_groupby" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 30,
"doc_count" : 2
},
{
"key" : 39,
"doc_count" : 2
},
{
"key" : 20,
"doc_count" : 1
},
{
"key" : 29,
"doc_count" : 1
},
{
"key" : 33,
"doc_count" : 1
},
{
"key" : 35,
"doc_count" : 1
}
]
}
}
}
文章来源地址https://www.toymoban.com/news/detail-400623.html
文章来源:https://www.toymoban.com/news/detail-400623.html
到了这里,关于七、ElasticSearch-高级查询操作三的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!