@Override public void addEvent(Event event, IndexNameBuilder indexNameBuilder, String indexType, long ttlMs) throws Exception { BytesReference content = serializer.getContentBuilder(event).bytes(); Map<String, Map<String, String>> parameters = new HashMap<String, Map<String, String>>(); Map<String, String> indexParameters = new HashMap<String, String>(); indexParameters.put(INDEX_PARAM, indexNameBuilder.getIndexName(event)); indexParameters.put(TYPE_PARAM, indexType); if (ttlMs > 0) { indexParameters.put(TTL_PARAM, Long.toString(ttlMs)); } parameters.put(INDEX_OPERATION_NAME, indexParameters); Gson gson = new Gson(); synchronized (bulkBuilder) { bulkBuilder.append(gson.toJson(parameters)); bulkBuilder.append("\n"); bulkBuilder.append(content.toBytesArray().toUtf8()); bulkBuilder.append("\n"); } }
private SourceToParse(Origin origin, BytesReference source) { this.origin = origin; // we always convert back to byte array, since we store it and Field only supports bytes.. // so, we might as well do it here, and improve the performance of working with direct byte arrays this.source = source.toBytesArray(); this.parser = null; }
public TermVector(BytesReference termVectors, long readOffset) throws IOException { this.perFieldTermVectorInput = StreamInput.wrap(termVectors.toBytesArray()); this.readOffset = readOffset; reset(); }
@Override public void addEvent(Event event, IndexNameBuilder indexNameBuilder, String indexType, long ttlMs) throws Exception { BytesReference content = serializer.getContentBuilder(event).bytes(); Map<String, Map<String, String>> parameters = new HashMap<String, Map<String, String>>(); Map<String, String> indexParameters = new HashMap<String, String>(); indexParameters.put(INDEX_PARAM, indexNameBuilder.getIndexName(event)); indexParameters.put(TYPE_PARAM, indexType); if (ttlMs > 0) { indexParameters.put(TTL_PARAM, Long.toString(ttlMs)); } parameters.put(INDEX_OPERATION_NAME, indexParameters); Gson gson = new Gson(); synchronized(bulkBuilder) { bulkBuilder.append(gson.toJson(parameters)); bulkBuilder.append("\n"); bulkBuilder.append(content.toBytesArray().toUtf8()); bulkBuilder.append("\n"); } }
@Override public void addEvent(Event event, IndexNameBuilder indexNameBuilder, String indexType, long ttlMs) throws Exception { BytesReference content = serializer.getContentBuilder(event).bytes(); Map<String, Map<String, String>> parameters = new HashMap<String, Map<String, String>>(); Map<String, String> indexParameters = new HashMap<String, String>(); indexParameters.put(INDEX_PARAM, indexNameBuilder.getIndexName(event)); indexParameters.put(TYPE_PARAM, indexType); if (ttlMs > 0) { indexParameters.put(TTL_PARAM, Long.toString(ttlMs)); } parameters.put(INDEX_OPERATION_NAME, indexParameters); Gson gson = new Gson(); synchronized (bulkBuilder) { bulkBuilder.append(gson.toJson(parameters)); bulkBuilder.append("\n"); bulkBuilder.append(content.toBytesArray().toUtf8()); bulkBuilder.append("\n"); } }
/** * Returns a {@link String} view of the data. */ public String string() { if (text == null) { if (!bytes.hasArray()) { bytes = bytes.toBytesArray(); } text = new String(bytes.array(), bytes.arrayOffset(), bytes.length(), StandardCharsets.UTF_8); } return text; }
/** * Returns a string representation of the builder (only applicable for text based xcontent). */ public String string() throws IOException { close(); BytesArray bytesArray = bytes().toBytesArray(); return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8); }
public static StreamInput wrap(BytesReference reference) { if (reference.hasArray() == false) { reference = reference.toBytesArray(); } return wrap(reference.array(), reference.arrayOffset(), reference.length()); }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder field(String name, BytesReference value) throws IOException { field(name); if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder field(XContentBuilderString name, BytesReference value) throws IOException { field(name); if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder value(BytesReference value) throws IOException { if (value == null) { return nullValue(); } if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
@Override public String toString() { if (sourceBuilder != null) { return sourceBuilder.toString(); } if (request.source() != null) { try { return XContentHelper.convertToJson(request.source().toBytesArray(), false, true); } catch (Exception e) { return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}"; } } return new SearchSourceBuilder().toString(); }
@Override public String toString() { if (sourceBuilder != null) { return sourceBuilder.toString(); } if (request.source() != null) { try { return XContentHelper.convertToJson(request.source().toBytesArray(), false, true); } catch (Exception e) { return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}"; } } return new QuerySourceBuilder().toString(); } }
/** * @param headerRef Stores offsets per field in the {@code termVectors} and some * header information as {@link BytesRef}. * @param termVectors Stores the actual term vectors as a {@link BytesRef}. */ public TermVectorsFields(BytesReference headerRef, BytesReference termVectors) throws IOException { StreamInput header = StreamInput.wrap(headerRef.toBytesArray()); fieldMap = new ObjectLongHashMap<>(); // here we read the header to fill the field offset map String headerString = header.readString(); assert headerString.equals("TV"); int version = header.readInt(); assert version == -1; hasTermStatistic = header.readBoolean(); hasFieldStatistic = header.readBoolean(); hasScores = header.readBoolean(); final int numFields = header.readVInt(); for (int i = 0; i < numFields; i++) { fieldMap.put((header.readString()), header.readVLong()); } header.close(); // reference to the term vector data this.termVectors = termVectors; }
public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException { if (bytes.hasArray()) { return convertToJson(bytes.array(), bytes.arrayOffset(), bytes.length(), reformatJson, prettyPrint); } XContentType xContentType = XContentFactory.xContentType(bytes); if (xContentType == XContentType.JSON && !reformatJson) { BytesArray bytesArray = bytes.toBytesArray(); return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8); } try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(bytes.streamInput())) { parser.nextToken(); XContentBuilder builder = XContentFactory.jsonBuilder(); if (prettyPrint) { builder.prettyPrint(); } builder.copyCurrentStructure(parser); return builder.string(); } }
BytesReference bytes = (BytesReference) value; if (!bytes.hasArray()) { bytes = bytes.toBytesArray(); generator.writeString(text.string()); } else { BytesArray bytesArray = text.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
public XContentBuilder field(String name, Text value) throws IOException { field(name); if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well? BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
public XContentBuilder field(XContentBuilderString name, Text value) throws IOException { field(name); if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well? BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
public XContentBuilder value(Text value) throws IOException { if (value == null) { return nullValue(); } if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
content = content.toBytesArray();