/** * Creates a query builder given a query provided as a {@link BytesReference} */ public WrapperQueryBuilder(BytesReference source) { if (source == null || source.length() == 0) { throw new IllegalArgumentException("query source text cannot be null or empty"); } this.source = BytesRef.deepCopyOf(source.toBytesRef()).bytes; }
private static BytesReference readXContent(final Reader reader, final XContentType xContentType) throws IOException { BytesReference retVal; XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); retVal = BytesReference.bytes(builder); } finally { if (parser != null) { parser.close(); } } //validate Settings.builder().loadFromStream("dummy.json", new ByteArrayInputStream(BytesReference.toBytes(retVal)), true).build(); return retVal; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (source != null) { builder.rawValue(source.streamInput(), xContentType); } else { builder.startObject().endObject(); } return builder; } }
@Override public String explain() { try { XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); this.firstSearchRequest.request().source().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS); XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); this.secondSearchRequest.request().source().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS); String explained = String.format("performing %s on :\n left query:\n%s\n right query:\n%s", this.relation.name, BytesReference.bytes(firstBuilder).utf8ToString(), BytesReference.bytes(secondBuilder).utf8ToString()); return explained; } catch (IOException e) { e.printStackTrace(); } return null; }
public static String hitsAsStringResult(SearchHits results, MetaSearchResult metaResults) throws IOException { if(results == null) return null; Object[] searchHits; searchHits = new Object[(int) results.getTotalHits()]; int i = 0; for(SearchHit hit : results) { HashMap<String,Object> value = new HashMap<>(); value.put("_id",hit.getId()); value.put("_type", hit.getType()); value.put("_score", hit.getScore()); value.put("_source", hit.getSourceAsMap()); searchHits[i] = value; i++; } HashMap<String,Object> hits = new HashMap<>(); hits.put("total",results.getTotalHits()); hits.put("max_score",results.getMaxScore()); hits.put("hits",searchHits); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); builder.startObject(); builder.field("took", metaResults.getTookImMilli()); builder.field("timed_out",metaResults.isTimedOut()); builder.field("_shards", ImmutableMap.of("total", metaResults.getTotalNumOfShards(), "successful", metaResults.getSuccessfulShards() , "failed", metaResults.getFailedShards())); builder.field("hits",hits) ; builder.endObject(); return BytesReference.bytes(builder).utf8ToString(); } }
DocWriteRequest.OpType opType = action.opType(); try (XContentBuilder metadata = XContentBuilder.builder(bulkContentType.xContent())) { metadata.startObject(); metadata.startObject(opType.getLowercase()); if (Strings.hasLength(action.index())) { metadata.field("_index", action.index()); metadata.endObject(); BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef(); content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length); content.write(separator); try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) { builder.copyCurrentStructure(parser); source = BytesReference.bytes(builder).toBytesRef(); source = XContentHelper.toXContent((UpdateRequest) action, bulkContentType, false).toBytesRef();
public static StreamInput wrap(BytesReference reference) { if (reference.hasArray() == false) { reference = reference.toBytesArray(); } return wrap(reference.array(), reference.arrayOffset(), reference.length()); }
@Override public void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(getName()); try (InputStream stream = functionBytes.streamInput()) { builder.rawField(fieldName, stream); } builder.field(DecayFunctionParser.MULTI_VALUE_MODE.getPreferredName(), multiValueMode.name()); builder.endObject(); }
/** * Create a {@link CompressedXContent} out of a {@link ToXContent} instance. */ public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException { BytesStreamOutput bStream = new BytesStreamOutput(); OutputStream compressedStream = CompressorFactory.COMPRESSOR.streamOutput(bStream); CRC32 crc32 = new CRC32(); OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32); try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) { builder.startObject(); xcontent.toXContent(builder, params); builder.endObject(); } this.bytes = BytesReference.toBytes(bStream.bytes()); this.crc32 = (int) crc32.getValue(); assertConsistent(); }
public static byte[] writeMultiLineFormat(MultiSearchRequest multiSearchRequest, XContent xContent) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); for (SearchRequest request : multiSearchRequest.requests()) { try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { writeSearchRequestParams(request, xContentBuilder); BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { if (request.source() != null) { request.source().toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); } else { xContentBuilder.startObject(); xContentBuilder.endObject(); } BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); } return output.toByteArray(); }
@Override public void readFrom(StreamInput in) throws IOException { index = in.readString(); type = in.readOptionalString(); id = in.readString(); if (in.getVersion().onOrAfter(Version.V_6_6_0)) { seqNo = in.readZLong(); if (exists) { source = in.readBytesReference(); if (source.length() == 0) { source = null;
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field(FULL_NAME.getPreferredName(), fullName); if (params.paramAsBoolean("pretty", false)) { builder.field("mapping", sourceAsMap()); } else { try (InputStream stream = source.streamInput()) { builder.rawField(MAPPING.getPreferredName(), stream, XContentType.JSON); } } return builder; }
/** * Interprets the referenced bytes as UTF8 bytes, returning the resulting string */ public String utf8ToString() { return toBytesRef().utf8ToString(); }
@Override public void readFrom(StreamInput in) throws IOException { score = in.readFloat(); id = in.readOptionalText(); type = in.readOptionalText(); nestedIdentity = in.readOptionalWriteable(NestedIdentity::new); version = in.readLong(); source = in.readBytesReference(); if (source.length() == 0) { source = null;
private String format(TcpChannel channel, BytesReference message, String event) throws IOException { final StringBuilder sb = new StringBuilder(); sb.append(channel); int messageLengthWithHeader = HEADER_SIZE + message.length(); if (message.length() == 0) { sb.append(" [ping]").append(' ').append(event).append(": ").append(messageLengthWithHeader).append('B'); } else { boolean success = false; StreamInput streamInput = message.streamInput(); try { final long requestId = streamInput.readLong(); final byte status = streamInput.readByte(); final boolean isRequest = TransportStatus.isRequest(status); final String type = isRequest ? "request" : "response"; final String version = Version.fromId(streamInput.readInt()).toString(); sb.append(" [length: ").append(messageLengthWithHeader); sb.append(", request id: ").append(requestId); try { final int bytesConsumed = TcpHeader.REQUEST_ID_SIZE + TcpHeader.STATUS_SIZE + TcpHeader.VERSION_ID_SIZE; compressor = CompressorFactory.compressor(message.slice(bytesConsumed, message.length() - bytesConsumed)); } catch (NotCompressedException ex) { throw new IllegalStateException(ex);
/** * @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 { try (StreamInput header = headerRef.streamInput()) { 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()); } } // reference to the term vector data this.termVectors = termVectors; }
String profileName = channel.getProfile(); InetSocketAddress remoteAddress = channel.getRemoteAddress(); int messageLengthBytes = reference.length(); final int totalMessageSize = messageLengthBytes + TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE; readBytesMetric.inc(totalMessageSize); StreamInput streamIn = reference.streamInput(); boolean success = false; try (ThreadContext.StoredContext tCtx = threadPool.getThreadContext().stashContext()) { long requestId = streamIn.readLong(); byte status = streamIn.readByte(); Version version = Version.fromId(streamIn.readInt()); if (TransportStatus.isCompress(status) && hasMessageBytesToRead && streamIn.available() > 0) { Compressor compressor; try { final int bytesConsumed = TcpHeader.REQUEST_ID_SIZE + TcpHeader.STATUS_SIZE + TcpHeader.VERSION_ID_SIZE; compressor = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed)); } catch (NotCompressedException ex) { int maxToRead = Math.min(reference.length(), 10); StringBuilder sb = new StringBuilder("stream marked as compressed, but no compressor found, first [").append(maxToRead) .append("] content bytes out of [").append(reference.length()) .append("] readable bytes with message size [").append(messageLengthBytes).append("] ").append("] are ["); for (int i = 0; i < maxToRead; i++) { sb.append(reference.get(i)).append(",");
@Override public BytesReference cacheKey() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); this.innerWriteTo(out, true); // copy it over, most requests are small, we might as well copy to make sure we are not sliced... // we could potentially keep it without copying, but then pay the price of extra unused bytes up to a page return new BytesArray(out.bytes().toBytesRef(), true);// do a deep copy }
public void testSliceWriteToOutputStream() throws IOException { int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 5)); BytesReference pbr = newBytesReference(length); int sliceOffset = randomIntBetween(1, length / 2); int sliceLength = length - sliceOffset; BytesReference slice = pbr.slice(sliceOffset, sliceLength); BytesStreamOutput sliceOut = new BytesStreamOutput(sliceLength); slice.writeTo(sliceOut); assertEquals(slice.length(), sliceOut.size()); assertArrayEquals(BytesReference.toBytes(slice), BytesReference.toBytes(sliceOut.bytes())); sliceOut.close(); }
/** * Create a {@link CompressedXContent} out of a serialized {@link ToXContent} * that may already be compressed. */ public CompressedXContent(BytesReference data) throws IOException { Compressor compressor = CompressorFactory.compressor(data); if (compressor != null) { // already compressed... this.bytes = BytesReference.toBytes(data); this.crc32 = crc32(new BytesArray(uncompressed())); } else { BytesStreamOutput out = new BytesStreamOutput(); try (OutputStream compressedOutput = CompressorFactory.COMPRESSOR.streamOutput(out)) { data.writeTo(compressedOutput); } this.bytes = BytesReference.toBytes(out.bytes()); this.crc32 = crc32(data); } assertConsistent(); }