private void processBodyPerLine(byte[] body, BulkRequestBuilder bulkRequestBuilder) throws Exception { BufferedReader reader = new BufferedReader(new StringReader(new String(body))); JsonFactory factory = new JsonFactory(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { JsonXContentParser parser = new JsonXContentParser(factory.createJsonParser(line)); Map<String, Object> asMap = parser.map(); if (asMap.get("delete") != null) { // We don't touch deleteRequests String newContent = line + "\n"; bulkRequestBuilder.add(newContent.getBytes(), 0, newContent.getBytes().length, false); } else { // But we send other requests to the script Engine in ctx field Map<String, Object> ctx; String payload = null; try { payload = reader.readLine(); ctx = XContentFactory.xContent(XContentType.JSON).createParser(payload).mapAndClose(); } catch (IOException e) { logger.warn("failed to parse {}", e, payload); continue; } script.setNextVar("ctx", ctx); script.run(); ctx = (Map<String, Object>) script.unwrap(ctx); if (ctx != null) { // Adding header StringBuffer request = new StringBuffer(line); request.append("\n"); // Adding new payload request.append(XContentFactory.jsonBuilder().map(ctx).string()); request.append("\n"); if (logger.isTraceEnabled()) { logger.trace("new bulk request is now: {}", request.toString()); } byte[] binRequest = request.toString().getBytes(); bulkRequestBuilder.add(binRequest, 0, binRequest.length, false); } } } }
private String buildJSONFromFields(Collection<SearchHitField> values) { JsonFactory nodeFactory = new JsonFactory(); try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); generator.writeStartObject(); for (SearchHitField value : values) { if (value.getValues().size() > 1) { generator.writeArrayFieldStart(value.getName()); for (Object val : value.getValues()) { generator.writeObject(val); } generator.writeEndArray(); } else { generator.writeObjectField(value.getName(), value.getValue()); } } generator.writeEndObject(); generator.flush(); return new String(stream.toByteArray(), Charset.forName("UTF-8")); } catch (IOException e) { return null; } }