Skip to main content

Method Signature

getVoiceSearchResults(
  query: string,
  parameters?: Omit<SearchParameters, 'filters' | 'sortBy' | 'sortOrder'>,
  networkParameters?: NetworkParameters
): Promise<SearchResponse>
Retrieve search results for natural language voice queries. This method uses Constructor.io’s natural language processing to interpret conversational queries like “show me lipstick” or “find red dresses under $50”.
Voice search does not support filters, sortBy, or sortOrder parameters as these are interpreted from the natural language query itself.

Parameters

query

query
string
required
The natural language search query. Can be conversational (e.g., “show me red dresses”, “find affordable laptops”).

parameters

parameters
object
Optional parameters to refine the result set. Note that filtering and sorting are handled through natural language processing.

networkParameters

networkParameters
object
Parameters relevant to the network request.

Response

Returns a Promise that resolves to a SearchResponse object. The response structure is identical to getSearchResults.
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.

Examples

const results = await constructorio.search.getVoiceSearchResults('show me lipstick');

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

Conversational Query with Price

const results = await constructorio.search.getVoiceSearchResults(
  'find red dresses under fifty dollars'
);

// Natural language processing automatically extracts:
// - Product type: dresses
// - Color filter: red
// - Price filter: < $50

Voice Search with Pagination

const results = await constructorio.search.getVoiceSearchResults(
  'show me affordable laptops',
  {
    page: 1,
    resultsPerPage: 20
  }
);

Voice Search with Section

const results = await constructorio.search.getVoiceSearchResults(
  'find running shoes for women',
  {
    section: 'Athletic Wear'
  }
);

Voice Search with Hidden Fields

const results = await constructorio.search.getVoiceSearchResults(
  'show me wireless headphones',
  {
    hiddenFields: ['inventory_count', 'supplier_id'],
    fmtOptions: {
      show_hidden_fields: true
    }
  }
);

// Access hidden fields in results
results.response.results.forEach(result => {
  console.log(`Stock: ${result.data.inventory_count}`);
});

Voice Search with Pre-Filter Expression

// Search within a specific collection using natural language
const results = await constructorio.search.getVoiceSearchResults(
  'show me blue shirts',
  {
    preFilterExpression: {
      name: 'collection',
      value: 'new-arrivals'
    }
  }
);

Voice Search with Variations Mapping

const results = await constructorio.search.getVoiceSearchResults(
  'find sneakers',
  {
    variationsMap: {
      group_by: [
        { name: 'style', field: 'style_id' }
      ],
      values: {
        price: { aggregation: 'min', field: 'price' },
        sizes: { aggregation: 'all', field: 'size' }
      },
      dtype: 'object'
    }
  }
);

Voice Search with Timeout

try {
  const results = await constructorio.search.getVoiceSearchResults(
    'show me smart watches',
    {},
    { timeout: 5000 }
  );
} catch (error) {
  console.error('Voice search timed out:', error);
}

Handling Redirects

const response = await constructorio.search.getVoiceSearchResults(
  'show me clearance items'
);

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

Natural Language Processing

Voice search automatically extracts and applies:
  • Product types - “show me dresses”
  • Colors - “red shoes”, “blue shirts”
  • Sizes - “medium t-shirts”, “size 10 shoes”
  • Price ranges - “under 50","between50", "between 20 and $100”
  • Materials - “cotton sweaters”, “leather jackets”
  • Brands - “Nike shoes”, “Apple products”
  • Qualifiers - “affordable”, “premium”, “best”

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.getVoiceSearchResults(
    'show me laptops'
  );
} catch (error) {
  if (error.message === 'query is a required parameter of type string') {
    console.error('Invalid query parameter');
  } else {
    console.error('Voice search failed:', error);
  }
}

Use Cases

Voice Assistants

// Integrate with voice assistant platforms
voiceAssistant.onSpeech(async (transcript) => {
  const results = await constructorio.search.getVoiceSearchResults(transcript);
  speakResults(results.response.results.slice(0, 3));
});

Mobile Apps

// Voice search button in mobile app
const handleVoiceSearch = async (voiceInput) => {
  const results = await constructorio.search.getVoiceSearchResults(voiceInput);
  
  if (results.response.total_num_results === 0) {
    showMessage('No results found. Try rephrasing your query.');
  } else {
    displayResults(results.response.results);
  }
};

Accessibility Features

// Voice search for users with accessibility needs
const enableVoiceSearch = () => {
  const recognition = new webkitSpeechRecognition();
  
  recognition.onresult = async (event) => {
    const transcript = event.results[0][0].transcript;
    const results = await constructorio.search.getVoiceSearchResults(transcript);
    announceResults(results.response.total_num_results);
    displayResults(results.response.results);
  };
  
  recognition.start();
};

Differences from getSearchResults

FeaturegetSearchResultsgetVoiceSearchResults
Query formatKeywordsNatural language
FiltersManual via filters paramExtracted from query
SortingManual via sortBy/sortOrderInterpreted from query
Use caseTraditional searchVoice interfaces
API endpoint/search/search/natural_language

See Also

Build docs developers (and LLMs) love