private static String convertToJsType(Object value) throws PermanentBackendException { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); builder.field("value", convertToEsType(value)); String s = builder.string(); int prefixLength = "{\"value\":".length(); int suffixLength = "}".length(); String result = s.substring(prefixLength, s.length() - suffixLength); result = result.replace("$", "\\$"); return result; } catch (IOException e) { throw new PermanentBackendException("Could not write json"); } }
private Response search(final String table, final XContentBuilder builder) throws IOException { refreshIfNeeded(); final Map<String, String> params = emptyMap(); final StringEntity entity = new StringEntity(builder.string()); final Header header = new BasicHeader("content-type", ContentType.APPLICATION_JSON.toString()); return restClient.performRequest("GET", "/" + indexKey + "/" + table + "/_search", params, entity, header); }
private XContentBuilder getGridFSMapping() throws IOException { XContentBuilder mapping = jsonBuilder() .startObject() .startObject(definition.getTypeName()) .startObject("properties") .startObject("content").field("type", "attachment").endObject() .startObject("filename").field("type", "string").endObject() .startObject("contentType").field("type", "string").endObject() .startObject("md5").field("type", "string").endObject() .startObject("length").field("type", "long").endObject() .startObject("chunkSize").field("type", "long").endObject() .endObject() .endObject() .endObject(); logger.info("GridFS Mapping: {}", mapping.string()); return mapping; }
builder.endObject(); final Map<String, String> params = emptyMap(); final StringEntity entity = new StringEntity(builder.string()); final Response createResponse = performRequest(restClient, "PUT", "/" + indexKey, params, entity); final int statusCode = createResponse.getStatusLine().getStatusCode();
@Override public void addResults(QueryStatusInfo statusInfo, QueryData data) { if (types.get() == null && statusInfo.getColumns() != null) { types.set(getTypes(statusInfo.getColumns())); } if (data.getData() == null) { return; } checkState(types.get() != null, "Type information is missing"); List<Column> columns = statusInfo.getColumns(); for (List<Object> fields : data.getData()) { try { XContentBuilder dataBuilder = jsonBuilder().startObject(); for (int i = 0; i < fields.size(); i++) { Type type = types.get().get(i); Object value = convertValue(fields.get(i), type); dataBuilder.field(columns.get(i).getName(), value); } dataBuilder.endObject(); client.prepareIndex(tableName, "doc") .setSource(dataBuilder.string(), JSON) .get(); } catch (IOException e) { throw new UncheckedIOException("Error loading data into Elasticsearch index: " + tableName, e); } } }
@Override public void add(PhotonDoc doc) { try { writer.println("{\"index\": {}}"); writer.println(Utils.convert(doc, this.languages).string()); } catch (IOException e) { log.error("error writing json file", e); } }
public static String toXContent(MetaData metaData, ToXContent.Params params) throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.startObject(); toXContent(metaData, builder, params); builder.endObject(); return builder.string(); }
/** * The mapping source definition. */ public PutMappingRequest source(XContentBuilder mappingBuilder) { try { return source(mappingBuilder.string(), mappingBuilder.contentType()); } catch (IOException e) { throw new IllegalArgumentException("Failed to build json for mapping request", e); } }
@Override public String toString() { try { return toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { return super.toString(); } } }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return builder.string(); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
@Override public synchronized String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); builder.startObject(); toXContent(builder, EMPTY_PARAMS); builder.endObject(); return builder.string(); } catch (IOException e) { return "{ \"error\" : \"" + e.getMessage() + "\"}"; } }
public void testEmptyName() throws IOException { // after version 5 for (String type : TYPES) { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("").field("type", type).endObject().endObject() .endObject().endObject().string(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)) ); assertThat(e.getMessage(), containsString("name cannot be empty string")); } }
public Builder filter(XContentBuilder filterBuilder) { try { return filter(filterBuilder.string()); } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
/** * 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()); }
/** * Allows to set the settings using a json builder. */ public CreateIndexRequest settings(XContentBuilder builder) { try { settings(builder.string(), builder.contentType()); } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to generate json settings from builder", e); } return this; }
NameOrDefinition(Map<String, ?> definition) { this.name = null; Objects.requireNonNull(definition); try { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.map(definition); this.definition = Settings.builder().loadFromSource(builder.string(), builder.contentType()).build(); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); } }
public static String queryAsJson(QueryBuilder query) throws IOException { XContentBuilder x = XContentFactory.jsonBuilder(); x.prettyPrint().lfAtEnd(); query.toXContent(x, ToXContent.EMPTY_PARAMS); return x.string(); }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.prettyPrint(); toXContent(builder, EMPTY_PARAMS); return builder.string(); } catch (Exception e) { return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}"; } }
@Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.prettyPrint(); toXContent(builder, EMPTY_PARAMS); return builder.string(); } catch (Exception e) { return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}"; } }
@Override public Object eval(LocalRenderContext ctx, Expression[] args) { try (XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint()) { return ((ToXContent) args[0].eval(ctx)).toXContent(builder, ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { Exceptions.handle(e); } return ""; }