Algolia is a hosted search engine and capable of delivering real-time results from the first keystroke. Algolia’s powerful API allows us quickly and seamlessly search within our websites and mobile applications.
For The Algolia search implementation in android, we have to add algolia dependency and sync the project. You can review it below:-
1 |
implementation 'com.algolia:algoliasearch-android:3.+' |
Firstly for Initializing Algolia search for fetching data and make search results faster, we can implement it with the following code.
1 2 3 4 5 6 7 8 9 |
val client = Client("LBXV17Z7K0", "71df9f1cc0676c0c2eca8016ef3355be") index = client.getIndex("live_2021default_products") completionHandler = object : CompletionHandler { override fun requestCompleted(content: JSONObject?, error: AlgoliaException?) { Log.d(TAG, "content-- "+content) } } |
For searching the query with the help of algolia, we have to pass query string. Please review the below code.
For searching the data from algolia dashboard, we have to pass the query as below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
etSearch?.addTextChangedListener(object : android.text.TextWatcher { override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { if (p0?.length!! >= 3) { index?.searchAsync(Query(p0), completionHandler) } } override fun afterTextChanged(p0: Editable?) { } }) |
Note:-
Most importantly, In order to get the all filter data, we have to pass “*”. You can go through the code below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
etSearch?.addTextChangedListener(object : android.text.TextWatcher { override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { if (p0?.length!! >= 3) { index?.searchAsync(Query(p0).setFacets("*"), completionHandler) } } override fun afterTextChanged(p0: Editable?) { } }) |
For filtering the data, we have to set Filter and facetsFilter. The first difference is that filters must be a string and facetFilters must be an array.
1 2 3 4 5 6 7 8 9 10 |
index.search('', { filters: 'author:"Stephen King"' }); index.search('', { facetFilters: [ "author:Stephen King" ] }); |
In conclusion, With the help of it, we can implement the algolia search in the android app and for more information, we are can go through the following link:-
https://www.algolia.com/doc/guides/getting-started/what-is-algolia/
https://www.algolia.com/doc/guides/managing-results/refine-results/filtering/in-depth/filters-and-facetfilters/
Finally, Hope you enjoy Algolia search implementation in android blog.