47

A Lightweight Android Client for ElasticSearch

 4 years ago
source link: https://www.tuicool.com/articles/n6zauqE
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

appbase-droid

The missing Android client for Elasticsearch and appbase.io.

Download

Download the latest JAR or configure this dependency:

Maven:

<dependency>
  <groupId>io.appbase</groupId>
  <artifactId>appbase-droid</artifactId>
  <version>1.1.0</version>
</dependency>

Gradle:

implementation("io.appbase:appbase-droid:1.1.0")

Snapshots of the development version are available in Sonatype's snapshots repository .

Overview

appbase-droid is an Elasticsearch client library usable from an Android device.

We built this as there are currently no good or maintained Elasticsearch libraries out there for Android.

Our design goals in building appbase-droid are:

  1. Provide a lightweight library for querying right from Android ( 1MB in size),
  2. Maintain compatibility with Java so it can also be used as a lightweight Java alternative ( 20x lighter than the official Elasticsearch client library),
  3. Only support methods related to document and query DSL, ideally your Elasticsearch hosting environment comes with a read-only access.
  4. Bonus: Support for streaming data queries when Elasticsearch is hosted on appbase.io app; build live charts, newsfeeds, streaming search.

Quick Start

  • Creating the client

    AppbaseClient client = new AppbaseClient(url, appname, username, password);

Note:If you are hosting with appbase.io the url should be " https://scalr.api.appbase.io ". The username should refer to the first part of the API credentials, and the password as the second part of it. You can get an idea on how to map the appname, username, password by viewing this GIF .

  • Index a document

    String result = client.prepareIndex(type, id, body)
      .execute()
      .body()
      .string();
    System.out.println(result);

    Sample Output:

    {
      "_index": "droid-test",
      "_type": "_doc",
      "_id": "xvvooe",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
      },
      "_seq_no": 18,
      "_primary_term": 3
    }
  • Update a document

    String result = client.prepareUpdate(type, id, parameters, doc)
      .execute()
      .body()
      .string();
    System.out.println(result);

    Sample Output:

    {
      "_index": "droid-test",
      "_type": "_doc",
      "_id": "eqsrxtmggk",
      "_version": 2,
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
      },
      "_seq_no": 26,
      "_primary_term": 3
    }
  • Delete a document

    String result = client.prepareDelete(type, id)
      .execute()
      .body()
      .string();

    Sample Output:

    {
      "_index": "droid-test",
      "_type": "_doc",
      "_id": "smemya",
      "_version": 3,
      "result": "deleted",
      "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
      },
      "_seq_no": 26,
      "_primary_term": 3
    }
  • Get a document

    String result = client.prepareGet(type, id)
      .execute()
      .body()
      .string();

    Sample Output:

    {
      "_index": "droid-test",
      "_type": "_doc",
      "_id": "nvyxcec",
      "found": false
    }
  • Make a search request

    String query =  "{ \"query\": { \"term\": { \"price\": \"100\" } } }";
    String result = client.prepareSearch(type, query)
      .execute()
      .body()
      .string();

    Sample Output:

    {
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1.0,
        "hits": [
          {
            "_index": "droid-test",
            "_type": "_doc",
            "_id": "bkxfsvddmo",
            "_score": 1.0,
            "_source": {
              "department_id": 1,
              "price": 5,
              "department_name": "Books",
              "name": "A Fake Book on Network Routing"
            }
          }
        ]
      }
    }

Documentation Reference

A brief primer on the supported methods.

See the online documentation reference .

Example

Checkout an example app built with appbase-droid .

How To Run Locally

Clone

git clone https://github.com/appbaseio/appbase-droid

Testing

mvn test

will test all the supported methods.

Developing

The codebase resides under the src/main/java/io/appbase path.

src/main/java/io/appbase/client
src/main/java/io/appbase/requestbuilders
src/main/java/io/appbase/interceptor
src/main/java/io/appbase/trial

Test files reside under src/test/java/test path.

  • src/test/java/test/AppbaseTest is the main test file that currently tests 7 methods. It is also a good place to see the library usage.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK