Java Code Examples for org.elasticsearch.common.xcontent.XContentFactory#jsonBuilder()

The following examples show how to use org.elasticsearch.common.xcontent.XContentFactory#jsonBuilder() . 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: Test.java    From dht-spider with MIT License 6 votes vote down vote up
public static void createIndex() throws Exception{
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("message");
            {
                builder.field("type", "text");
                builder.field("analyzer", "ik_max_word");
                builder.field("search_analyzer", "ik_max_word");

            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    CreateIndexRequest request = new CreateIndexRequest("twitter");
    request.mapping(builder);
    client.indices().create(request, RequestOptions.DEFAULT);

}
 
Example 2
Source File: HTTPSpnegoAuthenticator.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
private XContentBuilder getNegotiateResponseBody() {
	try {
		XContentBuilder negotiateResponseBody = XContentFactory.jsonBuilder();
		negotiateResponseBody.startObject();
		negotiateResponseBody.field("error");
		negotiateResponseBody.startObject();
		negotiateResponseBody.field("header");
		negotiateResponseBody.startObject();
		negotiateResponseBody.field("WWW-Authenticate", "Negotiate");
		negotiateResponseBody.endObject();
		negotiateResponseBody.endObject();
		negotiateResponseBody.endObject();
		return negotiateResponseBody;
	} catch (Exception ex) {
		log.error("Can't construct response body", ex);
		return null;
	}
}
 
Example 3
Source File: AbstractXContentTestCaseTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testInsertRandomFieldsAndShuffle() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.field("field", 1);
    }
    builder.endObject();
    BytesReference insertRandomFieldsAndShuffle = RandomizedContext.current().runWithPrivateRandomness(1,
            () -> AbstractXContentTestCase.insertRandomFieldsAndShuffle(
                    BytesReference.bytes(builder),
                    XContentType.JSON,
                    true,
                    new String[] {},
                    null,
                    this::createParser));
    try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) {
        Map<String, Object> mapOrdered = parser.mapOrdered();
        assertThat(mapOrdered.size(), equalTo(2));
        assertThat(mapOrdered.keySet().iterator().next(), not(equalTo("field")));
    }
}
 
Example 4
Source File: UserDefinedFunctionsMetaDataTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserDefinedFunctionToXContentWithEmptyMetadata() throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();

    // reflects the logic used to process custom metadata in the cluster state
    builder.startObject();
    UserDefinedFunctionsMetaData functions = UserDefinedFunctionsMetaData.of();
    functions.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();

    XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
        xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(BytesReference.bytes(builder)));
    parser.nextToken(); // enter START_OBJECT
    UserDefinedFunctionsMetaData functions2 = UserDefinedFunctionsMetaData.fromXContent(parser);
    assertEquals(functions, functions2);
}
 
Example 5
Source File: MetricProfileRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean profilingExists(Map<String, String> tags) {
    try {
        List<BytesReference> refList = new ArrayList<>();
        XContentBuilder xContent = XContentFactory.jsonBuilder();
        xContent.map(tags);
        refList.add(BytesReference.bytes(xContent));

        PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder(PercolatorMetricProfiling.QUERY_KEYWORD,
                refList, XContentType.JSON);

        val boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(percolateQuery);
        val searchSourceBuilder = elasticsearchUtil.getSourceBuilder(boolQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(elasticSearchProperties.getConfig().getConnectionTimeout()));
        searchSourceBuilder.size(500);

        val searchRequest = new SearchRequest().source(searchSourceBuilder).indices(METRIC_PROFILING_INDEX);
        val searchResponse = legacyElasticSearchClient.search(searchRequest, RequestOptions.DEFAULT);

        return searchResponse.getHits().getHits().length > 0;
    } catch (IOException e) {
        log.error("Error ES lookup", e);
        throw new RuntimeException(e);
    }
}
 
Example 6
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 7
Source File: ElasticsearchResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new elasticsearch index and adds a mapping for the document type.
 * Previously existing indexes will be removed.
 *
 * @throws Exception
 *             the exception
 */
private void createIndex() throws Exception {

	boolean exists = client.admin().indices().prepareExists(mIndex).execute().actionGet().isExists();

	// remove preexisting index
	if (exists) {
		logger.log(Level.INFO, "Preexisting index " + mIndex + " will be removed.");
		DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest(mIndex))
				.actionGet();
		if (deleteResponse.isAcknowledged()) {
			logger.log(Level.INFO, "Preexisting index " + mIndex + " successfully removed.");
			exists = false;
		}
	}

	// create schema mapping from file
	logger.log(Level.INFO, "Index " + mIndex + " will be created.");
	String docMapping = new String(Files.readAllBytes(Paths.get(documentMappingFile)));

	XContentBuilder builder = XContentFactory.jsonBuilder();
	XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(docMapping.getBytes());
	parser.close();
	builder.copyCurrentStructure(parser);

	CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(mIndex);
	createIndexRequestBuilder.addMapping(DOCUMENT_TYPE, builder);
	createIndexRequestBuilder.execute().actionGet();

}
 
Example 8
Source File: IndexerQueryBuilder.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates an index request for given part
 *
 * @param partIteration
 * @return
 * @throws IOException
 */
public Update.Builder updateRequest(PartIteration partIteration) throws IOException {
    Map<String, String> contentInputs = textExtractor.getContentInputs(partIteration.getAttachedFiles());
    try (XContentBuilder xcb = XContentFactory.jsonBuilder()) {
        xcb.startObject()
                .field("doc_as_upsert", true)
                .startObject("doc");
        EntityMapper.partIterationToJSON(xcb, partIteration, contentInputs);
        xcb.endObject().endObject();
        return new Update.Builder(Strings.toString(xcb))
                .index(indicesUtils.getIndexName(partIteration.getWorkspaceId(), IndexerMapping.INDEX_PARTS))
                .type(IndexerMapping.TYPE)
                .id(indicesUtils.formatDocId(partIteration.getKey().toString()));
    }
}
 
Example 9
Source File: Utils.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
public static XContentBuilder createContent(EventDataInfo eventDataInfo) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.field("name", eventDataInfo.getName());
        builder.field("description", eventDataInfo.getDescription());
        builder.field("timeStamp", eventDataInfo.getTimeStamp());
        builder.startObject("geoLocation");
        {
            builder.field("lon", eventDataInfo.getGeoLocation().getLongitude());
            builder.field("lat", eventDataInfo.getGeoLocation().getLatitude());
        }
        builder.endObject();
        builder.startArray("related");
        {
            for (EventId event: eventDataInfo.getRelated()) {
                builder.startObject();
                {
                    builder.field("id", event.getId());
                }
                builder.endObject();
            }
        }
        builder.endArray();
    }
    builder.endObject();
    return builder;
}
 
Example 10
Source File: Utils.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
public static XContentBuilder createContent(EventData eventData) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.field("name", eventData.getName());
        builder.field("description", eventData.getDescription());
        builder.field("timeStamp", eventData.getTimeStamp());
        builder.startObject("geoLocation");
        {
            builder.field("lon", eventData.getGeoLocation().getLongitude());
            builder.field("lat", eventData.getGeoLocation().getLatitude());
        }
        builder.endObject();
        builder.startArray("related");
        {
            for (EventId event: eventData.getRelated()) {
                builder.startObject();
                {
                    builder.field("id", event.getId());
                }
                builder.endObject();
            }
        }
        builder.endArray();
    }
    builder.endObject();
    return builder;
}
 
Example 11
Source File: XContentTestUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> convertToMap(ToXContent part) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    part.toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
}
 
Example 12
Source File: ElasticSearchHelper.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static String convertRequestToJson(ActionRequest request) throws IOException {
    BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
    request.writeTo(bytesStreamOutput);

    XContentBuilder builder = XContentFactory.jsonBuilder(bytesStreamOutput);
    builder.prettyPrint();

//    builder.startObject();
//    builder.endObject();
    BytesArray bytesArray = builder.bytes().toBytesArray();
    return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
  }
 
Example 13
Source File: UserDefinedFunctionsMetaDataTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataTypeStreaming() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder();

    var type = new ArrayType<>(new ArrayType<>(DataTypes.STRING));
    UserDefinedFunctionMetaData.DataTypeXContent.toXContent(type, builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
        xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(BytesReference.bytes(builder)));
    parser.nextToken();  // enter START_OBJECT
    ArrayType type2 = (ArrayType) UserDefinedFunctionMetaData.DataTypeXContent.fromXContent(parser);
    assertTrue(type.equals(type2));
}
 
Example 14
Source File: GathererState.java    From elasticsearch-gatherer with Apache License 2.0 5 votes vote down vote up
private String generateSetting(List<JobEvent> values) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startArray();
    for (JobEvent value : values) {
        value.toXContent(builder, EMPTY_PARAMS);
    }
    builder.endArray();
    return builder.string();
}
 
Example 15
Source File: TransportBasedClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void setAggregations(Aggregations aggregations, ActionResponse actionResp) {
  // Only the result of the first aggregation is returned
  //
  final Aggregation agg = aggregations.asList().get(0);

  if (agg instanceof InternalMetricsAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
  } else if (agg instanceof InternalSingleBucketAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
  } else if (agg instanceof InternalMultiBucketAggregation) {
    final Set<String> headerKeys = new HashSet<>();
    final List<Map<String, Object>> buckets = new LinkedList<>();
    final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;

    for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
      try {
        final XContentBuilder builder = XContentFactory.jsonBuilder();
        bucket.toXContent(builder, null);
        actionResp.addAggregation(
            new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
      } catch (final IOException e) {
        // Ignored
      }
    }
  }
}
 
Example 16
Source File: CronTransportActionTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testNormal() throws IOException, JsonPathNotFoundException {
    CronRequest request = new CronRequest();

    CronNodeRequest nodeRequest = new CronNodeRequest();
    BytesStreamOutput nodeRequestOut = new BytesStreamOutput();
    nodeRequestOut.setVersion(Version.CURRENT);
    nodeRequest.writeTo(nodeRequestOut);
    StreamInput siNode = nodeRequestOut.bytes().streamInput();

    CronNodeRequest nodeResponseRead = new CronNodeRequest(siNode);

    CronNodeResponse nodeResponse1 = action.nodeOperation(nodeResponseRead);
    CronNodeResponse nodeResponse2 = action.nodeOperation(new CronNodeRequest());

    CronResponse response = action.newResponse(request, Arrays.asList(nodeResponse1, nodeResponse2), Collections.emptyList());

    assertEquals(2, response.getNodes().size());
    assertTrue(!response.hasFailures());

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    response.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();

    String json = Strings.toString(builder);
    Function<JsonElement, String> function = (s) -> {
        try {
            return JsonDeserializer.getTextValue(s, CronNodeResponse.NODE_ID);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        return null;
    };
    assertArrayEquals(
        JsonDeserializer.getArrayValue(json, function, CronResponse.NODES_JSON_KEY),
        new String[] { localNodeID, localNodeID }
    );
}
 
Example 17
Source File: FulltextAnalyzerResolver.java    From crate with Apache License 2.0 5 votes vote down vote up
public static BytesReference encodeSettings(Settings settings) {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        settings.toXContent(builder, new ToXContent.MapParams(Collections.emptyMap()));
        builder.endObject();
        return BytesReference.bytes(builder);
    } catch (IOException e) {
        // this is a memory stream so no real I/O happens and a IOException can't really happen at runtime
        throw new RuntimeException(e);
    }
}
 
Example 18
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doUserMappingCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_USER_INDEX, params.param("index"));
    final String type = params.param(
            TasteConstants.REQUEST_PARAM_USER_TYPE,
            TasteConstants.USER_TYPE);
    final String userIdField = params.param(
            TasteConstants.REQUEST_PARAM_USER_ID_FIELD,
            TasteConstants.USER_ID_FIELD);
    final String timestampField = params.param(
            TasteConstants.REQUEST_PARAM_TIMESTAMP_FIELD,
            TasteConstants.TIMESTAMP_FIELD);

    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final ClusterHealthResponse healthResponse = client
                .admin()
                .cluster()
                .prepareHealth(index)
                .setWaitForYellowStatus()
                .setTimeout(
                        params.param("timeout",
                                DEFAULT_HEALTH_REQUEST_TIMEOUT)).execute()
                .actionGet();
        if (healthResponse.isTimedOut()) {
            listener.onError(new OperationFailedException(
                    "Failed to create index: " + index + "/" + type));
        }

        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(type)//
                .startObject("properties")//

                // @timestamp
                .startObject(timestampField)//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // user_id
                .startObject(userIdField)//
                .field("type", "long")//
                .endObject()//

                // system_id
                .startObject("system_id")//
                .field("type", "string")//
                .field("index", "not_analyzed")//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();

        final PutMappingResponse mappingResponse = client.admin().indices()
                .preparePutMapping(index).setType(type).setSource(builder)
                .execute().actionGet();
        if (mappingResponse.isAcknowledged()) {
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        } else {
            listener.onError(new OperationFailedException(
                    "Failed to create mapping for " + index + "/" + type));
        }
    } catch (final Exception e) {
        listener.onError(e);
    }
}
 
Example 19
Source File: SimilarUsersHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
protected UserWriter createSimilarUsersWriter(final IndexInfo indexInfo,
        final Map<String, Object> rootSettings) {
    final UserWriter writer = new UserWriter(client,
            indexInfo.getUserSimilarityIndex(),
            indexInfo.getUserSimilarityType(), indexInfo.getUserIdField(),
            indexInfo.getMaxNumOfWriters());
    writer.setUserIdField(indexInfo.getUserIdField());
    writer.setUsersField(indexInfo.getUsersField());
    writer.setValueField(indexInfo.getValueField());
    writer.setTimestampField(indexInfo.getTimestampField());
    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(indexInfo.getUserSimilarityType())//
                .startObject("properties")//

                // @timestamp
                .startObject(indexInfo.getTimestampField())//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // user_id
                .startObject(indexInfo.getUserIdField())//
                .field("type", "long")//
                .endObject()//

                // users
                .startObject(indexInfo.getUsersField())//
                .startObject("properties")//

                // user_id
                .startObject(indexInfo.getUserIdField())//
                .field("type", "long")//
                .endObject()//

                // value
                .startObject(indexInfo.getValueField())//
                .field("type", "double")//
                .endObject()//

                .endObject()//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();
        writer.setMapping(builder);

        final Map<String, Object> writerSettings = SettingsUtils.get(
                rootSettings, "writer");
        final boolean verbose = SettingsUtils.get(writerSettings, "verbose",
                false);
        if (verbose) {
            writer.setVerbose(verbose);
            final int maxCacheSize = SettingsUtils.get(writerSettings,
                    "cache_size", 1000);
            final Cache<Long, Map<String, Object>> cache = CacheBuilder
                    .newBuilder().maximumSize(maxCacheSize).build();
            writer.setCache(cache);
        }

        writer.open();
    } catch (final IOException e) {
        logger.info("Failed to create a mapping {}/{}.", e,
                indexInfo.getReportIndex(), indexInfo.getReportType());
    }

    return writer;
}
 
Example 20
Source File: ESClient.java    From dht-spider with MIT License 4 votes vote down vote up
public  void createIndex() throws Exception{
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("name");
            {
                builder.field("type", "text");
                builder.field("analyzer", "ik_max_word");
                builder.field("search_analyzer", "ik_max_word");

            }
            builder.endObject();

            builder.startObject("infoHash");
            {
                builder.field("type", "keyword");

            }
            builder.endObject();

            builder.startObject("length");
            {
                builder.field("type", "long");

            }
            builder.endObject();

            builder.startObject("nameInfo");
            {
                builder.field("type", "text");

            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    CreateIndexRequest request = new CreateIndexRequest("torrent");
    request.mapping(builder);
    client.indices().create(request, RequestOptions.DEFAULT);

}