Java Code Examples for org.elasticsearch.common.xcontent.XContentBuilder#flush()

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    boolean includeDefaults = params.paramAsBoolean("include_defaults", false);
    if (!includeDefaults) {
        // simulate the generation to make sure we don't add unnecessary content if all is default
        // if all are defaults, no need to write it at all - generating is twice is ok though
        BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0);
        XContentBuilder b =  new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput);
        b.startObject().flush();
        long pos = bytesStreamOutput.position();
        innerToXContent(b, false);
        b.flush();
        if (pos == bytesStreamOutput.position()) {
            return builder;
        }
    }
    builder.startObject(CONTENT_TYPE);
    innerToXContent(builder, includeDefaults);
    builder.endObject();
    return builder;
}
 
Example 2
Source File: SystemMonitorTarget.java    From fess with Apache License 2.0 6 votes vote down vote up
private void appendElasticsearchStats(final StringBuilder buf) {
    String stats = null;
    try {
        final FessEsClient esClient = ComponentUtil.getFessEsClient();
        final NodesStatsResponse response = esClient.admin().cluster().prepareNodesStats().all().execute().actionGet(10000L);
        final XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        response.toXContent(builder, ToXContent.EMPTY_PARAMS);
        builder.endObject();
        builder.flush();
        try (OutputStream out = builder.getOutputStream()) {
            stats = ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
        }
    } catch (final Exception e) {
        logger.debug("Failed to access Elasticsearch stats.", e);
    }
    buf.append("\"elasticsearch\":").append(stats).append(',');
}
 
Example 3
Source File: FulltextAnalyzerResolver.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static BytesReference encodeSettings(Settings settings) throws IOException {
    BytesStreamOutput bso = new BytesStreamOutput();
    XContentBuilder builder = XContentFactory.jsonBuilder(bso);
    builder.startObject();
    for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
        builder.field(entry.getKey(), entry.getValue());
    }
    builder.endObject();
    builder.flush();
    return bso.bytes();
}
 
Example 4
Source File: ExportCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    fieldsVisitor.reset();
    currentReader.document(doc, fieldsVisitor);

    Map<String, SearchHitField> searchFields = null;
    if (fieldsVisitor.fields() != null) {
        searchFields = new HashMap<String, SearchHitField>(fieldsVisitor.fields().size());
        for (Map.Entry<String, List<Object>> entry : fieldsVisitor.fields().entrySet()) {
            searchFields.put(entry.getKey(), new InternalSearchHitField(entry.getKey(), entry.getValue()));
        }
    }

    DocumentMapper documentMapper = context.mapperService()
            .documentMapper(fieldsVisitor.uid().type());
    Text typeText;
    if (documentMapper == null) {
        typeText = new StringAndBytesText(fieldsVisitor.uid().type());
    } else {
        typeText = documentMapper.typeText();
    }

    InternalSearchHit searchHit = new InternalSearchHit(doc,
            fieldsVisitor.uid().id(), typeText,
            sourceRequested ? fieldsVisitor.source() : null,
            searchFields);

    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
        if (fetchSubPhase.hitExecutionNeeded(context)) {
            hitContext.reset(searchHit, arc, doc, context.searcher().getIndexReader(), doc, fieldsVisitor);
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }

    searchHit.shardTarget(context.shardTarget());
    exportFields.hit(searchHit);
    BytesStreamOutput os = new BytesStreamOutput();
    XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(XContentType.JSON), os);
    exportFields.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.flush();
    BytesReference bytes = os.bytes();
    out.write(bytes.array(), bytes.arrayOffset(), bytes.length());
    out.write('\n');
    out.flush();
    numExported++;
}