# @elastic/app-search-javascript 中文文档

规范地址：https://npmcn.com/package/%40elastic/app-search-javascript/

版本：8\.13\.0
npm 元数据更新时间：2026-09-25T13:38:03.742Z
中文译文更新时间：2026-11-13T10:33:59.880Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40elastic/app-search-javascript
原文对照：https://npmcn.com/package/%40elastic/app-search-javascript/?lang=original
项目主页：https://github.com/elastic/app-search-javascript
源码仓库：https://github.com/elastic/app-search-javascript

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：278 kB，57 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：1 个
- 首次发布：2019-08-16T17:57:47.556Z
- 最新版本发布：2024-03-26T16:29:58.511Z
- 近 30 天下载量：211329（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：28（近 12 个月 0 个）

## 中文简介

Elastic App 搜索 Api 的 Javascript 客户端

## 安装

```sh
npm install @elastic/app-search-javascript
```

也可以使用：`pnpm add @elastic/app-search-javascript`、`yarn add @elastic/app-search-javascript`、`bun add @elastic/app-search-javascript`

## README

![Elastic App Search Logo](https://github.com/elastic/app-search-javascript/blob/master/logo-app-search.png?raw=true)

[![CircleCI buidl](https://circleci.com/gh/elastic/app-search-javascript.svg?style=svg)](https://circleci.com/gh/elastic/app-search-javascript)

> 用于构建优秀相关搜索体验的第一党JavaScript客户端 [弹性 App 搜索](https://www.elastic.co/products/app-search)。 。 。 .

## 目录

-   [开始](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-getting-started-)
-   [版本](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-versioning)
-   [浏览器支持](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-browser-support)
-   [使用量](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-usage)
-   [正在测试](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-running-tests)
-   [发展](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-development)
-   [财务问题](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-faq-)
-   [贡献](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-contribute-)
-   [许可证](https://npmcn.com/package/%40elastic/app-search-javascript/#readme-license-)

* * *

## 开始 QQ

### 从 CDN 安装

安装此客户端的最简单方式就是简单地包含从 [日记](https://www.jsdelivr.com/) 疾控网.

```html
<script src="https://cdn.jsdelivr.net/npm/@elastic/app-search-javascript@8.13.0/dist/elastic_app_search.umd.js"></script>
```

这将使客户在全球通过以下方式获得服务:

```javascript
window.ElasticAppSearch;
```

### 从国家预防机制安装

此软件包也可以安装 `npm` 或者说 `yarn`。 。 。 .

```
npm install --save @elastic/app-search-javascript
```

然后,可以将客户纳入您的项目如下:

```javascript
// CommonJS
var ElasticAppSearch = require("@elastic/app-search-javascript");

// ES
import * as ElasticAppSearch from "@elastic/app-search-javascript";
```

## 版本

这个客户端在App Search同时被版本并发布.

为保证相容性,在相应的App Search执行的主要版本中使用此库的最新版本.

例如, App 搜索 `7.3`,使用时 `7.3` 本图书馆或以上,但不是 `8.0`。 。 。 .

如果你正在使用 [SaaS 版本可在sourkype.com上查阅](https://app.swiftype.com/as) app Search中,您应当使用客户端的7.5.x版本.

## 浏览器支持

该客户端与所有现代浏览器兼容.

请注意,此库取决于 [获取 API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)。 。 。 .

这不受Internet Explorer的支持. 如果您需要向后兼容 对于 Internet Explorer , 您需要将获取 API 的内容进行多功能填充 喜欢 [https://github.com/github/fetch (英语)](https://github.com/github/fetch)。 。 。 .

## 使用量

### 设置: 配置客户端和认证

使用此客户端假设您已有实例 [弹性 App 搜索](https://www.elastic.co/products/app-search) 继续跑.

客户端使用 `searchKey`, (中文(简体) ), `endpointBase`,以及 `engineName` 参数.

```javascript
var client = ElasticAppSearch.createClient({
  searchKey: "search-mu75psc5egt9ppzuycnc2mc3",
  endpointBase: "http://127.0.0.1:3002",
  engineName: "favorite-videos"
});
```

\* 请注意,你只能使用 **公共搜索密钥** 在浏览器上的 Javascript 代码中。 默认情况下,您的账户应该有一个密钥前缀 `search-` 这是只读。 更多信息可在 [文档](https://swiftype.com/documentation/app-search/authentication)。 。 。 .

### Swiftype.com App搜索用户 :

使用时 [SaaS 版本可在sourkype.com上查阅](https://app.swiftype.com/as) 在 App 搜索中,您可以使用您的 `hostIdentifier` 代替 `endpointBase` 参数. 这个 `hostIdentifier` 可在 [全权证书](https://app.swiftype.com/as#/credentials) 菜单.

```javascript
var client = ElasticAppSearch.createClient({
  hostIdentifier: "host-c5s2mj",
  searchKey: "search-mu75psc5egt9ppzuycnc2mc3",
  engineName: "favorite-videos"
});
```

### 配置选项列表 :

| 选项 | 需求 | 说明 |
| --- | --- | --- |
| 主机标识符 | 没有 |  **主机标识符**,应从 `host-`。要求,除非明确设置 `endpointBase` |
| 搜索键 | 没有 |  **公共搜索密钥**。它应该从 `search-`。 。 。 .  
  
注:这不是 _技术_ 在99%的案例中应该提供这种服务。 不提供此功能有一个小边例,主要用于内部的 App Search 用途,可以将此功能放入其中以杠杆化 App Search 的会话认证. |
| 引擎Name | 对 |  |
| 结束点Base | 没有 | 完全覆盖 App 搜索 API 端点的基础. 当代理 App 搜索 API 时有用,针对本地服务器,或自管理或云部署开发. 例如,“http://localhost:302” |
| 缓存回复 | 没有 | 是否应当缓存 API 响应 。 默认 : `true`。 。 。 . |
| 额外页眉 | 没有 | 在所有 API 请求中将转换为标题名和值的密钥和值对象 |

### API 方法

这个客户端是Elastic App Search API的稀薄接口. 请求和答复的更多细节可以是: 发现于 [文档](https://swiftype.com/documentation/app-search)。 。 。 .

#### 搜索中

对于查询术语 `lion`,搜索呼叫构建如下:

```javascript
var options = {
  search_fields: { title: {} },
  result_fields: { id: { raw: {} }, title: { raw: {} } }
};

client
  .search("lion", options)
  .then(resultList => {
    resultList.results.forEach(result => {
      console.log(`id: ${result.getRaw("id")} raw: ${result.getRaw("title")}`);
    });
  })
  .catch(error => {
    console.log(`error: ${error}`);
  });
```

请注意: `options` 支持在此列出的所有选项 : [https://swiftype.com/documentation/app-search/guides/search (中文(简体) )](https://swiftype.com/documentation/app-search/guides/search)。 。 。 .

除了上述得到支持的备选方案外,我们还支持以下领域:

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 断开功能 | 矩阵 \[标\] | 一组字段名称 。 这里列出的每个字段也必须作为字段的一面提供 `facet` 字段。 它表示一面应被视作相去不远. 在返回解开方位的计数时,计数将返回,仿佛该字段没有应用过滤器,即使使用了. |
| 解析功能分析 | 矩阵 \[标\] | 与《公约》一并使用 `disjunctiveFacets` 参数。 除非此处指定, 查询将在分析板上被标记为“ Facet- Only ” . |

_回应_

搜索方法返回包裹在 a 中的响应 `ResultList` 类型 :

```javascript
ResultList {
  rawResults: [], // List of raw `results` from JSON response
  rawInfo: { // Object wrapping the raw `meta` property from JSON response
    meta: {}
  },
  results: [ResultItem], // List of `results` wrapped in `ResultItem` type
  info: { // Currently the same as `rawInfo`
    meta: {}
  }
}

ResultItem {
  getRaw(fieldName), // Returns the HTML-unsafe raw value for a field, if it exists
  getSnippet(fieldName) // Returns the HTML-safe snippet value for a field, if it exists
}
```

#### 查询建议

```javascript
var options = {
  size: 3,
  types: {
    documents: {
      fields: ["name"]
    }
  }
};

client
  .querySuggestion("cat", options)
  .then(response => {
    response.results.documents.forEach(document => {
      console.log(document.suggestion);
    });
  })
  .catch(error => {
    console.log(`error: ${error}`);
  });
```

#### 多搜索

可以同时使用 `multiSearch` 方法.

寻找术语 `lion` 财务报告和审定财务报表 `tiger`,搜索呼叫构建如下:

```javascript
var options = {
  search_fields: { name: {} },
  result_fields: { id: { raw: {} }, title: { raw: {} } }
};

client
  .multiSearch([{ query: "node", options }, { query: "java", options }])
  .then(allResults => {
    allResults.forEach(resultList => {
      resultList.results.forEach(result => {
        console.log(
          `id: ${result.getRaw("id")} raw: ${result.getRaw("title")}`
        );
      });
    });
  })
  .catch(error => {
    console.log(`error: ${error}`);
  });
```

#### 点击通过跟踪

```javascript
client
  .click({
    query: "lion",
    documentId: "1234567",
    requestId: "8b55561954484f13d872728f849ffd22",
    tags: ["Animal"]
  })
  .catch(error => {
    console.log(`error: ${error}`);
  });
```

点击通道可以通过绑定跟踪 `client.click` 命令点击单个搜索结果链接上的事件.

以下例子说明如何通过注释与类和数据属性的链接来公开执行.

```javascript
document.addEventListener("click", function(e) {
  const el = e.target;
  if (!el.classList.contains("track-click")) return;

  client.click({
    query: el.getAttribute("data-query"),
    documentId: el.getAttribute("data-document-id"),
    requestId: el.getAttribute("data-request-id"),
    tags: [el.getAttribute("data-tag")]
  });
});
```

```html
<a
  href="/items/1234567"
  class="track-click"
  data-request-id="8b55561954484f13d872728f849ffd22"
  data-document-id="1234567"
  data-query="lion"
  data-tag="Animal"
>
  Item 1
</a>
```

## 正在测试

此工程的规格 [节点重放](https://github.com/assaf/node-replay) 以获取回应.

然后对照Jest的快照检查答复.

为了获取新的回复并更新快照,运行:

```
nvm use
REPLAY=record npm run test -u
```

进行测试:

```
nvm use
npm run test
```

## 发展

### 节点

您可能想要安装节点版本管理器, 如 nvm .

我们依赖于定义于 [.nvmrc (英语)](https://npmcn.com/package/%40elastic/app-search-javascript/.nvmrc)。 。 。 .

用 nvm 安装和使用正确的节点版本 :

```
nvm install
```

### Dev 服务器

安装依赖性 :

```bash
nvm use
npm install
```

在 `dist` 目录 :

```bash
# This will create files in the `dist` directory
npm run build
```

添加一个 `index.html` 转到您的文件 `dist` 目录

```html
<html>
  <head>
    <script src="elastic_app_search.umd.js"></script>
  </head>
  <body>

  </body>
</html>
```

运行 Dev 服务器 :

```bash
# This will serve files in the `dist` directory
npm run dev
```

导航到http://127.0.0.1:8080并通过浏览器Dev Console执行JavaScript命令.

### 构建

```
nvm use
npm run build
```

### 发布

```
nvm use
npm run publish
```

## 问 题

### 如果我需要写手术呢?

AppSearch 拥有一个第一当事方 [节点.js](https://github.com/elastic/app-search-node) 客户端支持像索引一样的写入操作.

### 我在哪里报告与客户的问题?

如果有东西不起作用,请打开 [问题](https://github.com/elastic/app-search-javascript/issues/new)。 。 。 .

### 哪里可以多学点App Search?

你最好的赌注是读 [文档](https://swiftype.com/documentation/app-search)。 。 。 .

### 我还能去哪找人帮忙?

你可以去检查 [弹性 App 搜索社区讨论论坛](https://discuss.elastic.co/c/app-search)。 。 。 .

## 贡献

我们欢迎该项目的捐助者。 在你开始前, 几个笔记...

-   在启动拉动请求前,请创建一个问题 [讨论您提案的范围](https://github.com/elastic/app-search-javascript/issues)。 。 。 .
-   请酌情写出简单的代码和简明文档.

## 许可证 QQ

[Apache 2.0 (英语)](https://github.com/elastic/app-search-javascript/blob/master/LICENSE.txt) © [弹性](https://github.com/elastic)

谢谢大家 [捐助者](https://github.com/elastic/app-search-javascript/graphs/contributors)来啊!

## 常见问题

### @elastic/app-search-javascript 是什么？

Elastic App 搜索 Api 的 Javascript 客户端

### 如何安装 @elastic/app-search-javascript？

在项目目录运行 npm install @elastic/app-search-javascript，或使用 pnpm add / yarn add / bun add。

### @elastic/app-search-javascript 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/elastic\_\_app-search-javascript 是否存在。

### @elastic/app-search-javascript 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @elastic/app-search-javascript 可以商用吗？

@elastic/app-search-javascript 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @elastic/app-search-javascript 的下载量有多少？

根据 npm 官方下载统计，@elastic/app-search-javascript 在 2026/10/13 – 2026/11/11 这 30 天内被下载 211,329 次，日均约 7,044 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @elastic/app-search-javascript 还在维护吗？

最新版本 8\.13\.0 发布于 2024/03/27（2 年前）；至今共发布 28 个版本，其中过去 12 个月发布了 0 个；首次发布于 2019/08/17。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

