@craftercms/search

中文文档
4.3.0发布于 2025/02/21GNU LGPL 3.0TypeScriptESM + CJS近 30 天下载 2869

Crafter CMS search service and associated tools

原始使用文档
文档目录5 节
  1. Usage
  2. Via npm
  3. Via html script imports
  4. Package Index
  5. search

npm (scoped)

@craftercms/search

This package contains tools for integrating your application with Crafter Search.

Usage

All of Crafter CMS packages can be used either via npm or in plain html/javascript via regular script imports.

Via npm

  • Install module using yarn or npm
    • yarn add @craftercms/search or
    • npm install @craftercms/search
  • Import and use the service(s) you need

Via html script imports

  • Download the bundle and import them in your page.
  • The bundles declare a global variable named craftercms. You can access all craftercms' packages and functions under this root.
  • The search package depends on rxjs, @craftercms/utils, @craftercms/classes; make sure to import those too before the search script.

Tip: Once you've imported the scripts, type craftercms on your browser's dev tools console to inspect the package(s)

Vanilla html/js example

<script src="https://unpkg.com/rxjs"></script>
<script src="https://unpkg.com/@craftercms/utils"></script>
<script src="https://unpkg.com/@craftercms/classes"></script>
<script src="https://unpkg.com/@craftercms/content"></script>
<script src="https://unpkg.com/@craftercms/search"></script>
<script>
 (function ({ search, content }, { operators }) {

   const { map } = operators;
   const { search, createQuery } = search;
   const { parseDescriptor, preParseSearchResults } = content;

   search(
     createQuery({
       query: {
         bool: {
           filter: [/*...*/]
         }
       }
     }),
     { baseUrl: 'http://localhost:8080', site: 'editorial' }
   ).pipe(
     map(({ hits, ...rest }) => ({
       ...rest,
       hits: hits.map(({ _source }) => parseDescriptor(
         preParseSearchResults(_source)
       ))
     }))
   ).subscribe((results) => {
     // Do stuff with results...
   });

 })(craftercms, rxjs);
</script>

Package Index

The examples below assume usage in the style of using via npm. If you're using the bundles, directly importing as a script in the browser, these functions will be under the global variable named craftercms.search (i.e. window.craftercms.search).

Returns the result for a given query.

search(query: Query)

Parameters
queryThe query object
configCrafter configuration. Optional. Default value in here.

Returns

Map model

Examples

  • Connect to Crafter Search to query for content with ELASTIC SEARCH:
  import { crafterConf } from '@craftercms/classes';
  import { search, createQuery } from '@craftercms/search';
  import { map } from 'rxjs/operators';
  import { parseDescriptor, preParseSearchResults } from '@craftercms/content';

  // First, set the Crafter configuration to cache your config. 
  // All subsequent calls to `getConfig` will use that configuration.
  crafterConf.configure({
    baseUrl: 'http://localhost:8080',
    site: 'wordify'
  });

  const query = 'lorem';
  const fields = ['headline_s', 'blurb_t'];
  const contentTypes = ['/page/post', '/component/post'];
  search(
    createQuery({
      query: {
        'bool': {
          'filter': [
            { 'bool': { 'should': contentTypes.map(id => ({ 'match': { 'content-type': id } })) } },
            { 'multi_match': { 'query': query, 'fields': fields } }
          ]
        }
      }
    }),
    // If you didn't pre-configure, you may send config values as second param here
    // { baseUrl: 'http://localhost:8080', site: 'wordify' }
  ).pipe(
    map(({ hits, ...rest }) => {
      return { 
        ...rest, 
        hits: hits.map(({ _source }) => parseDescriptor(
          preParseSearchResults(_source)
        )) 
      };
    })
  ).subscribe((results) => {
    console.log(results);
  });

You may use a different config by supplying the config object at the service call invoking time.

  import { search, createQuery } from '@craftercms/search';

  //Create query
  const query = createQuery({
    "query" : {
        "match_all" : {}
    }
  });

  search(query, { baseUrl: 'http://localhost:8080', site: 'editorial' }).subscribe((results) => {
    // ...
  });

版本与发布记录

@craftercms/search 共发布 35 个版本,首次发布于 2018/07/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.3.0latest2025/02/211.7 年前
4.2.22025/01/211.8 年前
4.2.12024/12/161.9 年前
4.1.9support-4.1.x2024/11/262 年前
4.2.02024/10/242 年前
4.1.82024/09/132 年前

关于 @craftercms/search 的常见问题

@craftercms/search 是什么?

Crafter CMS search service and associated tools

如何安装 @craftercms/search?

在项目目录运行 npm install @craftercms/search,或使用 pnpm add / yarn add / bun add。

@craftercms/search 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@craftercms/search 支持 ESM 还是 CommonJS?

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

@craftercms/search 可以商用吗?

@craftercms/search 使用 GNU LGPL 3.0 许可。声明的许可为 GNU LGPL 3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@craftercms/search 的下载量有多少?

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

@craftercms/search 还在维护吗?

最新版本 4.3.0 发布于 2025/02/21(1.7 年前);至今共发布 35 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/07/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。