搜索应用程序客户端
搜索应用程序客户端是一个JavaScript库,提供功能,用于在Elasticsearch中与搜索应用程序互动.
iQQ 搜索应用程序客户端在 技术预览 并且可以更改.
快速访问
以下是与重要章节的快速链接:
安装
要安装搜索应用程序客户端,您可以使用npm或纱线.
使用 npm :
npm install @elastic/search-application-client
使用线条 :
yarn add @elastic/search-application-client
通过 CDN 在浏览器中
<script src="https://cdn.jsdelivr.net/npm/@elastic/search-application-client@latest"></script>
使用量
导入客户端
您可以使用以下导入语句在 JavaScript 或 TypeScript 文件中导入搜索应用程序客户端 :
import SearchApplicationClient from '@elastic/search-application-client';
创建客户实例
要使用搜索应用程序客户端, 请使用导入模块返回的函数创建实例 :
const client = SearchApplicationClient(applicationName, endpoint, apiKey, params);
- 应用程序 名称:搜索应用程序的名称.
- 端点:搜索应用程序端点的URL.
- apiKey:用于访问搜索应用程序的API密钥.
- 参数(可选):要传递给客户端的额外参数.
- api 选项( 选项): 要传递给 API 客户端的附加选项 .
- 缓存扩展( 可选) : 以毫秒计的缓存到期时间 。 默认: 360000 (一个小时).
- 缓存( 可选的): 启用/ 禁用缓存 。 默认: 真.
- 头 (可选):要传递给API客户端的附加头.
- 返回 : 返回新的 QueryBuilder 实例的函数 .
使用客户端
搜索应用程序客户端提供了一个查询构建类,允许您构建复杂的搜索查询 :
const results = await client()
.query('John')
.search()
搜索方法返回随搜索结果而解决的保证 .
使用侧面
要使用侧面过滤, 请在客户端初始化中指定侧面 :
const client = SearchApplicationClient('*search-application-name*', '*elasticsearch-endpoint*', '*apiKey*', {
facets: {
category: { type: 'terms', field: 'category.keyword', size: 10 },
price: { type: 'stats', field: 'price', size: 10, disjunctive: true },
},
});
const results = await client()
.addFacetFilter('category', 'electronics')
.addFacetFilter('price', { from: 100, to: 1000 })
.addFacetFilter('author', 'orwell') // ❌ Throw an error, because the facet is not specified during initialization
.setSort({'price': 'desc'})
.search()
使用排序
要使用排序, 请指定字段名称和排序顺序或通过 score 来按相关排序 :
const results = await client()
.setSort([{'publish_date': 'desc'}, "_score"])
.search()
使用页码
要使用页码,请设置页码和页码 :
const results = await client()
.setPageSize(20)
.setFrom(20 * 2) // For the third page
.search()
默认的页面大小为 10
使用额外参数
const results = await client()
.addParameter('custom-parameter', 'custom-value')
.search()
API 参考
SearchApplicationClient()
初始化搜索功能 应用程序 Client and return 函数创建查询构建器实例 .
applicationName (string): 搜索应用程序的名称.endpoint (string): 搜索应用程序端点的URL.apiKey (string): 访问搜索应用程序的 API 密钥 .params? (Params): 附加参数将传递给客户端.
返回:返回新查询构建实例的函数.
QueryBuilder
此类提供了构建搜索查询的方法.
-
query(query: string): QueryBuilder设置搜索查询。 返回:查询构建实例. -
addFacetFilter(field: string, value: FilterFieldValue): QueryBuilder添加一面过滤器来缩小搜索结果。 返回:查询构建实例.field (string):面部过滤器的字段名.value (string | string[] | number | number[]):平面过滤器的值或值范围.
-
addParameter(field: string, value: unkown): QueryBuilder在查询中添加参数。 返回:查询构建实例. -
setPageSize(size: number): QueryBuilder为返回的最大结果数设定大小。 返回:查询构建实例. -
setFrom(value: number): QueryBuilder定义要跳过的结果数,默认为 0。 返回:查询构建实例. -
setSort(sort: SortFields): QueryBuilder设置搜索结果的排序标准。 返回:查询构建实例.sort (Record<string, 'desc' | 'asc'> | '_score')[]): 搜索结果的排序标准.
-
search(): Promise<ResponseParams>向搜索应用程序发送搜索请求。 使用通用类型指定hits._source类型. 返回:一个解决搜索结果的承诺.- 点击:搜索点击数组.
- 方方面面:一系列表面结果.
- 汇总:包含汇总结果的对象.
- 总计:搜索结果的总数.
- 采取: 搜索请求的时间.
关于现有方法及其参数的更详细资料,请参见代码和类型定义.
高级使用
沸腾模板
PUT _application/search_application/example-app
{
"indices": ["example-index"],
"template": {
"script": {
"lang": "mustache",
"source": """
{
"query": {
"bool": {
"must": [
{{#query}}
{
"query_string": {
"query": "{{query}}"
}
}
{{/query}}
],
"filter": {{#toJson}}_es_filters{{/toJson}}
}
},
"aggs": {{#toJson}}_es_aggs{{/toJson}},
"from": {{from}},
"size": {{size}},
"sort": {{#toJson}}_es_sort_fields{{/toJson}}
}
""",
"params": {
"query": "",
"_es_filters": {},
"_es_aggs": {},
"_es_sort_fields": {},
"size": 10,
"from": 0
}
}
}
}
使用自定义模板
要使用自定义模板, 您可以在特定属性或突出属性等参数内更新锅炉板模板 :
PUT _application/search_application/example-app
{
"indices": ["example-index"],
"template": {
"script": {
"lang": "mustache",
"source": """
{
"query": {
"bool": {
"must": [
{{#query}}
// ...
{{/query}}
],
"filter": {{#toJson}}_es_filters{{/toJson}}
}
},
"_source": {
"includes": ["title", "plot"]
},
"highlight": {
"fields": {
"title": { "fragment_size": 0 },
"plot": { "fragment_size": 200 }
}
},
"aggs": {{#toJson}}_es_aggs{{/toJson}},
"from": {{from}},
"size": {{size}},
"sort": {{#toJson}}_es_sort_fields{{/toJson}}
}
""",
"params": {
"query": "",
"_es_filters": {},
"_es_aggs": {},
"_es_sort_fields": {},
"size": 10,
"from": 0
}
}
}
}
模板使用词典
要使用有打字检查的模板,您可以通过运行命令以预建的json schema更新您的模板 :
npx @elastic/search-application-client update-template
一旦您更新了模板, 它将使用计划来对每个请求进行参数的打字检查 .
实例
您可以在 实例/框 此仓库的目录 。 这些例子说明如何使用搜索-应用程序-客户端库.