Search in sources :

Example 6 with ActionResponse

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

the class ElasticsearchInterpreter method processCount.

/**
   * Processes a "count" request.
   *
   * @param urlItems Items of the URL
   * @param data May contains the JSON of the request
   * @param interpreterContext Instance of the context
   * @return Result of the count request, it contains the total hits
   */
private InterpreterResult processCount(String[] urlItems, String data, InterpreterContext interpreterContext) {
    if (urlItems.length > 2) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Bad URL (it should be /index1,index2,.../type1,type2,...)");
    }
    final ActionResponse response = searchData(urlItems, data, 0);
    addAngularObject(interpreterContext, "count", response.getTotalHits());
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "" + response.getTotalHits());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 7 with ActionResponse

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

the class ElasticsearchInterpreter method processDelete.

/**
   * Processes a "delete" request.
   *
   * @param urlItems Items of the URL
   * @return Result of the delete request, it contains the id of the deleted document
   */
private InterpreterResult processDelete(String[] urlItems) {
    final String[] indexTypeId = getIndexTypeId(urlItems);
    if (indexTypeId == null) {
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Bad URL (it should be /index/type/id)");
    }
    final ActionResponse response = elsClient.delete(indexTypeId[0], indexTypeId[1], indexTypeId[2]);
    if (response.isSucceeded()) {
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, response.getHit().getId());
    }
    return new InterpreterResult(InterpreterResult.Code.ERROR, "Document not found");
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 8 with ActionResponse

use of org.apache.zeppelin.elasticsearch.action.ActionResponse 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)

Example 9 with ActionResponse

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

the class TransportBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    reqBuilder.setIndices();
    if (indices != null) {
        reqBuilder.setIndices(indices);
    }
    if (types != null) {
        reqBuilder.setTypes(types);
    }
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            @SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class);
            reqBuilder.setExtraSource(source);
        } catch (final JsonSyntaxException e) {
            // This is not a JSON (or maybe not well formatted...)
            reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
        }
    }
    reqBuilder.setSize(size);
    final SearchResponse searchResp = reqBuilder.get();
    final ActionResponse actionResp = new ActionResponse().succeeded(true).totalHits(searchResp.getHits().getTotalHits());
    if (searchResp.getAggregations() != null) {
        setAggregations(searchResp.getAggregations(), actionResp);
    } else {
        for (final SearchHit hit : searchResp.getHits()) {
            // Fields can be found either in _source, or in fields (it depends on the query)
            // => specific for elasticsearch's version < 5
            //
            String src = hit.getSourceAsString();
            if (src == null) {
                final Map<String, Object> hitFields = new HashMap<>();
                for (final SearchHitField hitField : hit.getFields().values()) {
                    hitFields.put(hitField.getName(), hitField.getValues());
                }
                src = gson.toJson(hitFields);
            }
            actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
        }
    }
    return actionResp;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) SearchHitField(org.elasticsearch.search.SearchHitField) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)9 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)5 JsonNode (com.mashape.unirest.http.JsonNode)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)4 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)4 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)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 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 AggWrapper (org.apache.zeppelin.elasticsearch.action.AggWrapper)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1