Quick Start with the Android API Client
On this page
Install
It’s recommended to use the Kotlin API client, which is better suited for Android development.
Install the Android client by adding the following dependency to your Gradle
build file:
1
2
3
4
5
dependencies {
// [...]
implementation 'com.algolia:algoliasearch-android:3.+'
// This will automatically update to the latest v3 release when you build your project
}
Quick Start
In 30 seconds, this quick start tutorial will show you how to index and search objects.
Initialize the client
To start, you need to initialize the client. To do this, you need your Application ID and API Key. You can find both on your Algolia account.
1
2
Client client = new Client("YourApplicationID", "YourAdminAPIKey");
Index index = client.getIndex("your_index_name");
The API key displayed here is your Admin API key. To maintain security, never use your Admin API key on your front end, nor share it with anyone. In your front end, use the search-only API key or any other key that has search-only rights.
If you are building a native app on mobile, make sure not to include the search API key directly in the source code. You should instead consider fetching the key from your servers during the app’s startup.
Push data
Without any prior configuration, you can start indexing contacts in the contacts
index using the following code:
1
2
3
4
5
6
7
8
9
10
11
Index index = client.initIndex("contacts");
index.addObjectAsync(new JSONObject()
.put("firstname", "Jimmie")
.put("lastname", "Barninger")
.put("followers", 93)
.put("company", "California Paint"), null);
index.addObjectAsync(new JSONObject()
.put("firstname", "Warren")
.put("lastname", "Speach")
.put("followers", 42)
.put("company", "Norwalk Crmc"), null);
Configure
You can customize settings to fine tune the search behavior. For example, you can add a custom ranking by number of followers to further enhance the built-in relevance:
1
2
JSONObject settings = new JSONObject().put("customRanking", "desc(followers)");
index.setSettingsAsync(settings, null);
You can also configure the list of attributes you want to index by order of importance (most important first).
Algolia is designed to suggest results as you type, which means you’ll generally search by prefix. In this case, the order of attributes is crucial to decide which hit is the best.
1
2
3
4
5
JSONObject settings = new JSONObject()
.put("searchableAttributes", "lastname")
.put("searchableAttributes", "firstname")
.put("searchableAttributes", "company");
index.setSettingsAsync(settings, null);
Search
You can now search for contacts by firstname
, lastname
, company
, etc. (even with typos):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
CompletionHandler completionHandler = new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
// [...]
}
};
// Search for a first name
index.searchAsync(new Query("jimmie"), completionHandler);
// Search for a first name with typo
index.searchAsync(new Query("jimie"), completionHandler);
// Search for a company
index.searchAsync(new Query("california paint"), completionHandler);
// Search for a first name and a company
index.searchAsync(new Query("jimmie paint"), completionHandler);