org.elasticsearch.common.xcontent.ToXContentObject Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.ToXContentObject. 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: AnomalyDetectorRestApiIT.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public void testGetDetectorWithAdJob() throws IOException {
    AnomalyDetector detector = createRandomAnomalyDetector(true, false);

    Response startAdJobResponse = TestHelpers
        .makeRequest(
            client(),
            "POST",
            TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getDetectorId() + "/_start",
            ImmutableMap.of(),
            "",
            null
        );

    assertEquals("Fail to start AD job", RestStatus.OK, restStatus(startAdJobResponse));

    ToXContentObject[] results = getAnomalyDetector(detector.getDetectorId(), true);
    assertEquals("Incorrect Location header", detector, results[0]);
    assertEquals("Incorrect detector job name", detector.getDetectorId(), ((AnomalyDetectorJob) results[1]).getName());
    assertTrue(((AnomalyDetectorJob) results[1]).isEnabled());

    results = getAnomalyDetector(detector.getDetectorId(), false);
    assertEquals("Incorrect Location header", detector, results[0]);
    assertEquals("Should not return detector job", null, results[1]);
}
 
Example #2
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static GetResponse createGetResponse(ToXContentObject o, String id) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    return new GetResponse(
        new GetResult(
            AnomalyDetector.ANOMALY_DETECTORS_INDEX,
            MapperService.SINGLE_MAPPING_NAME,
            id,
            UNASSIGNED_SEQ_NO,
            0,
            -1,
            true,
            BytesReference.bytes(content),
            Collections.emptyMap(),
            Collections.emptyMap()
        )
    );
}
 
Example #3
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createSearchResponse(ToXContentObject o) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    SearchHit[] hits = new SearchHit[1];
    hits[0] = new SearchHit(0).sourceRef(BytesReference.bytes(content));

    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(hits, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #4
Source File: AbstractSearchAction.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private RestResponseListener<SearchResponse> search(RestChannel channel, Class<T> clazz) {
    return new RestResponseListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse response) throws Exception {
            if (response.isTimedOut()) {
                return new BytesRestResponse(RestStatus.REQUEST_TIMEOUT, response.toString());
            }

            if (clazz == AnomalyDetector.class) {
                for (SearchHit hit : response.getHits()) {
                    XContentParser parser = XContentType.JSON
                        .xContent()
                        .createParser(
                            channel.request().getXContentRegistry(),
                            LoggingDeprecationHandler.INSTANCE,
                            hit.getSourceAsString()
                        );
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);

                    // write back id and version to anomaly detector object
                    ToXContentObject xContentObject = AnomalyDetector.parse(parser, hit.getId(), hit.getVersion());
                    XContentBuilder builder = xContentObject.toXContent(jsonBuilder(), EMPTY_PARAMS);
                    hit.sourceRef(BytesReference.bytes(builder));
                }
            }

            return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), EMPTY_PARAMS));
        }
    };
}
 
Example #5
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public ToXContentObject[] getAnomalyDetector(String detectorId, boolean returnJob) throws IOException {
    BasicHeader header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    return getAnomalyDetector(detectorId, header, returnJob);
}
 
Example #6
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public ToXContentObject[] getAnomalyDetector(String detectorId, BasicHeader header, boolean returnJob) throws IOException {
    Response response = TestHelpers
        .makeRequest(
            client(),
            "GET",
            TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "?job=" + returnJob,
            null,
            "",
            ImmutableList.of(header)
        );
    assertEquals("Unable to get anomaly detector " + detectorId, RestStatus.OK, restStatus(response));
    XContentParser parser = createAdParser(XContentType.JSON.xContent(), response.getEntity().getContent());
    parser.nextToken();
    XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);

    String id = null;
    Long version = null;
    AnomalyDetector detector = null;
    AnomalyDetectorJob detectorJob = null;
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        String fieldName = parser.currentName();
        parser.nextToken();
        switch (fieldName) {
            case "_id":
                id = parser.text();
                break;
            case "_version":
                version = parser.longValue();
                break;
            case "anomaly_detector":
                detector = AnomalyDetector.parse(parser);
                break;
            case "anomaly_detector_job":
                detectorJob = AnomalyDetectorJob.parse(parser);
                break;
        }
    }

    return new ToXContentObject[] {
        new AnomalyDetector(
            id,
            version,
            detector.getName(),
            detector.getDescription(),
            detector.getTimeField(),
            detector.getIndices(),
            detector.getFeatureAttributes(),
            detector.getFilterQuery(),
            detector.getDetectionInterval(),
            detector.getWindowDelay(),
            detector.getUiMetadata(),
            detector.getSchemaVersion(),
            detector.getLastUpdateTime()
        ),
        detectorJob };
}
 
Example #7
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
protected HttpEntity toHttpEntity(ToXContentObject object) throws IOException {
    return new StringEntity(toJsonString(object), APPLICATION_JSON);
}
 
Example #8
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
protected String toJsonString(ToXContentObject object) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    return TestHelpers.xContentBuilderToString(shuffleXContent(object.toXContent(builder, ToXContent.EMPTY_PARAMS)));
}