1、高亮查询
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。
Elasticsearch
可以对查询内容中的关键字部分,进行标签和样式
(
高亮
)
的设置。
在使用
match
查询的同时,加上一个
highlight
属性:
- 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、分页查询
from
:当前页的起始索引,默认从
0
开始。
from = (pageNum - 1) * size
size
:每页显示多少条
#####模糊查询first_name为tom 按照age倒叙排序,前两条数据
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、聚合查询
聚合允许使用者对
es
文档进行统计分析,类似与关系型数据库中的
group by
,当然还有很
多其他的聚合,例如取最大值、平均值等等。
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
}
}
}
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
}
}
}
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
}
}
}
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 聚合
stats
聚合,对某个字段一次性返回
count
,
max
,
min
,
avg
和
sum
五个指标
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、桶聚合查询
桶聚和相当于
sql
中的
group by
语句
terms
聚合,分组统计
#####对age group by
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
}
]
}
}
}
|