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

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#array() . 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: UserProperty.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void toXContent(UserProperty userProperty, XContentBuilder builder, ToXContent.Params params) throws IOException {
    builder.startObject(userProperty.usernameWithTenant, XContentBuilder.FieldCaseConversion.NONE);
    builder.field("password", userProperty.password);
    {
        builder.startObject("db_privileges");
        for (String dbName : userProperty.dbPrivileges.keySet()) {
            builder.array(dbName, userProperty.dbPrivileges.get(dbName).toArray());
        }
        builder.endObject();

        builder.startObject("table_privileges");
        for (String tableName : userProperty.tablePrivileges.keySet()) {
            builder.array(tableName, userProperty.tablePrivileges.get(tableName).toArray());
        }
        builder.endObject();
    }
    builder.array("ip_whitelist", userProperty.ipWhiteList.toArray());
    builder.array("hostname_whitelist", userProperty.hostnameWhiteList.toArray());
    builder.array("hostip_whitelist", userProperty.hostIpWhiteList.toArray());
    builder.endObject();
}
 
Example 2
Source File: TransportInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields.TRANSPORT);
    builder.array(Fields.BOUND_ADDRESS, (Object[]) address.boundAddresses());
    builder.field(Fields.PUBLISH_ADDRESS, address.publishAddress().toString());
    builder.startObject(Fields.PROFILES);
    if (profileAddresses != null && profileAddresses.size() > 0) {
        for (Map.Entry<String, BoundTransportAddress> entry : profileAddresses.entrySet()) {
            builder.startObject(entry.getKey());
            builder.array(Fields.BOUND_ADDRESS, (Object[]) entry.getValue().boundAddresses());
            builder.field(Fields.PUBLISH_ADDRESS, entry.getValue().publishAddress().toString());
            builder.endObject();
        }
    }
    builder.endObject();
    builder.endObject();
    return builder;
}
 
Example 3
Source File: SearchGuardRolesMapping.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException{
    try {
        // output keys are names of mapping
        for (RolesMapping mapping : mappings.values()) {
            builder.startObject(mapping.getName());
            if(mapping.getExpire() != null) {
                builder.field(EXPIRES, mapping.getExpire());
            }
            builder.array(USER_HEADER, mapping.getUsers().toArray());
            if(!mapping.getBackendRoles().isEmpty()) {
                builder.array(BACKEND_ROLES, mapping.getBackendRoles().toArray());
            }
            builder.endObject();
        }
        return builder;
    } catch (IOException e) {
        throw new RuntimeException("Unable to convert the SearchGuardRolesMapping to JSON", e);
    }
}
 
Example 4
Source File: Manifest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.field(CURRENT_TERM_PARSE_FIELD.getPreferredName(), currentTerm);
    builder.field(CLUSTER_STATE_VERSION_PARSE_FIELD.getPreferredName(), clusterStateVersion);
    builder.field(GENERATION_PARSE_FIELD.getPreferredName(), globalGeneration);
    builder.array(INDEX_GENERATIONS_PARSE_FIELD.getPreferredName(), indexEntryList().toArray());
    return builder;
}
 
Example 5
Source File: HttpInfo.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields.HTTP);
    builder.array(Fields.BOUND_ADDRESS, (Object[]) address.boundAddresses());
    builder.field(Fields.PUBLISH_ADDRESS, address.publishAddress().toString());
    builder.humanReadableField(Fields.MAX_CONTENT_LENGTH_IN_BYTES, Fields.MAX_CONTENT_LENGTH, maxContentLength());
    builder.endObject();
    return builder;
}
 
Example 6
Source File: DocumentGroup.java    From elasticsearch-carrot2 with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params)
        throws IOException {
    builder.startObject();
    builder
        .field("id", id)
        .field("score", score)
        .field("label", getLabel())
        .array("phrases", phrases);
    
    if (ungroupedDocuments) {
        builder.field("other_topics", ungroupedDocuments);
    }

    if (documentReferences.length > 0) {
        builder.array("documents", documentReferences);
    }

    if (subgroups.length > 0) {
        builder.startArray("clusters");
        for (DocumentGroup group : subgroups) {
            group.toXContent(builder, params);
        }
        builder.endArray();
    }

    builder.endObject();
    return builder;
}
 
Example 7
Source File: SettingsContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createNGramAnalyzerSettings(XContentBuilder contentBuilder) throws IOException {
  contentBuilder.startObject(FieldConstants.NGRAM_ANALYZER);
  contentBuilder.field("type", "custom");
  contentBuilder.array(FILTER, "lowercase", "asciifolding");
  contentBuilder.field("tokenizer", "ngram");
  contentBuilder.endObject();
}
 
Example 8
Source File: SettingsContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createDefaultAnalyzerSettings(XContentBuilder contentBuilder) throws IOException {
  contentBuilder.startObject(FieldConstants.DEFAULT_ANALYZER);
  contentBuilder.field("type", "custom");
  contentBuilder.array(FILTER, "lowercase", "asciifolding", DEFAULT_STEMMER);
  contentBuilder.field("tokenizer", "standard");
  contentBuilder.field("char_filter", "html_strip");
  contentBuilder.endObject();
}
 
Example 9
Source File: StandardnumberMapper.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(simpleName());
    builder.field("type", MAPPER_TYPE);
    builder.array("standardnumbers", settings.getAsList("standardnumbers"));
    builder.endObject();
    return builder;
}
 
Example 10
Source File: SearchGuardRoles.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException{
    try {
        // output keys are names of roles
        for (Roles role : roles.values()) {
            builder.startObject(role.getName());
            if (!role.getCluster().isEmpty()) {
                builder.array(CLUSTER_HEADER, role.getCluster().toArray());
            }
            if(role.getExpire() != null) {
                builder.field(EXPIRES, role.getExpire());
            }
            if(!role.getIndices().isEmpty()) {
                builder.startObject(INDICES_HEADER);
                role.getIndices().sort(new Comparator<Indices>() {
                    @Override
                    public int compare(Indices o1, Indices o2) {
                        return o1.getIndex().compareTo(o2.getIndex());
                    }
                });
                for (Indices index : role.getIndices()) {
                    if(!index.getTypes().isEmpty()) {
                        builder.startObject(index.getIndex());
                        for (Type type : index.getTypes()) {
                            builder.array(type.getType(), type.getActions().toArray());
                        }
                        builder.endObject();
                    }
                }
                builder.endObject();
            }
            builder.endObject();
        }
        return builder;
    } catch (IOException e) {
        throw new RuntimeException("Unable to convert the SearchGuardRoles to JSON", e);
    }
}
 
Example 11
Source File: GeoBoundingBoxQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
    // check values
    if(Double.isNaN(box[TOP])) {
        throw new IllegalArgumentException("geo_bounding_box requires top latitude to be set");
    } else if(Double.isNaN(box[BOTTOM])) {
        throw new IllegalArgumentException("geo_bounding_box requires bottom latitude to be set");
    } else if(Double.isNaN(box[RIGHT])) {
        throw new IllegalArgumentException("geo_bounding_box requires right longitude to be set");
    } else if(Double.isNaN(box[LEFT])) {
        throw new IllegalArgumentException("geo_bounding_box requires left longitude to be set");
    }

    builder.startObject(GeoBoundingBoxQueryParser.NAME);

    builder.startObject(name);
    builder.array(TOP_LEFT, box[LEFT], box[TOP]);
    builder.array(BOTTOM_RIGHT, box[RIGHT], box[BOTTOM]);
    builder.endObject();

    if (queryName != null) {
        builder.field("_name", queryName);
    }
    if (type != null) {
        builder.field("type", type);
    }
    if (coerce != null) {
        builder.field("coerce", coerce);
    }
    if (ignoreMalformed != null) {
        builder.field("ignore_malformed", ignoreMalformed);
    }

    builder.endObject();
}
 
Example 12
Source File: HttpInfo.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields.HTTP);
    builder.array(Fields.BOUND_ADDRESS, (Object[]) address.boundAddresses());
    builder.field(Fields.PUBLISH_ADDRESS, address.publishAddress().toString());
    builder.byteSizeField(Fields.MAX_CONTENT_LENGTH_IN_BYTES, Fields.MAX_CONTENT_LENGTH, maxContentLength);
    builder.endObject();
    return builder;
}
 
Example 13
Source File: TermsBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder doInternalXContent(XContentBuilder builder, Params params) throws IOException {

    bucketCountThresholds.toXContent(builder);

    if (showTermDocCountError != null) {
        builder.field(AbstractTermsParametersParser.SHOW_TERM_DOC_COUNT_ERROR.getPreferredName(), showTermDocCountError);
    }
    if (executionHint != null) {
        builder.field(AbstractTermsParametersParser.EXECUTION_HINT_FIELD_NAME.getPreferredName(), executionHint);
    }
    if (valueType != null) {
        builder.field("value_type", valueType.name().toLowerCase(Locale.ROOT));
    }
    if (order != null) {
        builder.field("order");
        order.toXContent(builder, params);
    }
    if (collectionMode != null) {
        builder.field(SubAggCollectionMode.KEY.getPreferredName(), collectionMode.parseField().getPreferredName());
    }
    if (includeTerms != null) {
        builder.array("include", includeTerms);
    }
    if (includePattern != null) {
        builder.field("include", includePattern);
    }
    if (excludeTerms != null) {
        builder.array("exclude", excludeTerms);
    }
    if (excludePattern != null) {
        builder.field("exclude", excludePattern);
    }
    return builder;
}
 
Example 14
Source File: SQLBaseResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected void writeSharedAttributes(XContentBuilder builder) throws IOException {
    builder.array(Fields.COLS, cols);
    if (includeTypes) {
        builder.startArray(Fields.COLUMNTYPES);
        if (colTypes != null) {
            for (DataType colType : colTypes) {
                toXContentNestedDataType(builder, colType);
            }
        }
        builder.endArray();
    }

    builder.field(Fields.DURATION, duration());
}
 
Example 15
Source File: ModelResults.java    From elasticsearch-linear-regression with Apache License 2.0 4 votes vote down vote up
@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params)
    throws IOException {
  builder.array("coefficients", this.coefficients);
  return builder;
}
 
Example 16
Source File: ListAlgorithmsAction.java    From elasticsearch-carrot2 with Apache License 2.0 4 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params)
        throws IOException {
    return builder.array(Fields.ALGORITHMS, algorithms);
}
 
Example 17
Source File: StatusUpdaterBolt.java    From storm-crawler with Apache License 2.0 4 votes vote down vote up
@Override
public void store(String url, Status status, Metadata metadata,
        Date nextFetch, Tuple tuple) throws Exception {

    String sha256hex = org.apache.commons.codec.digest.DigestUtils
            .sha256Hex(url);

    // need to synchronize: otherwise it might get added to the cache
    // without having been sent to ES
    synchronized (waitAck) {
        // check that the same URL is not being sent to ES
        List<Tuple> alreadySent = waitAck.getIfPresent(sha256hex);
        if (alreadySent != null && status.equals(Status.DISCOVERED)) {
            // if this object is discovered - adding another version of it
            // won't make any difference
            LOG.debug(
                    "Already being sent to ES {} with status {} and ID {}",
                    url, status, sha256hex);
            // ack straight away!
            super.ack(tuple, url);
            return;
        }
    }

    XContentBuilder builder = jsonBuilder().startObject();
    builder.field("url", url);
    builder.field("status", status);

    // check that we don't overwrite an existing entry
    // When create is used, the index operation will fail if a document
    // by that id already exists in the index.
    boolean create = status.equals(Status.DISCOVERED);

    builder.startObject("metadata");
    Iterator<String> mdKeys = metadata.keySet().iterator();
    while (mdKeys.hasNext()) {
        String mdKey = mdKeys.next();
        String[] values = metadata.getValues(mdKey);
        // periods are not allowed in ES2 - replace with %2E
        mdKey = mdKey.replaceAll("\\.", "%2E");
        builder.array(mdKey, values);
    }

    String partitionKey = partitioner.getPartition(url, metadata);
    if (partitionKey == null) {
        partitionKey = "_DEFAULT_";
    }

    // store routing key in metadata?
    if (StringUtils.isNotBlank(fieldNameForRoutingKey)
            && routingFieldNameInMetadata) {
        builder.field(fieldNameForRoutingKey, partitionKey);
    }

    builder.endObject();

    // store routing key outside metadata?
    if (StringUtils.isNotBlank(fieldNameForRoutingKey)
            && !routingFieldNameInMetadata) {
        builder.field(fieldNameForRoutingKey, partitionKey);
    }

    builder.field("nextFetchDate", nextFetch);

    builder.endObject();

    IndexRequest request = new IndexRequest(getIndexName(metadata));
    request.source(builder).id(sha256hex).create(create);

    if (doRouting) {
        request.routing(partitionKey);
    }

    synchronized (waitAck) {
        List<Tuple> tt = waitAck.getIfPresent(sha256hex);
        if (tt == null) {
            tt = new LinkedList<>();
            waitAck.put(sha256hex, tt);
        }
        tt.add(tuple);
        LOG.debug("Added to waitAck {} with ID {} total {}", url,
                sha256hex, tt.size());
    }

    LOG.debug("Sending to ES buffer {} with ID {}", url, sha256hex);

    connection.getProcessor().add(request);
}
 
Example 18
Source File: IndexerBolt.java    From storm-crawler with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(Tuple tuple) {

    String url = tuple.getStringByField("url");

    // Distinguish the value used for indexing
    // from the one used for the status
    String normalisedurl = valueForURL(tuple);

    LOG.info("Indexing {} as {}", url, normalisedurl);

    Metadata metadata = (Metadata) tuple.getValueByField("metadata");
    String text = tuple.getStringByField("text");

    boolean keep = filterDocument(metadata);
    if (!keep) {
        LOG.info("Filtered {}", url);
        eventCounter.scope("Filtered").incrBy(1);
        // treat it as successfully processed even if
        // we do not index it
        _collector.emit(StatusStreamName, tuple, new Values(url, metadata,
                Status.FETCHED));
        _collector.ack(tuple);
        return;
    }

    String docID = org.apache.commons.codec.digest.DigestUtils
            .sha256Hex(normalisedurl);

    try {
        XContentBuilder builder = jsonBuilder().startObject();

        // display text of the document?
        if (fieldNameForText() != null) {
            builder.field(fieldNameForText(), trimText(text));
        }

        // send URL as field?
        if (fieldNameForURL() != null) {
            builder.field(fieldNameForURL(), normalisedurl);
        }

        // which metadata to display?
        Map<String, String[]> keyVals = filterMetadata(metadata);

        Iterator<String> iterator = keyVals.keySet().iterator();
        while (iterator.hasNext()) {
            String fieldName = iterator.next();
            String[] values = keyVals.get(fieldName);
            if (values.length == 1) {
                builder.field(fieldName, values[0]);
            } else if (values.length > 1) {
                builder.array(fieldName, values);
            }
        }

        builder.endObject();

        String sha256hex = org.apache.commons.codec.digest.DigestUtils
                .sha256Hex(normalisedurl);

        IndexRequest indexRequest = new IndexRequest(getIndexName(metadata))
                .source(builder).id(sha256hex);

        DocWriteRequest.OpType optype = DocWriteRequest.OpType.INDEX;

        if (create) {
            optype = DocWriteRequest.OpType.CREATE;
        }

        indexRequest.opType(optype);

        if (pipeline != null) {
            indexRequest.setPipeline(pipeline);
        }

        connection.getProcessor().add(indexRequest);

        eventCounter.scope("Indexed").incrBy(1);
        perSecMetrics.scope("Indexed").update(1);

        synchronized (waitAck) {
            waitAck.put(docID, tuple);
        }
    } catch (IOException e) {
        LOG.error("Error building document for ES", e);
        // do not send to status stream so that it gets replayed
        _collector.fail(tuple);
        if (docID != null) {
            synchronized (waitAck) {
                waitAck.invalidate(docID);
            }
        }
    }
}
 
Example 19
Source File: SignificantTermsBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (field != null) {
        builder.field("field", field);
    }
    bucketCountThresholds.toXContent(builder);
    if (executionHint != null) {
        builder.field(AbstractTermsParametersParser.EXECUTION_HINT_FIELD_NAME.getPreferredName(), executionHint);
    }
    if (includePattern != null) {
        if (includeFlags == 0) {
            builder.field("include", includePattern);
        } else {
            builder.startObject("include")
                    .field("pattern", includePattern)
                    .field("flags", includeFlags)
                    .endObject();
        }
    }
    if (includeTerms != null) {
        builder.array("include", includeTerms);
    }
    
    if (excludePattern != null) {
        if (excludeFlags == 0) {
            builder.field("exclude", excludePattern);
        } else {
            builder.startObject("exclude")
                    .field("pattern", excludePattern)
                    .field("flags", excludeFlags)
                    .endObject();
        }
    }
    if (excludeTerms != null) {
        builder.array("exclude", excludeTerms);
    }
    
    if (filterBuilder != null) {
        builder.field(SignificantTermsParametersParser.BACKGROUND_FILTER.getPreferredName());
        filterBuilder.toXContent(builder, params); 
    }
    if (significanceHeuristicBuilder != null) {
        significanceHeuristicBuilder.toXContent(builder, params);
    }

    return builder.endObject();
}