Search in sources :

Example 1 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class HttpBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    ActionResponse response = null;
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            gson.fromJson(query, Map.class);
        } catch (final JsonParseException e) {
            // This is not a JSON (or maybe not well formatted...)
            query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
        }
    }
    try {
        final HttpRequestWithBody request = Unirest.post(getUrl(indices, types) + "/_search?size=" + size).header("Content-Type", "application/json");
        if (StringUtils.isNoneEmpty(query)) {
            request.header("Accept", "application/json").body(query);
        }
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<JsonNode> result = request.asJson();
        final JSONObject body = result.getBody() != null ? result.getBody().getObject() : null;
        if (isSucceeded(result)) {
            final long total = getFieldAsLong(result, "hits/total");
            response = new ActionResponse().succeeded(true).totalHits(total);
            if (containsAggs(result)) {
                JSONObject aggregationsMap = body.getJSONObject("aggregations");
                if (aggregationsMap == null) {
                    aggregationsMap = body.getJSONObject("aggs");
                }
                for (final String key : aggregationsMap.keySet()) {
                    final JSONObject aggResult = aggregationsMap.getJSONObject(key);
                    if (aggResult.has("buckets")) {
                        // Multi-bucket aggregations
                        final Iterator<Object> buckets = aggResult.getJSONArray("buckets").iterator();
                        while (buckets.hasNext()) {
                            response.addAggregation(new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
                        }
                    } else {
                        response.addAggregation(new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
                    }
                    // Keep only one aggregation
                    break;
                }
            } else if (size > 0 && total > 0) {
                final JSONArray hits = getFieldAsArray(body, "hits/hits");
                final Iterator<Object> iter = hits.iterator();
                while (iter.hasNext()) {
                    final JSONObject hit = (JSONObject) iter.next();
                    final Object data = hit.opt("_source") != null ? hit.opt("_source") : hit.opt("fields");
                    response.addHit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), data.toString()));
                }
            }
        } else {
            throw new ActionException(body.get("error").toString());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : JSONArray(org.json.JSONArray) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) JsonParseException(com.google.gson.JsonParseException) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject)

Example 2 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class HttpBasedClient method delete.

@Override
public ActionResponse delete(String index, String type, String id) {
    ActionResponse response = null;
    try {
        final HttpRequest request = Unirest.delete(getUrl(index, type, id, true));
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<String> result = request.asString();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            final JsonNode body = new JsonNode(result.getBody());
            response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), null));
        } else {
            throw new ActionException(result.getBody());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 3 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class HttpBasedClient method get.

@Override
public ActionResponse get(String index, String type, String id) {
    ActionResponse response = null;
    try {
        final HttpRequest request = Unirest.get(getUrl(index, type, id, true));
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<String> result = request.asString();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            final JsonNode body = new JsonNode(result.getBody());
            if (body.getObject().has("_index")) {
                response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), getFieldAsString(body, "_source")));
            } else {
                final JSONArray hits = getFieldAsArray(body.getObject(), "hits/hits");
                final JSONObject hit = (JSONObject) hits.iterator().next();
                response = new ActionResponse().succeeded(true).hit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), hit.opt("_source").toString()));
            }
        } else {
            if (result.getStatus() == 404) {
                response = new ActionResponse().succeeded(false);
            } else {
                throw new ActionException(result.getBody());
            }
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 4 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class ElasticsearchInterpreter method buildSearchHitsResponseMessage.

private String buildSearchHitsResponseMessage(ActionResponse response) {
    if (response.getHits() == null || response.getHits().size() == 0) {
        return "";
    }
    //First : get all the keys in order to build an ordered list of the values for each hit
    //
    final List<Map<String, Object>> flattenHits = new LinkedList<>();
    final Set<String> keys = new TreeSet<>();
    for (final HitWrapper hit : response.getHits()) {
        final String json = hit.getSourceAsString();
        final Map<String, Object> flattenJsonMap = JsonFlattener.flattenAsMap(json);
        final Map<String, Object> flattenMap = new HashMap<>();
        for (final Iterator<String> iter = flattenJsonMap.keySet().iterator(); iter.hasNext(); ) {
            // Replace keys that match a format like that : [\"keyname\"][0]
            final String fieldName = iter.next();
            final Matcher fieldNameMatcher = FIELD_NAME_PATTERN.matcher(fieldName);
            if (fieldNameMatcher.matches()) {
                flattenMap.put(fieldNameMatcher.group(1) + fieldNameMatcher.group(2), flattenJsonMap.get(fieldName));
            } else {
                flattenMap.put(fieldName, flattenJsonMap.get(fieldName));
            }
        }
        flattenHits.add(flattenMap);
        for (final String key : flattenMap.keySet()) {
            keys.add(key);
        }
    }
    // Next : build the header of the table
    //
    final StringBuffer buffer = new StringBuffer();
    for (final String key : keys) {
        buffer.append(key).append('\t');
    }
    buffer.replace(buffer.lastIndexOf("\t"), buffer.lastIndexOf("\t") + 1, "\n");
    //
    for (final Map<String, Object> hit : flattenHits) {
        for (final String key : keys) {
            final Object val = hit.get(key);
            if (val != null) {
                buffer.append(val);
            }
            buffer.append('\t');
        }
        buffer.replace(buffer.lastIndexOf("\t"), buffer.lastIndexOf("\t") + 1, "\n");
    }
    return buffer.toString();
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) LinkedList(java.util.LinkedList) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) TreeSet(java.util.TreeSet) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with HitWrapper

use of org.apache.zeppelin.elasticsearch.action.HitWrapper in project zeppelin by apache.

the class HttpBasedClient method index.

@Override
public ActionResponse index(String index, String type, String id, String data) {
    ActionResponse response = null;
    try {
        HttpRequestWithBody request = null;
        if (StringUtils.isEmpty(id)) {
            request = Unirest.post(getUrl(index, type, id, false));
        } else {
            request = Unirest.put(getUrl(index, type, id, false));
        }
        request.header("Accept", "application/json").header("Content-Type", "application/json").body(data).getHttpRequest();
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<JsonNode> result = request.asJson();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(result, "_index"), getFieldAsString(result, "_type"), getFieldAsString(result, "_id"), null));
        } else {
            throw new ActionException(result.getBody().toString());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Aggregations

HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)6 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)5 JsonNode (com.mashape.unirest.http.JsonNode)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)4 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Matcher (java.util.regex.Matcher)1 AggWrapper (org.apache.zeppelin.elasticsearch.action.AggWrapper)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1