@Override public String toString() { try { return Strings.toString(toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS)); } catch (IOException e) { return super.toString(); } }
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
@Override public String convert(QueryBuilder anItem) { try { BytesReference bytes = anItem.toXContent(JsonXContent.contentBuilder(), new ToXContent.MapParams(null)) .bytes(); return bytes.utf8ToString(); } catch (IOException e) { log.error("Unable to transform querybuilder to a json string due to an exception", e); throw new RuntimeException("Unable to transform querybuilder to a json string due to an exception", e); } } }
/** * Helper to declare an object that will be parsed into a {@link BytesReference} */ public void declareRawObject(final AbstractObjectParser<Value, Context> parser, final BiConsumer<Value, BytesReference> consumer, final ParseField field) { final CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> { try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.copyCurrentStructure(p); return BytesReference.bytes(builder); } }; parser.declareField(consumer, bytesParser, field, ValueType.OBJECT); }
/** * Parse the script configured in the given settings. */ public static Script parse(Settings settings) { try (XContentBuilder builder = JsonXContent.contentBuilder()){ builder.startObject(); settings.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); try (InputStream stream = BytesReference.bytes(builder).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { return parse(parser); } } catch (IOException e) { // it should not happen since we are not actually reading from a stream but an in-memory byte[] throw new IllegalStateException(e); } }
/** * Parses the value as a geopoint. The following types of values are supported: * <p> * Object: has to contain either lat and lon or geohash fields * <p> * String: expected to be in "latitude, longitude" format or a geohash * <p> * Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true */ public static GeoPoint parseGeoPoint(Object value, final boolean ignoreZValue) throws ElasticsearchParseException { try { XContentBuilder content = JsonXContent.contentBuilder(); content.startObject(); content.field("null_value", value); content.endObject(); try (InputStream stream = BytesReference.bytes(content).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value return parseGeoPoint(parser, new GeoPoint(), ignoreZValue); } } catch (IOException ex) { throw new ElasticsearchParseException("error parsing geopoint", ex); } }
@Override public String toString() { try { return toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { return super.toString(); } }
@Override public String toString() { try { return toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { return super.toString(); } } }
@Override public String toString() { try { return Strings.toString(toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS)); } catch (IOException e) { return super.toString(); } }
@Override public String toString() { try { return toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { return super.toString(); } }
XContentBuilder createIndexSettings(int shards, int replicas, String refresh) { try { XContentBuilder createIndexSettings = JsonXContent.contentBuilder().startObject(). field("index.number_of_shards", shards). field("index.number_of_replicas", replicas). field("index.refresh_interval", refresh).endObject(); return createIndexSettings; } catch (IOException ex) { throw new RuntimeException(ex); } }
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
private static BytesReference xContentToBytes(ToXContentObject object) throws IOException { try (XContentBuilder builder = JsonXContent.contentBuilder()) { object.toXContent(builder, ToXContentObject.EMPTY_PARAMS); return BytesReference.bytes(builder); } } }
private static BytesReference xContentToBytes(ToXContentObject object) throws IOException { try (XContentBuilder builder = JsonXContent.contentBuilder()) { object.toXContent(builder, ToXContentObject.EMPTY_PARAMS); return BytesReference.bytes(builder); } } }
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
@Override public String toString() { try { XContentBuilder xcontent = JsonXContent.contentBuilder(); return toXContent(xcontent, EMPTY_PARAMS).prettyPrint().string(); } catch (IOException e) { return super.toString(); } }
private static BytesReference parseBytes(XContentParser parser) throws IOException { XContentBuilder contentBuilder = JsonXContent.contentBuilder(); contentBuilder.generator().copyCurrentStructure(parser); return BytesReference.bytes(contentBuilder); }
/** * Prints the segments info for the given indices as debug logging. */ public void logSegmentsState(String... indices) throws Exception { IndicesSegmentResponse segsRsp = client().admin().indices().prepareSegments(indices).get(); logger.debug("segments {} state: \n{}", indices.length == 0 ? "[_all]" : indices, segsRsp.toXContent(JsonXContent.contentBuilder().prettyPrint(), ToXContent.EMPTY_PARAMS).string()); }
private static BytesReference parseBytes(XContentParser parser) throws IOException { XContentBuilder contentBuilder = JsonXContent.contentBuilder(); contentBuilder.generator().copyCurrentStructure(parser); return BytesReference.bytes(contentBuilder); }
public void declareRawObject(BiConsumer<Value, BytesReference> consumer, ParseField field) { CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> { try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.prettyPrint(); builder.copyCurrentStructure(p); return builder.bytes(); } }; declareField(consumer, bytesParser, field, ValueType.OBJECT); }