Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@craftercms/search

Package Overview
Dependencies
Maintainers
3
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@craftercms/search

Crafter CMS search service and associated tools

  • 4.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

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) => {
    // ...
  });

Keywords

FAQs

Package last updated on 08 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc