Skip to main content
The Search module provides methods to retrieve search results and voice search results from Constructor.io API.

Getting Started

The search module is available on your Constructor.io client instance:
const ConstructorioClient = require('@constructor-io/constructorio-client-javascript');

const constructorio = new ConstructorioClient({
  apiKey: 'YOUR_API_KEY',
});

// Access the search module
constructorio.search.getSearchResults('query');

Methods

getSearchResults

Retrieve search results based on a search query.
constructorio.search.getSearchResults('t-shirt')
  .then((response) => {
    console.log(response.response.results);
  });

Parameters

query
string
required
The search term to query for
parameters
object
Additional parameters to refine the result set
networkParameters
object
Parameters relevant to the network request

Response Structure

{
  "response": {
    "results": [
      {
        "matched_terms": ["t-shirt"],
        "data": {
          "id": "shirt-123",
          "title": "Blue T-Shirt",
          "url": "/products/shirt-123"
        },
        "result_id": "abc123-xyz789"
      }
    ],
    "facets": [...],
    "groups": [...],
    "total_num_results": 150
  },
  "result_id": "abc123-xyz789",
  "request": {...}
}
Each result item includes a result_id field that should be used when tracking user interactions.

getVoiceSearchResults

Retrieve results for natural language voice search queries.
constructorio.search.getVoiceSearchResults('show me lipstick')
  .then((response) => {
    console.log(response.response.results);
  });

Parameters

query
string
required
The natural language search term to query for
parameters
object
Additional parameters to refine the result set

Events

The search module dispatches custom events on the browser window when requests complete:
window.addEventListener('cio.client.search.getSearchResults.completed', (event) => {
  // event.detail contains the response data
  console.log(event.detail);
}, false);

window.addEventListener('cio.client.search.getVoiceSearchResults.completed', (event) => {
  console.log(event.detail);
}, false);

Redirect Rules

Search results may include redirect rules. Check for the redirect property:
constructorio.search.getSearchResults('special-sale')
  .then((response) => {
    if (response.response.redirect) {
      // Handle redirect
      window.location.href = response.response.redirect.url;
    } else {
      // Display results
      displayResults(response.response.results);
    }
  });

Build docs developers (and LLMs) love