- Elasticsearch 指南其他版本
- 8.17 中的新功能
- Elasticsearch 基础
- 快速入门
- 设置 Elasticsearch
- 升级 Elasticsearch
- 索引模块
- 映射
- 文本分析
- 索引模板
- 数据流
- 摄取管道
- 别名
- 搜索您的数据
- 重新排名
- 查询 DSL
- 聚合
- 地理空间分析
- 连接器
- EQL
- ES|QL
- SQL
- 脚本
- 数据管理
- 自动缩放
- 监视集群
- 汇总或转换数据
- 设置高可用性集群
- 快照和还原
- 保护 Elastic Stack 的安全
- Watcher
- 命令行工具
- elasticsearch-certgen
- elasticsearch-certutil
- elasticsearch-create-enrollment-token
- elasticsearch-croneval
- elasticsearch-keystore
- elasticsearch-node
- elasticsearch-reconfigure-node
- elasticsearch-reset-password
- elasticsearch-saml-metadata
- elasticsearch-service-tokens
- elasticsearch-setup-passwords
- elasticsearch-shard
- elasticsearch-syskeygen
- elasticsearch-users
- 优化
- 故障排除
- 修复常见的集群问题
- 诊断未分配的分片
- 向系统中添加丢失的层
- 允许 Elasticsearch 在系统中分配数据
- 允许 Elasticsearch 分配索引
- 索引将索引分配过滤器与数据层节点角色混合,以在数据层之间移动
- 没有足够的节点来分配所有分片副本
- 单个节点上索引的分片总数已超过
- 每个节点的分片总数已达到
- 故障排除损坏
- 修复磁盘空间不足的数据节点
- 修复磁盘空间不足的主节点
- 修复磁盘空间不足的其他角色节点
- 启动索引生命周期管理
- 启动快照生命周期管理
- 从快照恢复
- 故障排除损坏的存储库
- 解决重复的快照策略失败问题
- 故障排除不稳定的集群
- 故障排除发现
- 故障排除监控
- 故障排除转换
- 故障排除 Watcher
- 故障排除搜索
- 故障排除分片容量健康问题
- 故障排除不平衡的集群
- 捕获诊断信息
- REST API
- API 约定
- 通用选项
- REST API 兼容性
- 自动缩放 API
- 行为分析 API
- 紧凑和对齐文本 (CAT) API
- 集群 API
- 跨集群复制 API
- 连接器 API
- 数据流 API
- 文档 API
- 丰富 API
- EQL API
- ES|QL API
- 功能 API
- Fleet API
- 图表探索 API
- 索引 API
- 别名是否存在
- 别名
- 分析
- 分析索引磁盘使用量
- 清除缓存
- 克隆索引
- 关闭索引
- 创建索引
- 创建或更新别名
- 创建或更新组件模板
- 创建或更新索引模板
- 创建或更新索引模板(旧版)
- 删除组件模板
- 删除悬挂索引
- 删除别名
- 删除索引
- 删除索引模板
- 删除索引模板(旧版)
- 存在
- 字段使用情况统计信息
- 刷新
- 强制合并
- 获取别名
- 获取组件模板
- 获取字段映射
- 获取索引
- 获取索引设置
- 获取索引模板
- 获取索引模板(旧版)
- 获取映射
- 导入悬挂索引
- 索引恢复
- 索引段
- 索引分片存储
- 索引统计信息
- 索引模板是否存在(旧版)
- 列出悬挂索引
- 打开索引
- 刷新
- 解析索引
- 解析集群
- 翻转
- 收缩索引
- 模拟索引
- 模拟模板
- 拆分索引
- 解冻索引
- 更新索引设置
- 更新映射
- 索引生命周期管理 API
- 推理 API
- 信息 API
- 摄取 API
- 许可 API
- Logstash API
- 机器学习 API
- 机器学习异常检测 API
- 机器学习数据帧分析 API
- 机器学习训练模型 API
- 迁移 API
- 节点生命周期 API
- 查询规则 API
- 重新加载搜索分析器 API
- 存储库计量 API
- 汇总 API
- 根 API
- 脚本 API
- 搜索 API
- 搜索应用程序 API
- 可搜索快照 API
- 安全 API
- 身份验证
- 更改密码
- 清除缓存
- 清除角色缓存
- 清除权限缓存
- 清除 API 密钥缓存
- 清除服务帐户令牌缓存
- 创建 API 密钥
- 创建或更新应用程序权限
- 创建或更新角色映射
- 创建或更新角色
- 批量创建或更新角色 API
- 批量删除角色 API
- 创建或更新用户
- 创建服务帐户令牌
- 委托 PKI 身份验证
- 删除应用程序权限
- 删除角色映射
- 删除角色
- 删除服务帐户令牌
- 删除用户
- 禁用用户
- 启用用户
- 注册 Kibana
- 注册节点
- 获取 API 密钥信息
- 获取应用程序权限
- 获取内置权限
- 获取角色映射
- 获取角色
- 查询角色
- 获取服务帐户
- 获取服务帐户凭据
- 获取安全设置
- 获取令牌
- 获取用户权限
- 获取用户
- 授予 API 密钥
- 具有权限
- 使 API 密钥失效
- 使令牌失效
- OpenID Connect 准备身份验证
- OpenID Connect 身份验证
- OpenID Connect 注销
- 查询 API 密钥信息
- 查询用户
- 更新 API 密钥
- 更新安全设置
- 批量更新 API 密钥
- SAML 准备身份验证
- SAML 身份验证
- SAML 注销
- SAML 失效
- SAML 完成注销
- SAML 服务提供商元数据
- SSL 证书
- 激活用户配置文件
- 禁用用户配置文件
- 启用用户配置文件
- 获取用户配置文件
- 建议用户配置文件
- 更新用户配置文件数据
- 具有用户配置文件权限
- 创建跨集群 API 密钥
- 更新跨集群 API 密钥
- 快照和还原 API
- 快照生命周期管理 API
- SQL API
- 同义词 API
- 文本结构 API
- 转换 API
- 使用情况 API
- Watcher API
- 定义
- 迁移指南
- 发行说明
- Elasticsearch 版本 8.17.0
- Elasticsearch 版本 8.16.1
- Elasticsearch 版本 8.16.0
- Elasticsearch 版本 8.15.5
- Elasticsearch 版本 8.15.4
- Elasticsearch 版本 8.15.3
- Elasticsearch 版本 8.15.2
- Elasticsearch 版本 8.15.1
- Elasticsearch 版本 8.15.0
- Elasticsearch 版本 8.14.3
- Elasticsearch 版本 8.14.2
- Elasticsearch 版本 8.14.1
- Elasticsearch 版本 8.14.0
- Elasticsearch 版本 8.13.4
- Elasticsearch 版本 8.13.3
- Elasticsearch 版本 8.13.2
- Elasticsearch 版本 8.13.1
- Elasticsearch 版本 8.13.0
- Elasticsearch 版本 8.12.2
- Elasticsearch 版本 8.12.1
- Elasticsearch 版本 8.12.0
- Elasticsearch 版本 8.11.4
- Elasticsearch 版本 8.11.3
- Elasticsearch 版本 8.11.2
- Elasticsearch 版本 8.11.1
- Elasticsearch 版本 8.11.0
- Elasticsearch 版本 8.10.4
- Elasticsearch 版本 8.10.3
- Elasticsearch 版本 8.10.2
- Elasticsearch 版本 8.10.1
- Elasticsearch 版本 8.10.0
- Elasticsearch 版本 8.9.2
- Elasticsearch 版本 8.9.1
- Elasticsearch 版本 8.9.0
- Elasticsearch 版本 8.8.2
- Elasticsearch 版本 8.8.1
- Elasticsearch 版本 8.8.0
- Elasticsearch 版本 8.7.1
- Elasticsearch 版本 8.7.0
- Elasticsearch 版本 8.6.2
- Elasticsearch 版本 8.6.1
- Elasticsearch 版本 8.6.0
- Elasticsearch 版本 8.5.3
- Elasticsearch 版本 8.5.2
- Elasticsearch 版本 8.5.1
- Elasticsearch 版本 8.5.0
- Elasticsearch 版本 8.4.3
- Elasticsearch 版本 8.4.2
- Elasticsearch 版本 8.4.1
- Elasticsearch 版本 8.4.0
- Elasticsearch 版本 8.3.3
- Elasticsearch 版本 8.3.2
- Elasticsearch 版本 8.3.1
- Elasticsearch 版本 8.3.0
- Elasticsearch 版本 8.2.3
- Elasticsearch 版本 8.2.2
- Elasticsearch 版本 8.2.1
- Elasticsearch 版本 8.2.0
- Elasticsearch 版本 8.1.3
- Elasticsearch 版本 8.1.2
- Elasticsearch 版本 8.1.1
- Elasticsearch 版本 8.1.0
- Elasticsearch 版本 8.0.1
- Elasticsearch 版本 8.0.0
- Elasticsearch 版本 8.0.0-rc2
- Elasticsearch 版本 8.0.0-rc1
- Elasticsearch 版本 8.0.0-beta1
- Elasticsearch 版本 8.0.0-alpha2
- Elasticsearch 版本 8.0.0-alpha1
- 依赖项和版本
搜索应用程序搜索 API 和模板
编辑搜索应用程序搜索 API 和模板
编辑您的搜索应用程序使用搜索模板来执行搜索。模板通过仅公开模板参数来帮助降低复杂性,同时使用 Elasticsearch 查询 DSL 的全部功能来制定查询。模板可以在创建或更新搜索应用程序时设置,并且可以自定义。可以使用 Put 搜索应用程序 API API 调用随时编辑或更新此模板。
简而言之,您可以使用参数而不是特定的硬编码搜索值来创建搜索模板。在搜索时,您传入这些参数的实际值,从而实现自定义搜索,而无需重写整个查询结构。搜索应用程序模板
- 简化查询请求
- 减小请求大小
- 确保安全性和性能,因为查询是预定义的,不能任意更改
本文档提供信息和示例模板,以帮助您开始使用搜索应用程序以进行其他用例。这些模板旨在易于修改以满足您的需求。使用模板创建搜索应用程序后,您可以使用此模板搜索您的搜索应用程序。
默认模板示例
编辑如果搜索应用程序中未存储模板,则将在搜索时应用最小默认搜索模板。默认模板实现了一个简单的搜索用例。
要使用默认模板创建搜索应用程序,请发出创建或更新搜索应用程序请求,而无需指定模板
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1", "index2" ] }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1", "index2"], }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": ["index1", "index2"] }
然后,您可以使用获取搜索应用程序 API 调用来查看您新创建的搜索应用程序,其中还将包括为您创建的默认模板
resp = client.search_application.get( name="my_search_application", ) print(resp)
const response = await client.searchApplication.get({ name: "my_search_application", }); console.log(response);
GET _application/search_application/my_search_application
在这种情况下,响应将是
{ "name": "my_search_application", "indices": [ "index1", "index2" ], "updated_at_millis": 1715802354482, "template": { "script": { "source": """{ "query": { "query_string": { "query": "{{query_string}}", "default_field": "{{default_field}}" } } } """, "lang": "mustache", "params": { "default_field": "*", "query_string": "*" } } } }
默认模板非常简单
{ "template": { "script": { "source": { "query": { "query_string": { "query": "{{query_string}}", "default_field": "{{default_field}}" } } }, "params": { "query_string": "*", "default_field": "*" } } } }
这可能有助于初始探索搜索模板,但您可能需要更新它。
此模板不支持其他参数,包括 from
、size
或 boost
。如果需要使用这些参数,可以相应地自定义与您的搜索应用程序关联的模板,以将其作为参数包括在内。
您可以通过查看模板来查看参数及其默认值,但如果您使用各种参数搜索您的搜索应用程序,查看将生成的查询也可能很有价值。
您可以使用渲染搜索应用程序查询来查看此模板将生成的查询,包括默认参数。例如,使用不带任何参数搜索搜索应用程序
resp = client.search_application.render_query( name="my_search_application", body=None, ) print(resp)
const response = await client.searchApplication.renderQuery({ name: "my_search_application", body: null, }); console.log(response);
POST _application/search_application/my_search_application/_render_query
将返回
{ "query": { "query_string": { "query": "*", "default_field": "*", "fields": [] } } }
这使用与模板一起定义的默认参数。您还可以为渲染调用指定一个或多个参数,例如
resp = client.search_application.render_query( name="my_search_application", body={ "params": { "query_string": "rock climbing" } }, ) print(resp)
const response = await client.searchApplication.renderQuery({ name: "my_search_application", body: { params: { query_string: "rock climbing", }, }, }); console.log(response);
POST _application/search_application/my_search_application/_render_query { "params": { "query_string": "rock climbing" } }
将返回
{ "query": { "query_string": { "query": "rock climbing", "default_field": "*", "fields": [] } } }
在这种情况下,{{query_string}}
参数已替换为值 rock climbing
,并且未指定 {{default_field}}
参数,因此使用了默认值 *
。
当您实际执行不带任何参数的搜索时,它将执行渲染调用返回的底层查询。在这种情况下,不带任何参数的搜索将返回所有结果,类似于对 /_search
进行的无参数调用。
resp = client.search_application.search( name="my_search_application", ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", }); console.log(response);
POST _application/search_application/my_search_application/_search
使用 query_string
和/或 default_field
参数进行搜索将执行 query_string
查询。
默认模板可能会在搜索应用程序功能的未来版本中更改。
尝试本文档中的其他示例以试验特定用例,或尝试创建您自己的示例!
搜索搜索应用程序
编辑模板搜索
编辑与搜索应用程序交互的最简单方法是使用与其创建和存储的搜索模板。每个搜索应用程序都有一个与其关联的模板,该模板定义搜索条件、参数和默认值。
您可以使用搜索应用程序搜索 API 向搜索应用程序发送搜索请求。
使用默认模板,搜索看起来像这样
resp = client.search_application.search( name="my_search_application", params={ "query_string": "kayaking" }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { query_string: "kayaking", }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "query_string": "kayaking" } }
在此示例中,我们已覆盖 query_string
参数的默认值 *
。由于我们未指定 default_field
,因此此参数的值仍将为 *
。
别名搜索
编辑如果您不想为搜索应用程序设置搜索模板,则会创建一个与您的搜索应用程序同名的别名。当您尝试使用在构建搜索应用程序的搜索模板时想要使用的特定搜索查询时,这可能会有所帮助。
如果您的搜索应用程序的名称为 my_search_application
,则您的别名将为 my_search_application
。您可以使用搜索 API 搜索此内容。
跨集群搜索
编辑搜索应用程序目前不支持跨集群搜索,因为无法将远程集群的索引或索引模式添加到索引别名。
您应使用搜索应用程序管理 API 来更新您的应用程序,而不直接使用 Elasticsearch API,例如别名 API。例如,使用带有 indices
参数的 PUT 搜索应用程序。这将自动使关联的别名保持最新,并确保将索引正确添加到搜索应用程序。
搜索模板示例
编辑我们创建了许多示例来探索特定用例。将这些用作创建您自己的搜索模板的起点。
文本搜索示例
编辑以下模板支持在指定字段和 boost 上进行 multi_match
搜索
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1", "index2" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"query\": {\n \"multi_match\": {\n \"query\": \"{{query_string}}\",\n \"fields\": [{{#text_fields}}\"{{name}}^{{boost}}\",{{/text_fields}}]\n }\n },\n \"explain\": \"{{explain}}\",\n \"from\": \"{{from}}\",\n \"size\": \"{{size}}\"\n }\n ", "params": { "query_string": "*", "text_fields": [ { "name": "title", "boost": 10 }, { "name": "description", "boost": 5 } ], "explain": False, "from": 0, "size": 10 } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1", "index2"], template: { script: { lang: "mustache", source: '\n {\n "query": {\n "multi_match": {\n "query": "{{query_string}}",\n "fields": [{{#text_fields}}"{{name}}^{{boost}}",{{/text_fields}}]\n }\n },\n "explain": "{{explain}}",\n "from": "{{from}}",\n "size": "{{size}}"\n }\n ', params: { query_string: "*", text_fields: [ { name: "title", boost: 10, }, { name: "description", boost: 5, }, ], explain: false, from: 0, size: 10, }, }, }, }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": ["index1", "index2"], "template": { "script": { "lang": "mustache", "source": """ { "query": { "multi_match": { "query": "{{query_string}}", "fields": [{{#text_fields}}"{{name}}^{{boost}}",{{/text_fields}}] } }, "explain": "{{explain}}", "from": "{{from}}", "size": "{{size}}" } """, "params": { "query_string": "*", "text_fields": [ {"name": "title", "boost": 10}, {"name": "description", "boost": 5} ], "explain": false, "from": 0, "size": 10 } } } }
使用此模板的搜索查询可能如下所示
resp = client.search_application.search( name="my_search_application", params={ "size": 5, "query_string": "mountain climbing", "text_fields": [ { "name": "title", "boost": 10 }, { "name": "description", "boost": 2 }, { "name": "state", "boost": 1 } ] }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { size: 5, query_string: "mountain climbing", text_fields: [ { name: "title", boost: 10, }, { name: "description", boost: 2, }, { name: "state", boost: 1, }, ], }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "size": 5, "query_string": "mountain climbing", "text_fields": [ {"name": "title", "boost": 10}, {"name": "description", "boost": 2}, {"name": "state", "boost": 1} ] } }
可以使用新的/不同的字段和 boost 来覆盖 text_fields
参数,以试验最适合您用例的配置。此模板还通过参数支持分页和 explain
。
文本搜索 + ELSER(带 RRF)
编辑此示例支持用于组合 BM25 和 ELSER 搜索的倒数排名融合 (RRF)] 方法。倒数排名融合可以持续改进不同搜索算法的组合结果。它优于所有其他排名算法,并且通常无需校准即可超越最佳的单个结果。
resp = client.search_application.put( name="my-search-app", search_application={ "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"retriever\": {\n \"rrf\": {\n \"retrievers\": [\n {{#text_fields}}\n {\n \"standard\": {\n \"query\": {\n \"match\": {\n \"{{.}}\": \"{{query_string}}\"\n }\n }\n }\n },\n {{/text_fields}}\n {{#elser_fields}}\n {\n \"standard\": {\n \"query\": {\n \"sparse_vector\": {\n \"field\": \"ml.inference.{{.}}_expanded.predicted_value\",\n \"inference_id\": \"<elser_inference_id>\",\n \"query\": \"{{query_string}}\"\n }\n }\n }\n },\n {{/elser_fields}}\n ],\n \"rank_window_size\": {{rrf.rank_window_size}},\n \"rank_constant\": {{rrf.rank_constant}}\n }\n }\n }\n ", "params": { "elser_fields": [ "title", "meta_description" ], "text_fields": [ "title", "meta_description" ], "query_string": "", "rrf": { "rank_window_size": 100, "rank_constant": 60 } } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my-search-app", search_application: { indices: ["index1"], template: { script: { lang: "mustache", source: '\n {\n "retriever": {\n "rrf": {\n "retrievers": [\n {{#text_fields}}\n {\n "standard": {\n "query": {\n "match": {\n "{{.}}": "{{query_string}}"\n }\n }\n }\n },\n {{/text_fields}}\n {{#elser_fields}}\n {\n "standard": {\n "query": {\n "sparse_vector": {\n "field": "ml.inference.{{.}}_expanded.predicted_value",\n "inference_id": "<elser_inference_id>",\n "query": "{{query_string}}"\n }\n }\n }\n },\n {{/elser_fields}}\n ],\n "rank_window_size": {{rrf.rank_window_size}},\n "rank_constant": {{rrf.rank_constant}}\n }\n }\n }\n ', params: { elser_fields: ["title", "meta_description"], text_fields: ["title", "meta_description"], query_string: "", rrf: { rank_window_size: 100, rank_constant: 60, }, }, }, }, }, }); console.log(response);
PUT _application/search_application/my-search-app { "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": """ { "retriever": { "rrf": { "retrievers": [ {{#text_fields}} { "standard": { "query": { "match": { "{{.}}": "{{query_string}}" } } } }, {{/text_fields}} {{#elser_fields}} { "standard": { "query": { "sparse_vector": { "field": "ml.inference.{{.}}_expanded.predicted_value", "inference_id": "<elser_inference_id>", "query": "{{query_string}}" } } } }, {{/elser_fields}} ], "rank_window_size": {{rrf.rank_window_size}}, "rank_constant": {{rrf.rank_constant}} } } } """, "params": { "elser_fields": ["title", "meta_description"], "text_fields": ["title", "meta_description"], "query_string": "", "rrf": { "rank_window_size": 100, "rank_constant": 60 } } } } }
将 <elser_model_id>
替换为 ELSER 部署的模型 ID。
此模板的示例查询将如下所示
resp = client.search_application.search( name="my-search-app", params={ "query_string": "What is the most popular brand of coffee sold in the United States?", "elser_fields": [ "title", "meta_description" ], "text_fields": [ "title", "meta_description" ], "rrf": { "rank_window_size": 50, "rank_constant": 25 } }, ) print(resp)
const response = await client.searchApplication.search({ name: "my-search-app", params: { query_string: "What is the most popular brand of coffee sold in the United States?", elser_fields: ["title", "meta_description"], text_fields: ["title", "meta_description"], rrf: { rank_window_size: 50, rank_constant: 25, }, }, }); console.log(response);
POST _application/search_application/my-search-app/_search { "params": { "query_string": "What is the most popular brand of coffee sold in the United States?", "elser_fields": ["title", "meta_description"], "text_fields": ["title", "meta_description"], "rrf": { "rank_window_size": 50, "rank_constant": 25 } } }
文本搜索 + ELSER
编辑Elastic Learned Sparse EncodeR (ELSER) 通过文本扩展来提高搜索相关性,从而实现语义搜索。此实验性模板要求为一个或多个字段启用 ELSER。有关如何使用 ELSER 的更多信息,请参阅使用 ELSER 进行语义搜索。在这种情况下,已在 title
和 description
字段上启用 ELSER。
此示例提供了一个可用于各种搜索应用程序场景的单个模板:文本搜索、ELSER 或上述所有场景。如果未指定参数,它还提供一个简单的默认 query_string
查询。
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1", "index2" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"query\": {\n \"bool\": {\n \"should\": [\n {{#text}}\n {\n \"multi_match\": {\n \"query\": \"{{query_string}}\",\n \"fields\": [{{#text_fields}}\"{{name}}^{{boost}}\",{{/text_fields}}],\n \"boost\": \"{{text_query_boost}}\"\n }\n },\n {{/text}}\n {{#elser}}\n {{#elser_fields}}\n {\n \"sparse_vector\": {\n \"field\": \"ml.inference.{{.}}_expanded.predicted_value\",\n \"inference_id\": \"<elser_inference_id>\",\n \"query\": \"{{query_string}}\"\n }\n },\n {{/elser_fields}}\n { \"bool\": { \"must\": [] } },\n {{/elser}}\n {{^text}}\n {{^elser}}\n {\n \"query_string\": {\n \"query\": \"{{query_string}}\",\n \"default_field\": \"{{default_field}}\",\n \"default_operator\": \"{{default_operator}}\",\n \"boost\": \"{{text_query_boost}}\"\n }\n },\n {{/elser}}\n {{/text}}\n { \"bool\": { \"must\": [] } }\n ],\n \"minimum_should_match\": 1\n }\n },\n \"min_score\": \"{{min_score}}\",\n \"explain\": \"{{explain}}\",\n \"from\": \"{{from}}\",\n \"size\": \"{{size}}\"\n }\n ", "params": { "text": False, "elser": False, "elser_fields": [ { "name": "title", "boost": 1 }, { "name": "description", "boost": 1 } ], "text_fields": [ { "name": "title", "boost": 10 }, { "name": "description", "boost": 5 }, { "name": "state", "boost": 1 } ], "query_string": "*", "text_query_boost": 4, "default_field": "*", "default_operator": "OR", "explain": False, "from": 0, "size": 10, "min_score": 0 } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1", "index2"], template: { script: { lang: "mustache", source: '\n {\n "query": {\n "bool": {\n "should": [\n {{#text}}\n {\n "multi_match": {\n "query": "{{query_string}}",\n "fields": [{{#text_fields}}"{{name}}^{{boost}}",{{/text_fields}}],\n "boost": "{{text_query_boost}}"\n }\n },\n {{/text}}\n {{#elser}}\n {{#elser_fields}}\n {\n "sparse_vector": {\n "field": "ml.inference.{{.}}_expanded.predicted_value",\n "inference_id": "<elser_inference_id>",\n "query": "{{query_string}}"\n }\n },\n {{/elser_fields}}\n { "bool": { "must": [] } },\n {{/elser}}\n {{^text}}\n {{^elser}}\n {\n "query_string": {\n "query": "{{query_string}}",\n "default_field": "{{default_field}}",\n "default_operator": "{{default_operator}}",\n "boost": "{{text_query_boost}}"\n }\n },\n {{/elser}}\n {{/text}}\n { "bool": { "must": [] } }\n ],\n "minimum_should_match": 1\n }\n },\n "min_score": "{{min_score}}",\n "explain": "{{explain}}",\n "from": "{{from}}",\n "size": "{{size}}"\n }\n ', params: { text: false, elser: false, elser_fields: [ { name: "title", boost: 1, }, { name: "description", boost: 1, }, ], text_fields: [ { name: "title", boost: 10, }, { name: "description", boost: 5, }, { name: "state", boost: 1, }, ], query_string: "*", text_query_boost: 4, default_field: "*", default_operator: "OR", explain: false, from: 0, size: 10, min_score: 0, }, }, }, }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": [ "index1", "index2" ], "template": { "script": { "lang": "mustache", "source": """ { "query": { "bool": { "should": [ {{#text}} { "multi_match": { "query": "{{query_string}}", "fields": [{{#text_fields}}"{{name}}^{{boost}}",{{/text_fields}}], "boost": "{{text_query_boost}}" } }, {{/text}} {{#elser}} {{#elser_fields}} { "sparse_vector": { "field": "ml.inference.{{.}}_expanded.predicted_value", "inference_id": "<elser_inference_id>", "query": "{{query_string}}" } }, {{/elser_fields}} { "bool": { "must": [] } }, {{/elser}} {{^text}} {{^elser}} { "query_string": { "query": "{{query_string}}", "default_field": "{{default_field}}", "default_operator": "{{default_operator}}", "boost": "{{text_query_boost}}" } }, {{/elser}} {{/text}} { "bool": { "must": [] } } ], "minimum_should_match": 1 } }, "min_score": "{{min_score}}", "explain": "{{explain}}", "from": "{{from}}", "size": "{{size}}" } """, "params": { "text": false, "elser": false, "elser_fields": [ {"name": "title", "boost": 1}, {"name": "description", "boost": 1} ], "text_fields": [ {"name": "title", "boost": 10}, {"name": "description", "boost": 5}, {"name": "state", "boost": 1} ], "query_string": "*", "text_query_boost": 4, "default_field": "*", "default_operator": "OR", "explain": false, "from": 0, "size": 10, "min_score": 0 } } } }
使用此模板的文本搜索查询可能如下所示
resp = client.search_application.search( name="my_search_application", params={ "text": True, "size": 5, "query_string": "mountain climbing", "text_fields": [ { "name": "title", "boost": 10 }, { "name": "description", "boost": 5 }, { "name": "state", "boost": 1 } ] }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { text: true, size: 5, query_string: "mountain climbing", text_fields: [ { name: "title", boost: 10, }, { name: "description", boost: 5, }, { name: "state", boost: 1, }, ], }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "text": true, "size": 5, "query_string": "mountain climbing", "text_fields": [ {"name": "title", "boost": 10}, {"name": "description", "boost": 5}, {"name": "state", "boost": 1} ] } }
使用此模板的 ELSER 搜索查询将如下所示
resp = client.search_application.search( name="my_search_application", params={ "elser": True, "query_string": "where is the best mountain climbing?", "elser_fields": [ { "name": "title", "boost": 1 }, { "name": "description", "boost": 1 } ] }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { elser: true, query_string: "where is the best mountain climbing?", elser_fields: [ { name: "title", boost: 1, }, { name: "description", boost: 1, }, ], }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "elser": true, "query_string": "where is the best mountain climbing?", "elser_fields": [ {"name": "title", "boost": 1}, {"name": "description", "boost": 1} ] } }
使用此模板的组合文本搜索和 ELSER 搜索查询将如下所示
resp = client.search_application.search( name="my_search_application", params={ "elser": True, "text": True, "query_string": "where is the best mountain climbing?", "elser_fields": [ { "name": "title", "boost": 1 }, { "name": "description", "boost": 1 } ], "text_query_boost": 4, "min_score": 10 }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { elser: true, text: true, query_string: "where is the best mountain climbing?", elser_fields: [ { name: "title", boost: 1, }, { name: "description", boost: 1, }, ], text_query_boost: 4, min_score: 10, }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "elser": true, "text": true, "query_string": "where is the best mountain climbing?", "elser_fields": [ {"name": "title", "boost": 1}, {"name": "description", "boost": 1} ], "text_query_boost": 4, "min_score": 10 } }
在某些情况下,文本搜索结果和 ELSER 搜索结果的评分预计会明显不同,这使得排名具有挑战性。要为您的数据集找到最佳的搜索结果组合,我们建议试验示例模板中提供的 boost 值
上述 boost 应该足以满足许多用例,但在某些情况下,向模板添加 重新评分 查询或索引提升可能会有所帮助。请记住使用put 搜索应用程序命令来更新您的搜索应用程序,以使用新模板。
最后,使用此模板的无参数搜索将回退到返回所有文档的默认搜索
resp = client.search_application.search( name="my_search_application", ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", }); console.log(response);
POST _application/search_application/my_search_application/_search
ELSER 搜索
编辑此示例支持简化的 ELSER 搜索版本。
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1", "index2" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"query\": {\n \"bool\": {\n \"should\": [\n {{#elser_fields}}\n {\n \"sparse_vector\": {\n \"field\": \"ml.inference.{{.}}_expanded.predicted_value\",\n \"inference_id\": \"<elser_inference_id>\",\n \"query\": \"{{query_string}}\"\n }\n },\n {{/elser_fields}}\n ]\n }\n },\n \"min_score\": \"{{min_score}}\"\n }\n ", "params": { "query_string": "*", "min_score": "10", "elser_fields": [ { "name": "title" }, { "name": "description" } ] } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1", "index2"], template: { script: { lang: "mustache", source: '\n {\n "query": {\n "bool": {\n "should": [\n {{#elser_fields}}\n {\n "sparse_vector": {\n "field": "ml.inference.{{.}}_expanded.predicted_value",\n "inference_id": "<elser_inference_id>",\n "query": "{{query_string}}"\n }\n },\n {{/elser_fields}}\n ]\n }\n },\n "min_score": "{{min_score}}"\n }\n ', params: { query_string: "*", min_score: "10", elser_fields: [ { name: "title", }, { name: "description", }, ], }, }, }, }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": [ "index1", "index2" ], "template": { "script": { "lang": "mustache", "source": """ { "query": { "bool": { "should": [ {{#elser_fields}} { "sparse_vector": { "field": "ml.inference.{{.}}_expanded.predicted_value", "inference_id": "<elser_inference_id>", "query": "{{query_string}}" } }, {{/elser_fields}} ] } }, "min_score": "{{min_score}}" } """, "params": { "query_string": "*", "min_score": "10", "elser_fields": [ { "name": "title" }, { "name": "description" } ] } } } }
将 <elser_model_id>
替换为 ELSER 部署的模型 ID。
此模板的示例查询将如下所示
resp = client.search_application.search( name="my_search_application", params={ "query_string": "Where is the best place for mountain climbing?" }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { query_string: "Where is the best place for mountain climbing?", }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "query_string": "Where is the best place for mountain climbing?" } }
kNN 搜索
编辑此示例支持k 最近邻 (kNN) 搜索
支持精确 kNN 搜索的模板将如下所示
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"query\": {\n \"script_score\": {\n \"query\": {\n \"bool\": {\n \"filter\": {\n \"range\": {\n \"{{field}}\": {\n \"{{operator}}\": {{value}}\n }\n }\n }\n }\n },\n \"script\": {\n \"source\": \"cosineSimilarity({{#toJson}}query_vector{{/toJson}}, '{{dense_vector_field}}') + 1.0\"\n }\n }\n }\n }\n ", "params": { "field": "price", "operator": "gte", "value": 1000, "dense_vector_field": "product-vector", "query_vector": [] } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1"], template: { script: { lang: "mustache", source: '\n {\n "query": {\n "script_score": {\n "query": {\n "bool": {\n "filter": {\n "range": {\n "{{field}}": {\n "{{operator}}": {{value}}\n }\n }\n }\n }\n },\n "script": {\n "source": "cosineSimilarity({{#toJson}}query_vector{{/toJson}}, \'{{dense_vector_field}}\') + 1.0"\n }\n }\n }\n }\n ', params: { field: "price", operator: "gte", value: 1000, dense_vector_field: "product-vector", query_vector: [], }, }, }, }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": """ { "query": { "script_score": { "query": { "bool": { "filter": { "range": { "{{field}}": { "{{operator}}": {{value}} } } } } }, "script": { "source": "cosineSimilarity({{#toJson}}query_vector{{/toJson}}, '{{dense_vector_field}}') + 1.0" } } } } """, "params": { "field": "price", "operator": "gte", "value": 1000, "dense_vector_field": "product-vector", "query_vector": [] } } } }
使用此模板的搜索查询将如下所示
resp = client.search_application.search( name="my_search_application", params={ "field": "price", "operator": "gte", "value": 500 }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { field: "price", operator: "gte", value: 500, }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "field": "price", "operator": "gte", "value": 500 } }
支持近似 kNN 搜索的模板将如下所示
resp = client.search_application.put( name="my_search_application", search_application={ "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": "\n {\n \"knn\": {\n \"field\": \"{{knn_field}}\",\n \"query_vector\": {{#toJson}}query_vector{{/toJson}},\n \"k\": \"{{k}}\",\n \"num_candidates\": {{num_candidates}}\n },\n \"fields\": {{#toJson}}fields{{/toJson}}\n }\n ", "params": { "knn_field": "image-vector", "query_vector": [], "k": 10, "num_candidates": 100, "fields": [ "title", "file-type" ] } } } }, ) print(resp)
const response = await client.searchApplication.put({ name: "my_search_application", search_application: { indices: ["index1"], template: { script: { lang: "mustache", source: '\n {\n "knn": {\n "field": "{{knn_field}}",\n "query_vector": {{#toJson}}query_vector{{/toJson}},\n "k": "{{k}}",\n "num_candidates": {{num_candidates}}\n },\n "fields": {{#toJson}}fields{{/toJson}}\n }\n ', params: { knn_field: "image-vector", query_vector: [], k: 10, num_candidates: 100, fields: ["title", "file-type"], }, }, }, }, }); console.log(response);
PUT _application/search_application/my_search_application { "indices": [ "index1" ], "template": { "script": { "lang": "mustache", "source": """ { "knn": { "field": "{{knn_field}}", "query_vector": {{#toJson}}query_vector{{/toJson}}, "k": "{{k}}", "num_candidates": {{num_candidates}} }, "fields": {{#toJson}}fields{{/toJson}} } """, "params": { "knn_field": "image-vector", "query_vector": [], "k": 10, "num_candidates": 100, "fields": ["title", "file-type"] } } } }
使用此模板的搜索查询将如下所示
resp = client.search_application.search( name="my_search_application", params={ "knn_field": "image-vector", "query_vector": [ -5, 9, -12 ], "k": 10, "num_candidates": 100, "fields": [ "title", "file-type" ] }, ) print(resp)
const response = await client.searchApplication.search({ name: "my_search_application", params: { knn_field: "image-vector", query_vector: [-5, 9, -12], k: 10, num_candidates: 100, fields: ["title", "file-type"], }, }); console.log(response);
POST _application/search_application/my_search_application/_search { "params": { "knn_field": "image-vector", "query_vector": [-5, 9, -12], "k": 10, "num_candidates": 100, "fields": ["title", "file-type"] } }