/algoliasearch-client-android

Algolia Search API Client for Android

Primary LanguageJavaMIT LicenseMIT

Algolia Search API Client for Android

Algolia Search is a hosted full-text, numerical, and faceted search engine capable of delivering realtime results from the first keystroke. The Algolia Search API Client for Android lets you easily use the Algolia Search REST API from your Android code.

Build Status GitHub version Note: If you were using version 2.x of our Android client, read the migration guide to version 3.x.

As a complement to this readme, you can browse the automatically generated reference documentation. (See also the offline-enabled version.)

This project is open-source under the MIT License.

Contributing

Your contributions are welcome! Please use our formatting configuration to keep the coding style consistent.

API Documentation

You can find the full reference on Algolia's website.

Table of Contents

  1. Contributing

  2. Install

  3. Quick Start

  4. Getting Help

Getting Started

Install

Add the following dependency to your Gradle build file:

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

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

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

Search

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

Note: 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