- Input using speech-to-text
- Processing using Algolia
- Output using speech synthesis
Speech-to-text input layer
You must have a speech-to-text layer to convert your users’ speech into something Algolia understands (Algolia can’t process non-textual searches). You can add a speech-to-text layer in two ways:- Using the Chrome browser, iOS or Android native apps, or a voice platform tool like Alexa or Google Assistant with speech-to-text built-in.
- Using a third-party service. You send user speech to the service. When you receive it back, you then send it to Algolia as a search query. Some services include:
Process the query with Algolia
After converting speech to text, you need to process the query and return relevant results from your Algolia index. This step involves:- Query time settings
- Index configuration
Query time settings
The query time settings improve search results during query time. For instance, selecting a language for Algolia let you set certain features like ignoring “noise” words that users could enter in their search query. If you choose English as the language, and you turn on the stop words feature, the search engine ignores words like ‘a’ and ‘an’ as they’re not relevant to the search query. This gives more exact search results.- Set
removeStopWordsand ensure to select a supported language. For example,enfor English. This setting removes stop words like “a”, “an”, or “the” before running the search query. - Send the entire query string along as
optionalWords. Speech often has words that aren’t in any of your records. With this setting, records don’t need to match all the words. Records matching more words rank higher. For example, in the spoken query “Show me all blue dresses”, only “blue dresses” may yield results for a clothing store: the other words should be optional. - Set
ignorePluralstotrueand ensure to select a supported language. For example,enfor English. This setting marks words like “car” and “cars” as matching terms. - Apply
analyticsTagsto the query, including voice queries. You can activate these settings using thenaturalLanguagesparameter. These settings work well together when the query format is in natural language instead of keywords, for example, when your user performs a voice search.
Index configuration
Similarly, you can apply some rules related to your index. These rules are dynamic and apply depending on what users type in the search query. Detecting user intent can help dynamically change the search results.Speech synthesis output
Not all voice platforms need speech synthesis or text-to-speech. For example, a site that shows search results may be enough. If your voice platform does need speech synthesis, your options are:- A built-in system such as Alexa or Google Assistant.
- A third-party system. Most modern browsers support speech synthesis through the SpeechSynthesis API. If you want a wider choice of voices, you have Azure Cognitive Services or Amazon Web Services’ Polly.
Prepare your project
To use InstantSearch Android, you need an Algolia account. You can create a new account, or use the following credentials:- Application ID:
latency - Search API Key:
1f6fd3a6fb973cb08419fe7d288fa4db - Index name:
instant_search
Create a new Android project
Start by creating a new Android project. Open Android Studio, and select Create New Project.

API 21 as minimum SDK version. Click Finish.


Add project dependencies
In yourbuild.gradle file at the project level, add mavenCentral() as a dependency repository in the repositories blocks:
groovy
build.gradle file under app module, add the following in the dependencies block:
groovy
AndroidManifest.xml must include the following permissions:
XML
build.gradle:
groovy
plugins block to your app’s build.gradle:
gradle
Create a basic search experience
Start by creating a classic search interface with search bar and results list. First, create a layout file calledlist_item.xml under res/layout/.
Add a TextView to display a hit:
XML
activity_main.xml to have a minimal set of components for a basic search experience:
SearchView: view for textual query inputRecyclerView: the list of search results
XML
Product to hold search results:
Kotlin
ProductAdapter to render search results:
Kotlin
MainActivity use the SearchBox and Hits widgets:
Kotlin

Create a voice search experience
To integrate voice search:1
Setup audio permissions
Add
RECORD_AUDIO permission to AndroidManifest.xml file:XML
2
Add voice input UI
Change
activity_main.xml to add the new voice input button to the existing UI.
The updated layout file should look as follows:XML
3
Add voice input logic
Add a basic implementation of VoiceOverlay:In the click listener, check if you have the audio permission and show the appropriate dialog:Once the user speaks, you get their input back by implementing
Kotlin
VoiceSpeechRecognizer.ResultsListenerKotlin
MainActivity should look as follows:
Kotlin

VoiceOverlay should appear when you tap the voice input button.
At the first launch, it asks for the permissions mentioned in the setup audio permission section.




VoiceOverlay implementation on the git repository.
Conclusion
With Algolia’s libraries, you can build a voice search experience in less than a hundred lines of code. You can customize your search experience and make it unique by modifyingInstantSearch components, as well as the VoiceOverlay components.