@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); } } }
/** * The query source to execute. */ public TermsByQueryRequest query(XContentBuilder builder) { this.querySource = builder == null ? null : builder.bytes(); return this; }
/** * Returns a string representation of the builder (only applicable for text based xcontent). */ public String string() throws IOException { return bytes().utf8ToString(); }
/** * Sets the aliases that will be associated with the index when it gets created */ public CreateIndexRequest aliases(XContentBuilder source) { return aliases(source.bytes()); }
/** * Constructs a new builder with a raw search query. */ public DeleteByQueryRequestBuilder setQuery(XContentBuilder query) { return setQuery(query.bytes()); }
private static BytesReference toXContent(ToXContent result) throws IOException { try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { // Elasticsearch's Response object never emit starting or ending objects. Most other implementers of ToXContent do.... builder.startObject(); result.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); return builder.bytes(); } }
public static ElasticsearchDocument build(Callable<XContentBuilder> builder) { try { return new ElasticsearchDocument(builder.call().bytes()); } catch (Exception e) { throw new ElasticsearchRepositoryException(e); } } }
/** * The template source definition. */ public PutIndexTemplateRequest source(XContentBuilder templateBuilder) { try { return source(templateBuilder.bytes(), templateBuilder.contentType()); } catch (Exception e) { throw new IllegalArgumentException("Failed to build json for template request", e); } }
/** * Adds mapping that will be added when the index gets created. * * @param type The mapping type * @param source The mapping source */ public CreateIndexRequest mapping(String type, XContentBuilder source) { return mapping(type, source.bytes(), source.contentType()); }
/** * Sets the content source to index. */ public IndexRequest source(XContentBuilder sourceBuilder) { return source(sourceBuilder.bytes(), sourceBuilder.contentType()); }
/** * Creates a new response based on {@link XContentBuilder}. */ public BytesRestResponse(RestStatus status, XContentBuilder builder) { this(status, builder.contentType().mediaType(), builder.bytes()); }
public MappingMetaData(String type, Map<String, Object> mapping) throws IOException { this.type = type; XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mapping); this.source = new CompressedXContent(mappingBuilder.bytes()); Map<String, Object> withoutType = mapping; if (mapping.size() == 1 && mapping.containsKey(type)) { withoutType = (Map<String, Object>) mapping.get(type); } initMappers(withoutType); }
public BytesReference source(IndexService indexService, DocumentMapper docMapper, Map sourceAsMap, Uid uid) throws JsonParseException, JsonMappingException, IOException { if (docMapper.sourceMapper().enabled()) { // retreive from _source columns stored as blob in cassandra if available. ByteBuffer bb = (ByteBuffer) sourceAsMap.get(SourceFieldMapper.NAME); if (bb != null) return new BytesArray(bb.array(), bb.position(), bb.limit() - bb.position()); } // rebuild _source from all cassandra columns. XContentBuilder builder = buildDocument(docMapper, sourceAsMap, true, isStaticDocument(indexService, uid)); builder.humanReadable(true); return builder.bytes(); }
public static Map<String, Object> convertToMap(ToXContent part) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); part.toXContent(builder, EMPTY_PARAMS); builder.endObject(); return XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2(); }
public BytesReference buildAsBytes(XContentType contentType) throws SearchSourceBuilderException { try { XContentBuilder builder = XContentFactory.contentBuilder(contentType); toXContent(builder, ToXContent.EMPTY_PARAMS); return builder.bytes(); } catch (Exception e) { throw new SearchSourceBuilderException("Failed to build search source", e); } } }
public NettyInteractiveResponse(String type, XContentBuilder builder) throws IOException { this.type = type; XContentBuilder responseBuilder = jsonBuilder() .startObject().field("success", true).field("type", type); if (builder != null) { responseBuilder.rawField("data", builder.bytes()); } responseBuilder.endObject(); this.response = new TextWebSocketFrame(responseBuilder.string()); }
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); }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return builder.bytes(); } }
public static BytesReference nodeBytesReferenceForMapValue(final @Nullable Map<String, ?> o) { if (o == null) { return null; } else { try { return XContentFactory.jsonBuilder().map(o).bytes(); } catch (IOException e) { throw Throwables.propagate(e); } } } }
/** * Create a new {@link XContentParser}. */ protected final XContentParser createParser(XContentBuilder builder) throws IOException { return builder.generator().contentType().xContent().createParser(xContentRegistry(), builder.bytes()); }