一、题目
在集群上有一个索引 food_ingredient
,搜索需要满足以下要求:
- 三个字段
manufacturer
,name
,brand
都能匹配到文本 cake mix - 高亮 字段
name
,并加标签 - 排序,对字段
user
正序,_score
降序,返回 20 个文档
# 创建符合条件的 task 索引,设置 field 字段,并写入数据
PUT food_ingredient
{
"mappings": {
"properties": {
"manufacturer":{
"type": "text"
},
"name":{
"type": "text"
},
"brand":{
"type": "text"
},
"user":{
"type": "keyword"
}
}
}
}
# 写入数据
POST food_ingredient/_bulk
{"index":{}}
{"manufacturer": "cake mix", "name": "cake mix", "brand": "cake mix", "user": "cai"}
1.1 考点
1.2 答案
GET food_ingredient/_search
{
"query": {
"multi_match": {
"query": "cake mix",
"fields": [
"manufacturer",
"name",
"brand"
]
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": [
"<em>"
],
"post_tags": [
"</em>"
]
}
}
},
"sort": [
{
"user": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
]
}
二、题目
集群中有 earthquakes
索引,timestamp
字段的格式为 yyyy-MM-dd HH:mm:ss
。对 earthquakes
索引按月分桶,并且对 magnitude
和 depth
进行最大值聚合。
# 创建索引
PUT earthquakes
{
"settings": {
"number_of_replicas": 0
},
"mappings": {
"properties": {
"timestamp":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"magnitude":{
"type": "float"
},
"type":{
"type":"integer"
},
"depth":{
"type":"float"
}
}
}
}
# 导入数据
POST earthquakes/_bulk
{"index":{}}
{"timestamp":"2012-01-01 12:12:12", "magnitude":4.56, "type":1, "depth":10}
{"index":{}}
{"timestamp":"2012-01-01 15:12:12", "magnitude":6.46, "type":2, "depth":11}
{"index":{}}
{"timestamp":"2012-02-02 13:12:12", "magnitude":4, "type":2, "depth":5}
{"index":{}}
{"timestamp":"2012-03-02 13:12:12", "magnitude":6, "type":3, "depth":8}
{"index":{}}
{"timestamp":"1967-03-02 13:12:12", "magnitude":6, "type":2, "depth":6}
2.1 考点
2.2 答案
发现有 min_doc_count
参数可以用,结果美观了很多
GET earthquakes/_search
{
"size": 0,
"aggs": {
"timestamp_aggs": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "1M",
"format": "yyyy-MM-dd",
"min_doc_count": 1
},
"aggs": {
"magnitude_max": {
"max": {
"field": "magnitude"
}
},
"depth_max": {
"max": {
"field": "depth"
}
}
}
}
}
}