Android

Getting Started

Install

Add the following dependency to your Gradle build file:

dependencies {
    // [...]
    compile 'com.algolia:algoliasearch-android:3.10.1'
}

Quick Start

In 30 seconds, this quick start tutorial will show you how to index and search objects.

Initialize the client

You first need to initialize the client. For that you need your Application ID and API Key. You can find both of them on your Algolia account.

Client client = new Client("YOUR_APP_ID", "YOUR_API_KEY");

The api key that is displayed here is your ADMIN API Key, never use it in your frontend nor give it to anyone.

In your frontend, you should use the SEARCH ONLY API Key or any other key that has search rights only

Push data

Without any prior configuration, you can start indexing contacts in the contacts index using the following code:

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);

You can now search for contacts using firstname, lastname, company, etc. (even with typos):

CompletionHandler completionHandler = new CompletionHandler() {
    @Override
    public void requestCompleted(JSONObject content, AlgoliaException error) {
        // [...]
    }
};
// search by firstname
index.searchAsync(new Query("jimmie"), completionHandler);
// search a firstname with typo
index.searchAsync(new Query("jimie"), completionHandler);
// search for a company
index.searchAsync(new Query("california paint"), completionHandler);
// search for a firstname & company
index.searchAsync(new Query("jimmie paint"), completionHandler);

Configure

Settings can be customized to tune the search behavior. For example, you can add a custom sort by number of followers to the already great built-in relevance:

JSONObject settings = new JSONObject().append("customRanking", "desc(followers)");
index.setSettingsAsync(settings, null);

You can also configure the list of attributes you want to index by order of importance (first = most important):

Since the engine is designed to suggest results as you type, you’ll generally search by prefix. In this case the order of attributes is very important to decide which hit is the best:

JSONObject settings = new JSONObject()
    .append("searchableAttributes", "lastname")
    .append("searchableAttributes", "firstname")
    .append("searchableAttributes", "company");
index.setSettingsAsync(settings, null);

Getting Help

Did you find this page helpful?

We're always looking for advice to help improve our documentation! Please let us know what's working (or what's not!) - we're constantly iterating thanks to the feedback we receive.

Send us your suggestions!