赞
踩
在索引 task8 中,写出满足以下条件的查询
title
中包含 my"或 metags
中包含 romatic movies,该条算分提高,如果不包含则算分不变。PUT task8
{
"mappings": {
"properties": {
"title":{
"type":"text"
},
"tags":{
"type": "keyword"
}
}
}
}
POST task8/_bulk
{"index": {}}
{"title":"my", "tags":["romatic movies"]}
{"index": {}}
{"title":"me", "tags":["movies"]}
GET task8/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"title": "my"
}
},
{
"match": {
"title": "me"
}
}
]
}
},
"functions": [
{
"filter": {
"term": {
"tags": {
"value": "romatic movies"
}
}
},
"weight": 2
}
],
"boost": "5",
"boost_mode": "multiply"
}
}
}
对索引 task9 编写一个查询模板,并满足以下要求:
a_01
参数查询 a
字段start_date
和 end_date
参数范围查询 timestamp
字段end_date
参数,那么结束时间默认是现在b
字段必须等于 active使用 查询模板查询 2018年6月1日到现在的数据,a
字段包含关键字 aaa
# 创建索引
PUT task9
{
"mappings": {
"properties": {
"a":{
"type": "text"
},
"b":{
"type": "keyword"
},
"timestamp":{
"type": "date"
}
}
}
}
# 写入数据
POST /task9/_bulk
{"index": {}}
{"a":"aaa AAA", "b":"active", "timestamp":"2021-11-11T11:21:21.000Z"}
{"index": {}}
{"a":"aaa AAA", "b":"active", "timestamp":"2022-11-11T11:21:21.000Z"}
{"index": {}}
{"a":"AAA", "b":"b", "timestamp":"2023-11-11T11:21:21.000Z"}
# 创建模板
POST _scripts/my_search_template
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{
"match": {
"a": "{{a_01}}"
}
},
{
"range": {
"timestamp": {
"gte": "{{start_date}}",
"lte": "{{end_date}}{{^end_date}}now{{/end_date}}"
}
}
},
{
"term": {
"b": {
"value": "active"
}
}
}
]
}
}
}
}
}
# 预览检索模板
GET _render/template/my_search_template
{
"params": {
"a_01": "aaa",
"start_date": "2018-06-01T00:00:00.000Z"
}
}
# 查询
GET task9/_search/template
{
"id": "my_search_template",
"params": {
"a_01": "aaa",
"start_date": "2018-06-01T00:00:00.000Z"
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。