private Response search(final String table, final String key) throws IOException { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("query"); builder.startObject("term"); builder.field(KEY, key); builder.endObject(); builder.endObject(); builder.endObject(); return search(table, builder); } }
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; }
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(); } }
public static XContentBuilder restContentBuilder(RestRequest request, @Nullable BytesReference autoDetectSource) throws IOException { XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type"))); if (contentType == null) { // try and guess it from the auto detect source if (autoDetectSource != null) { contentType = XContentFactory.xContentType(autoDetectSource); } } if (contentType == null) { // default to JSON contentType = XContentType.JSON; } XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), new BytesStreamOutput()); if (request.paramAsBoolean("pretty", false)) { builder.prettyPrint().lfAtEnd(); } builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable())); String casing = request.param("case"); if (casing != null && "camelCase".equals(casing)) { builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE); } else { // we expect all REST interfaces to write results in underscore casing, so // no need for double casing builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE); } return builder; }
/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key. * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See this class's * description for a discussion of error codes. */ @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { final XContentBuilder doc = jsonBuilder().startObject(); for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.field(entry.getKey(), entry.getValue()); } doc.endObject(); client.prepareIndex(indexKey, table, key).setSource(doc).execute().actionGet(); return Status.OK; } catch (Exception e) { e.printStackTrace(); return Status.ERROR; } }
private static class StreamingPropertyString { private final String propertyValue; public StreamingPropertyString(String propertyValue) { this.propertyValue = propertyValue; } public String getPropertyValue() { return propertyValue; } }
public void registerPercolateQuery(String indexName, String queryName, QueryBuilder queryBuilder) { try { client.prepareIndex(indexName, PERCOLATOR_TYPE, queryName) .setSource(XContentFactory.jsonBuilder() .startObject() .field("query", queryBuilder) .endObject()) .setRefresh(true) .execute().actionGet(); } catch (IOException e) { DTThrowable.rethrow(e); } }
@Override public boolean indexWord(String word) { if (bulk == null) { bulk = client.prepareBulk(); } try { bulk.add(client.prepareIndex(INDEX_NAME, INDEX_TYPE) .setSource(XContentFactory.jsonBuilder() .startObject() .field(FIELD_NAME, word) .endObject())); } catch (IOException e) { // should never happen throw new RuntimeException(e); } return true; }
public void store(HttpArticle article, Map<String, Object> fields) throws IOException { XContentBuilder jsonBuilder = jsonBuilder(); jsonBuilder.startObject(); applyFields(jsonBuilder, article, fields); jsonBuilder.endObject(); IndexRequestBuilder insert = getConnection().getClient() .prepareIndex(getIndex(), getType(), article.getUrl()) .setSource(jsonBuilder); getConnection().getProcessor().add(insert.request()); }
public void memStatus() throws IOException { NodeStats[] nodeStats = client.admin().cluster().prepareNodesStats() .setJvm(true).setIndices(true).setTransport(true) .execute().actionGet().getNodes(); log("==== MEMORY ===="); log("Committed heap size: [0]=" + nodeStats[0].getJvm().getMem().getHeapCommitted() + ", [1]=" + nodeStats[1].getJvm().getMem().getHeapCommitted()); log("Used heap size: [0]=" + nodeStats[0].getJvm().getMem().getHeapUsed() + ", [1]=" + nodeStats[1].getJvm().getMem().getHeapUsed()); log("FieldData cache size: [0]=" + nodeStats[0].getIndices().getFieldData().getMemorySize() + ", [1]=" + nodeStats[1].getIndices().getFieldData().getMemorySize()); log("Query cache size: [0]=" + nodeStats[0].getIndices().getQueryCache().getMemorySize() + ", [1]=" + nodeStats[1].getIndices().getQueryCache().getMemorySize()); log(""); log("==== NETWORK ===="); log("Transport: [0]=" + nodeStats[0].getTransport().toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS).string() + ", [1]=" + nodeStats[1].getTransport().toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS).string()); log(""); }
private static XContentBuilder source(Timestamp<?> time) throws IOException { XContentBuilder builder = jsonBuilder().startObject().startObject(TYPE); time.saveFields(builder); return builder.endObject().endObject(); }
protected void write(T obj, StreamOutput streamOutput) throws IOException { try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, streamOutput)) { builder.startObject(); obj.toXContent(builder, SNAPSHOT_ONLY_FORMAT_PARAMS); builder.endObject(); } } }
/** * Write indexing info into activity log if enabled. * * @param indexingInfo to write */ protected void writeActivityLogRecord(ProjectIndexingInfo indexingInfo) { if (activityLogIndexName != null) { try { client.prepareIndex(activityLogIndexName, activityLogTypeName) .setSource(indexingInfo.buildDocument(jsonBuilder(), riverName().getName(), true, true)).execute() .actionGet(); } catch (Exception e) { logger.error("Error during index update result writing to the audit log {}", e.getMessage()); } } }
protected void storeIndex(final String indexName, final String typeName, final Map<String, Object> dataMap) { dataMap.put(TIMESTAMP_FIELD, new Date()); if (logger.isDebugEnabled()) { logger.debug(indexName + "/" + typeName + " : dataMap" + dataMap); } try { esClient.prepareIndex(indexName, typeName).setRefresh(true).setSource(jsonBuilder().value(dataMap)).execute().actionGet(); } catch (final Exception e) { logger.warn("Could not write a content into index.", e); } }
XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (currentFieldName != null) { if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) { item.index = parser.text(); } else if (TYPE.match(currentFieldName, parser.getDeprecationHandler())) { item.id = parser.text(); } else if (DOC.match(currentFieldName, parser.getDeprecationHandler())) { item.doc = BytesReference.bytes(jsonBuilder().copyCurrentStructure(parser)); item.xContentType = XContentType.JSON; } else if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) { IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString()); try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS); index.setSource(builder); } catch (IOException e) { throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask()); } index.execute(new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { listener.onResponse(null); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); }
parser = XContentFactory.xContent(contentType).createParser(data); while (parser.nextToken() != null) {}; parser = XContentFactory.xContent(contentType).createParser(data); builder.field(fieldName); builder.copyCurrentStructure(parser); } catch (JsonParseException ex) { } finally { if (parser != null) { parser.close();
private void setMapping(final String index) throws IOException { // Create index (if needed) final IndicesAdminClient indices = esResource.getClient().admin().indices(); if (!indices.exists(Requests.indicesExistsRequest(index)).actionGet().isExists()) { // We must submit all the mappings at once for parent child indices .prepareCreate(index) .addMapping(documentType, createDocumentMapping(XContentFactory.jsonBuilder())) .addMapping(mentionType, createMentionMapping(XContentFactory.jsonBuilder())) .addMapping(entityType, createEntityMapping(XContentFactory.jsonBuilder())) .addMapping(relationType, createRelationMapping(XContentFactory.jsonBuilder())) .execute() .actionGet(); } }
@Override public XContentBuilder getContentBuilder(Event event) throws IOException { XContentBuilder builder = jsonBuilder().startObject(); appendBody(builder, event); appendHeaders(builder, event); return builder; }
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("settings".equals(currentFieldName)) { builder.settings(templateSettingsBuilder.build()); } else if ("mappings".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); Map<String, Object> mappingSource = MapBuilder.<String, Object>newMapBuilder().put(mappingType, parser.mapOrdered()).map(); builder.putMapping(mappingType, Strings.toString(XContentFactory.jsonBuilder().map(mappingSource))); if (mapping.size() == 1) { String mappingType = mapping.keySet().iterator().next(); String mappingSource = Strings.toString(XContentFactory.jsonBuilder().map(mapping));