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
The search term to query for
Additional parameters to refine the result set
The page number of the results (Can’t be used together with offset)
The number of results to skip from the beginning (Can’t be used together with page)
The number of results per page to return
Key/value mapping of filters used to refine results
sortBy
string
default:"relevance"
The sort method for results
sortOrder
string
default:"descending"
The sort order for results
The section name for results
Format options used to refine result groups
Faceting expression to scope search results
Hidden metadata fields to return
The variations map object to aggregate variations
Specify whether results must match all, any or none of a given filter
Parameters relevant to the network request
Request timeout in milliseconds
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
The natural language search term to query for
Additional parameters to refine the result set
The page number of the results (Can’t be used together with offset)
The number of results to skip from the beginning (Can’t be used together with page)
The number of results per page to return
The section name for results
Format options used to refine result groups
Faceting expression to scope search results
The variations map object to aggregate variations
Hidden metadata fields to return
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);
}
});