Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ElasticSearch API in Android

Can anyone explain how can I use the ElasticSearch API in Android. Does anyone successfully integrated the api in android?

I added following dependencies in Gradle:

compile 'org.elasticsearch.client:transport:5.2.1'

Of course I run into issues:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE File1: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpcore-nio\4.4.5\f4be009e7505f6ceddf21e7960c759f413f15056\httpcore-nio-4.4.5.jar File2: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpasyncclient\4.1.2\95aa3e6fb520191a0970a73cf09f62948ee614be\httpasyncclient-4.1.2.jar File3: C:\Users\dude.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.dataformat\jackson-dataformat-yaml\2.8.6\8bd44d50f9a6cdff9c7578ea39d524eb519e35ab\jackson-dataformat-yaml-2.8.6.jar File4: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpcore\4.4.5\e7501a1b34325abb00d17dde96150604a0658b54\httpcore-4.4.5.jar File5: C:\Users\dude.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.8.6\2ef7b1cc34de149600f5e75bc2d5bf40de894e60\jackson-core-2.8.6.jar

.

UPDATE 1:

Well I have to use the REST API using Android Asynchronous Http Client, because adding packagingOptions does not solve the issue

like image 893
lidox Avatar asked Feb 22 '17 09:02

lidox


People also ask

Does Elasticsearch have an API?

One of the great things about Elasticsearch is its extensive REST API which allows you to integrate, manage and query the indexed data in countless different ways. Examples of using this API to integrate with Elasticsearch are abundant, spanning different companies and use cases.

How do you query an Elasticsearch from the postman?

Once ElasticSearch has started, you can use any REST API client such as postman or fiddler. The first part (localhost) denotes the host (server) where your ElasticSearch is hosted, and the default port is 9200. The second part (company) is index , followed by the (employee) type name, followed by (_search) action.

What is Elasticsearch client used for?

Elasticsearch allows you to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds. It's able to achieve fast search responses because instead of searching the text directly, it searches an index.

What is Elasticsearch client?

Elasticsearch is a distributed search and analytics engine built on Apache Lucene. Since its release in 2010, Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.


2 Answers

Ok so I found out how to access a REST API from Android using a library. Check out more details on Android Asynchronous Http Client and Github.

First add the permission into the manifest

<uses-permission android:name="android.permission.INTERNET" />

In gradle add:

compile 'com.loopj.android:android-async-http:1.4.9'

Now you can start implementing the REST API like this:

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class ElasticRestClient {

    private static final String BASE_URL = "http://httpbin.org/"; //http://localhost:9200/
    private static final String CLASS_NAME = ElasticRestClient.class.getSimpleName();

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

    public void getHttpRequest() {
        try {


            ElasticRestClient.get("get", null, new JsonHttpResponseHandler() { // instead of 'get' use twitter/tweet/1
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    // If the response is JSONObject instead of expected JSONArray
                    Log.i(CLASS_NAME, "onSuccess: " + response.toString());
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    Log.i(CLASS_NAME, "onSuccess: " + response.toString());
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Log.e(CLASS_NAME, "onFailure");
                    // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                }

                @Override
                public void onRetry(int retryNo) {
                    Log.i(CLASS_NAME, "onRetry " + retryNo);
                    // called when request is retried
                }
            });
        }
        catch (Exception e){
            Log.e(CLASS_NAME, e.getLocalizedMessage());
        }
    }
}
like image 200
lidox Avatar answered Oct 18 '22 07:10

lidox


I faced exactly the same issue when I tried to add the ES rest client in gradle:

compile 'org.elasticsearch.client:rest:5.4.0'

I finally resolved it by adding these lines at the top of my build.gradle file:

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

The lines above simply remove all duplicated dependencies. And after doing it, you can refer to the ES documentation for querying your data.

like image 3
Maximilien Belinga Avatar answered Oct 18 '22 06:10

Maximilien Belinga