Search in sources :

Example 1 with AggWrapper

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

the class ElasticsearchInterpreter method buildAggResponseMessage.

private InterpreterResult buildAggResponseMessage(List<AggWrapper> aggregations) {
    final InterpreterResult.Type resType = InterpreterResult.Type.TABLE;
    String resMsg = "";
    final Set<String> headerKeys = new HashSet<>();
    final List<Map<String, Object>> buckets = new LinkedList<>();
    for (final AggWrapper aggregation : aggregations) {
        final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(aggregation.getResult());
        headerKeys.addAll(bucketMap.keySet());
        buckets.add(bucketMap);
    }
    final StringBuffer buffer = new StringBuffer();
    final String[] keys = headerKeys.toArray(new String[0]);
    for (final String key : keys) {
        buffer.append("\t" + key);
    }
    buffer.deleteCharAt(0);
    for (final Map<String, Object> bucket : buckets) {
        buffer.append("\n");
        for (final String key : keys) {
            buffer.append(bucket.get(key)).append("\t");
        }
        buffer.deleteCharAt(buffer.length() - 1);
    }
    resMsg = buffer.toString();
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, resType, resMsg);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) LinkedList(java.util.LinkedList) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with AggWrapper

use of org.apache.zeppelin.elasticsearch.action.AggWrapper 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 = getTotal(result);
            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 3 with AggWrapper

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

the class TransportBasedClient method setAggregations.

private void setAggregations(Aggregations aggregations, ActionResponse actionResp) {
    // Only the result of the first aggregation is returned
    // 
    final Aggregation agg = aggregations.asList().get(0);
    if (agg instanceof InternalMetricsAggregation) {
        actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE, XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
    } else if (agg instanceof InternalSingleBucketAggregation) {
        actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE, XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
    } else if (agg instanceof InternalMultiBucketAggregation) {
        final Set<String> headerKeys = new HashSet<>();
        final List<Map<String, Object>> buckets = new LinkedList<>();
        final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;
        for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
            try {
                final XContentBuilder builder = XContentFactory.jsonBuilder();
                bucket.toXContent(builder, null);
                actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
            } catch (final IOException e) {
            // Ignored
            }
        }
    }
}
Also used : AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) IOException(java.io.IOException) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) LinkedList(java.util.LinkedList) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalMetricsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Aggregations

AggWrapper (org.apache.zeppelin.elasticsearch.action.AggWrapper)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonNode (com.mashape.unirest.http.JsonNode)1 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)1 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)1 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Aggregation (org.elasticsearch.search.aggregations.Aggregation)1 InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)1 InternalSingleBucketAggregation (org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation)1