Learn how to build Voice search experience with InstantSearch Android and Voice Overlay.
This guide explains how to build step by step a voice search experience using the libraries provided by Algolia.
You’ll build an Android app with a classic search box and a button that triggers the voice input.
To create this app, you’ll use the InstantSearch and Voice overlay libraries.Building a voice search experience has three steps:
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:
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 removeStopWords and ensure to select a supported language. For example, en for 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 . 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 ignorePlurals to true and ensure to select a supported language. For example, en for English.
This setting marks words like “car” and “cars” as matching terms.
Apply analyticsTags to the query, including voice queries.
You can activate these settings using the naturalLanguages parameter. 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.
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.
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.
Start by creating a new Android project.
Open Android Studio, and select Create New Project.In the Select a Project Template window, select Empty Activity and click Next.Input the name of your app. Select API 21 as minimum SDK version. Click Finish.Build and run your app.
You should see your app with blank screen.
Start by creating a classic search interface with search box and results list.
First, create a layout file called list_item.xml under res/layout/.
Add a TextView to display a hit:
Add a basic implementation of VoiceOverlay:In the click listener, check if you have the audio permission and show the appropriate dialog:
Kotlin
Report incorrect code
Copy
val microphone = findViewById<View>(R.id.microphone)microphone.setOnClickListener { when (isRecordAudioPermissionGranted()) { true -> VoiceInputDialogFragment().show(supportFragmentManager, "INPUT") false -> VoicePermissionDialogFragment().show(supportFragmentManager, "PERMISSION") }}
Once the user speaks, you get their input back by implementing VoiceSpeechRecognizer.ResultsListener
Kotlin
Report incorrect code
Copy
class MainActivity : AppCompatActivity(), VoiceSpeechRecognizer.ResultsListener { //... override fun onResults(possibleTexts: Array<out String>) { val searchView = findViewById<SearchView>(R.id.searchView) possibleTexts.firstOrNull()?.let { input -> searchView.setQuery(input, true) } }}
In the end, the code of your MainActivity should look as follows:
Kotlin
Report incorrect code
Copy
class MainActivity : AppCompatActivity(), VoiceSpeechRecognizer.ResultsListener { val searcher = HitsSearcher( applicationID = "latency", apiKey = "1f6fd3a6fb973cb08419fe7d288fa4db", indexName = "bestbuy" ) val searchBox = SearchBoxConnector(searcher, searchMode = SearchMode.AsYouType) val connection = ConnectionHandler(searchBox) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Setup SearchBox widget val searchView = findViewById<SearchView>(R.id.searchView) val searchBoxView = SearchBoxViewAppCompat(searchView) connection += searchBox.connectView(searchBoxView) // Setup Hits widget val hits = findViewById<RecyclerView>(R.id.hits) val productAdapter = ProductAdapter() hits.adapter = productAdapter connection += searcher.connectHitsView(productAdapter) { response -> response.hits.deserialize(Product.serializer()) } // Setup VoiceOverlay val microphone = findViewById<View>(R.id.microphone) microphone.setOnClickListener { when (isRecordAudioPermissionGranted()) { true -> VoiceInputDialogFragment().show(supportFragmentManager, "INPUT") false -> VoicePermissionDialogFragment().show(supportFragmentManager, "PERMISSION") } } // Initial search searcher.searchAsync() } override fun onResults(possibleTexts: Array<out String>) { val searchView = findViewById<SearchView>(R.id.searchView) possibleTexts.firstOrNull()?.let { input -> searchView.setQuery(input, true) } } override fun onDestroy() { super.onDestroy() searcher.cancel() connection.clear() }}
Build and run your app to test your voice search. You should see the voice input button on the right of the search box.The 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.
Once you give all the authorizations, the voice input interface appears. Try to say something and get the instant search results.
You can find a complete VoiceOverlay implementation on the git repository.
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 modifying InstantSearch components, as well as the VoiceOverlay components.