Search in sources :

Example 1 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project camel by apache.

the class ElasticsearchActionRequestConverter method toSearchRequest.

@Converter
public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange) {
    SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class)).types(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    String queryText = null;
    if (queryObject instanceof Map<?, ?>) {
        Map<String, Object> mapQuery = (Map<String, Object>) queryObject;
        // Remove 'query' prefix from the query object for backward compatibility
        if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
            mapQuery = (Map<String, Object>) mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
        }
        try {
            XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
            queryText = contentBuilder.map(mapQuery).string();
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
    } else if (queryObject instanceof String) {
        queryText = (String) queryObject;
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode jsonTextObject = mapper.readValue(queryText, JsonNode.class);
            JsonNode parentJsonNode = jsonTextObject.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
            if (parentJsonNode != null) {
                queryText = parentJsonNode.toString();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
    } else {
        // Cannot convert the queryObject into SearchRequest
        return null;
    }
    searchSourceBuilder.query(QueryBuilders.wrapperQuery(queryText));
    searchRequest.source(searchSourceBuilder);
    return searchRequest;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Converter(org.apache.camel.Converter)

Example 2 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder 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)

Example 3 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project zeppelin by apache.

the class ElasticsearchInterpreter method buildAggResponseMessage.

private InterpreterResult buildAggResponseMessage(Aggregations aggregations) {
    // Only the result of the first aggregation is returned
    //
    final Aggregation agg = aggregations.asList().get(0);
    InterpreterResult.Type resType = InterpreterResult.Type.TEXT;
    String resMsg = "";
    if (agg instanceof InternalMetricsAggregation) {
        resMsg = XContentHelper.toString((InternalMetricsAggregation) agg).toString();
    } else if (agg instanceof InternalSingleBucketAggregation) {
        resMsg = 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);
                final Map<String, Object> bucketMap = JsonFlattener.flattenAsMap(builder.string());
                headerKeys.addAll(bucketMap.keySet());
                buckets.add(bucketMap);
            } catch (final IOException e) {
                logger.error("Processing bucket: " + e.getMessage(), e);
            }
        }
        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);
        }
        resType = InterpreterResult.Type.TABLE;
        resMsg = buffer.toString();
    }
    return new InterpreterResult(InterpreterResult.Code.SUCCESS, resType, resMsg);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) 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) JsonObject(com.google.gson.JsonObject) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet)

Example 4 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class SQLTransportIntegrationTest method getIndexMapping.

/**
     * Get all mappings from an index as JSON String
     *
     * @param index the name of the index
     * @return the index mapping as String
     * @throws IOException
     */
protected String getIndexMapping(String index) throws IOException {
    ClusterStateRequest request = Requests.clusterStateRequest().routingTable(false).nodes(false).metaData(true).indices(index);
    ClusterStateResponse response = client().admin().cluster().state(request).actionGet();
    MetaData metaData = response.getState().metaData();
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    IndexMetaData indexMetaData = metaData.iterator().next();
    for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) {
        builder.field(cursor.value.type());
        builder.map(cursor.value.sourceAsMap());
    }
    builder.endObject();
    return builder.string();
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) MetaData(org.elasticsearch.cluster.metadata.MetaData) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 5 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project crate by crate.

the class SQLTransportExecutor method toJsonString.

private static String toJsonString(Map value) throws IOException {
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.map(value);
    builder.close();
    return builder.bytes().toUtf8();
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)618 IOException (java.io.IOException)126 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28