Elasticsearch 认证模拟题 - 10

一、题目

在索引 task8 中,写出满足以下条件的查询

  1. title 中包含 my "或 me
  2. 如果 tags 中包含 romatic movies,该条算分提高,如果不包含则算分不变。
rust 复制代码
PUT task8
{
  "mappings": {
    "properties": {
      "title":{
        "type":"text"
      },
      "tags":{
        "type": "keyword"
      }
    }
  }
}

POST task8/_bulk
{"index": {}}
{"title":"my", "tags":["romatic movies"]}
{"index": {}}
{"title":"me", "tags":["movies"]}
1.1 考点
  1. Boolean
  2. Function score
1.2 答案
rust 复制代码
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 编写一个查询模板,并满足以下要求:

  1. 使用 a_01 参数查询 a 字段
  2. 使用 start_dateend_date 参数范围查询 timestamp 字段
  3. 如果没有提供 end_date 参数,那么结束时间默认是现在
  4. 查询结果中 b 字段必须等于 active

使用 查询模板查询 2018年6月1日到现在的数据,a 字段包含关键字 aaa

rust 复制代码
# 创建索引
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"}
2.1 考点
  1. 查询模板
  2. 查询模板的默认参数
  3. Boolean
2.2 答案
rust 复制代码
# 创建模板
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"
  }
}
相关推荐
打码人的日常分享13 小时前
运维服务方案,运维巡检方案,运维安全保障方案文件
大数据·运维·安全·word·安全架构
苦逼IT运维15 小时前
Jenkins 监控方案:Prometheus + Grafana 实践
jenkins·grafana·prometheus
半夏陌离15 小时前
SQL 拓展指南:不同数据库差异对比(MySQL/Oracle/SQL Server 基础区别)
大数据·数据库·sql·mysql·oracle·数据库架构
一念一花一世界16 小时前
DevOps实战(3) - 使用Arbess+GitLab+Hadess实现Java项目自动化部署
gitlab·jenkins·tiklab·arbess·开源cicd工具
A小弈同学17 小时前
新规则,新游戏:AI时代下的战略重构与商业实践
大数据·人工智能·重构·降本增效·电子合同
字节跳动数据平台18 小时前
一客一策:Data Agent 如何重构大模型时代的智能营销?
大数据·agent
用户Taobaoapi201419 小时前
京东图片搜索相似商品API开发指南
大数据·数据挖掘·数据分析
镭眸19 小时前
因泰立科技:用激光雷达重塑智能工厂物流生态
大数据·人工智能·科技
AAA修煤气灶刘哥19 小时前
ES 地理查询玩明白,产品要的 “附近的店” 再也难不倒我!(附 DSL+Java 实战)
java·后端·elasticsearch
IT研究室21 小时前
大数据毕业设计选题推荐-基于大数据的贵州茅台股票数据分析系统-Spark-Hadoop-Bigdata
大数据·hadoop·spark·毕业设计·源码·数据可视化·bigdata