Skip to main content

Method Signature

getSearchResults(
  query: string,
  parameters?: SearchParameters,
  networkParameters?: NetworkParameters
): Promise<SearchResponse>
Retrieve search results for a text query with support for filtering, sorting, pagination, and faceting.

Parameters

query

query
string
required
The search term to query. Must be a non-empty string.

parameters

parameters
object
Optional parameters to refine the result set.

networkParameters

networkParameters
object
Parameters relevant to the network request.

Response

Returns a Promise that resolves to a SearchResponse object.
request
object
The processed request parameters sent to the API.
response
object
The search results and metadata, or redirect information.
result_id
string
Unique identifier for this search request. Used for tracking and analytics. This is automatically appended to each result item in the results array.

Examples

const results = await constructorio.search.getSearchResults('laptop');

console.log(results.response.total_num_results);
console.log(results.response.results);

Search with Pagination

const results = await constructorio.search.getSearchResults('shoes', {
  page: 2,
  resultsPerPage: 24
});

Search with Filters

const results = await constructorio.search.getSearchResults('t-shirt', {
  resultsPerPage: 40,
  filters: {
    size: 'medium',
    color: ['red', 'blue']
  },
  filterMatchTypes: {
    size: 'all',
    color: 'any'
  }
});

Search with Sorting

const results = await constructorio.search.getSearchResults('laptop', {
  sortBy: 'price',
  sortOrder: 'ascending',
  filters: {
    brand: 'Apple'
  }
});

Search with Variations Mapping

const results = await constructorio.search.getSearchResults('shoes', {
  variationsMap: {
    group_by: [
      { name: 'style', field: 'style_id' }
    ],
    values: {
      price: { aggregation: 'min', field: 'price' },
      color: { aggregation: 'all', field: 'color' }
    },
    dtype: 'object'
  }
});

Search with Pre-Filter Expression

// Only search within a specific collection
const results = await constructorio.search.getSearchResults('dress', {
  preFilterExpression: {
    and: [
      { name: 'collection', value: 'summer-2024' },
      { name: 'price', range: [0, 100] }
    ]
  }
});

Search with Hidden Fields

const results = await constructorio.search.getSearchResults('phone', {
  hiddenFields: ['internal_id', 'warehouse_location'],
  hiddenFacets: ['supplier']
});

// Access hidden fields in results
results.response.results.forEach(result => {
  console.log(result.data.internal_id);
});

Search with Timeout

try {
  const results = await constructorio.search.getSearchResults('laptop', {}, {
    timeout: 5000 // 5 seconds
  });
} catch (error) {
  console.error('Search timed out:', error);
}

Handling Redirects

const response = await constructorio.search.getSearchResults('sale');

if (response.response.redirect) {
  // Redirect rule was triggered
  window.location.href = response.response.redirect.data.url;
} else {
  // Normal search results
  displayResults(response.response.results);
}

Error Handling

The method throws an error if:
  • query is not provided or is not a string
  • Network request fails
  • Response data is malformed
try {
  const results = await constructorio.search.getSearchResults('laptop');
} catch (error) {
  if (error.message === 'query is a required parameter of type string') {
    console.error('Invalid query parameter');
  } else {
    console.error('Search failed:', error);
  }
}

See Also

Build docs developers (and LLMs) love