Java Code Examples for org.elasticsearch.action.get.GetResponse#isExists()

The following examples show how to use org.elasticsearch.action.get.GetResponse#isExists() . 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: ElasticsearchConsolePersistence.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public ConsoleV2 getV2(String id) {
    try {
        GetResponse result = connection.getClient()
                .prepareGet()
                .setIndex(INDEX_V2)
                .setType(TYPE)
                .setId(id)
                .execute()
                .actionGet();
        if (!result.isExists()) {
            return null;
        }
        return mapper.readValue(result.getSourceAsBytes(), ConsoleV2.class);
    } catch (Exception e) {
        throw new ConsolePersistenceException(id, "console get failed", e);
    }
}
 
Example 2
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * Retrieve an indexed Fact by its UUID. Returns NULL if Fact cannot be fetched from ElasticSearch.
 *
 * @param id UUID of indexed Fact
 * @return Indexed Fact or NULL if not available
 */
public FactDocument getFact(UUID id) {
  if (id == null) return null;
  GetResponse response;

  try {
    GetRequest request = new GetRequest(INDEX_NAME, TYPE_NAME, id.toString());
    response = clientFactory.getClient().get(request, RequestOptions.DEFAULT);
  } catch (ElasticsearchException | IOException ex) {
    throw logAndExit(ex, String.format("Could not perform request to fetch Fact with id = %s.", id));
  }

  if (response.isExists()) {
    LOGGER.info("Successfully fetched Fact with id = %s.", id);
    return decodeFactDocument(id, response.getSourceAsBytes());
  } else {
    // Fact isn't indexed in ElasticSearch, log warning and return null.
    LOGGER.warning("Could not fetch Fact with id = %s. Fact not indexed?", id);
    return null;
  }
}
 
Example 3
Source File: S3River.java    From es-amazon-s3-river with Apache License 2.0 6 votes vote down vote up
private boolean isStarted(){
   // Refresh index before querying it.
   client.admin().indices().prepareRefresh("_river").execute().actionGet();
   GetResponse isStartedGetResponse = client.prepareGet("_river", riverName().name(), "_s3status").execute().actionGet();
   try{
      if (!isStartedGetResponse.isExists()){
         XContentBuilder xb = jsonBuilder().startObject()
               .startObject("amazon-s3")
                  .field("feedname", feedDefinition.getFeedname())
                  .field("status", "STARTED").endObject()
               .endObject();
         client.prepareIndex("_river", riverName.name(), "_s3status").setSource(xb).execute();
         return true;
      } else {
         String status = (String)XContentMapValues.extractValue("amazon-s3.status", isStartedGetResponse.getSourceAsMap());
         if ("STOPPED".equals(status)){
            return false;
         }
      }
   } catch (Exception e){
      logger.warn("failed to get status for " + riverName().name() + ", throttling....", e);
   }
   return true;
}
 
Example 4
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
private void createUserID(final long userID) {
    final GetResponse getResponse = client
            .prepareGet(userIndex, userType, Long.toString(userID))
            .setRefresh(true).execute().actionGet();
    if (!getResponse.isExists()) {
        final Map<String, Object> source = new HashMap<>();
        source.put("system_id", Long.toString(userID));
        source.put(userIdField, userID);
        source.put(timestampField, new Date());
        final IndexResponse response = client
                .prepareIndex(userIndex, userType, Long.toString(userID))
                .setSource(source).setRefresh(true).execute().actionGet();
        if (!response.isCreated()) {
            throw new TasteException("Failed to create " + source);
        }
    }
}
 
Example 5
Source File: TransportCreateModelFromSetAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private void doStore(Task parentTask, GetResponse response, CreateModelFromSetRequest request,
                     ActionListener<CreateModelFromSetResponse> listener) {
    if (!response.isExists()) {
        throw new IllegalArgumentException("Stored feature set [" + request.getFeatureSetName() + "] does not exist");
    }
    if (request.getExpectedSetVersion() != null && request.getExpectedSetVersion() != response.getVersion()) {
        throw new IllegalArgumentException("Stored feature set [" + request.getFeatureSetName() + "]" +
                " has version [" + response.getVersion() + "] but [" + request.getExpectedSetVersion() + "] was expected.");
    }
    final StoredFeatureSet set;
    try {
        set = IndexFeatureStore.parse(StoredFeatureSet.class, StoredFeatureSet.TYPE, response.getSourceAsBytesRef());
    } catch(IOException ioe) {
        throw new IllegalStateException("Cannot parse stored feature set [" + request.getFeatureSetName() + "]", ioe);
    }
    // Model will be parsed & checked by TransportFeatureStoreAction
    StoredLtrModel model = new StoredLtrModel(request.getModelName(), set, request.getDefinition());
    FeatureStoreRequest featureStoreRequest = new FeatureStoreRequest(request.getStore(), model, FeatureStoreRequest.Action.CREATE);
    featureStoreRequest.setRouting(request.getRouting());
    featureStoreRequest.setParentTask(clusterService.localNode().getId(), parentTask.getId());
    featureStoreRequest.setValidation(request.getValidation());
    featureStoreAction.execute(featureStoreRequest, ActionListener.wrap(
            (r) -> listener.onResponse(new CreateModelFromSetResponse(r.getResponse())),
            listener::onFailure));

}
 
Example 6
Source File: ElasticSearchService.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
@Override
public Metadata getDocument(String indexName, String indexDocId) {
    log.debug("Get document in ElasticSearch [indexName: {}, indexDocId:{}]", indexName, indexDocId);

    // Validation
    ValidatorUtils.rejectIfEmpty("indexName", indexName);
    ValidatorUtils.rejectIfEmpty("indexDocId", indexDocId);

    // Format index
    indexName = indexName.toLowerCase();

    GetResponse response = client.prepareGet(indexName, DEFAULT_TYPE, indexDocId).get();

    log.trace("Get document in ElasticSearch [indexName: {}, indexDocId: {}] : response= {}", indexName, indexDocId,
            response);

    if (!response.isExists()) {
        throw new NotFoundException(
                "Document [indexName: " + indexName + ", indexDocId: " + indexDocId + "] not found");
    }

    return convert(response.getIndex(), response.getId(), response.getSource());
}
 
Example 7
Source File: TableMapStore.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public Table load(String key) {
    logger.info("Load called for: {}", key);
    GetResponse response = elasticsearchConnection.getClient()
            .prepareGet()
            .setIndex(TABLE_META_INDEX)
            .setType(TABLE_META_TYPE)
            .setId(key)
            .execute()
            .actionGet();
    if(!response.isExists()) {
        return null;
    }
    try {
        return objectMapper.readValue(response.getSourceAsBytes(), Table.class);
    } catch (Exception e) {
        throw new TableMapStoreException("Error getting data for table: " + key);
    }
}
 
Example 8
Source File: AnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void onGetAdJobResponseForWrite(GetResponse response, RestChannel channel, AnomalyDetectorFunction function) {
    if (response.isExists()) {
        String adJobId = response.getId();
        if (adJobId != null) {
            // check if AD job is running on the detector, if yes, we can't delete the detector
            try (XContentParser parser = RestHandlerUtils.createXContentParser(channel, response.getSourceAsBytesRef())) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
                AnomalyDetectorJob adJob = AnomalyDetectorJob.parse(parser);
                if (adJob.isEnabled()) {
                    channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, "Detector job is running: " + adJobId));
                    return;
                }
            } catch (IOException e) {
                String message = "Failed to parse anomaly detector job " + adJobId;
                logger.error(message, e);
                channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, message));
            }
        }
    }
    function.execute();
}
 
Example 9
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public String get(String workflowInstanceId, String fieldToGet) {
    String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
    GetRequest request = new GetRequest(workflowIndexName, docType, workflowInstanceId)
            .fetchSourceContext(new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY));
    GetResponse response = elasticSearchClient.get(request).actionGet();

    if (response.isExists()) {
        Map<String, Object> sourceAsMap = response.getSourceAsMap();
        if (sourceAsMap.get(fieldToGet) != null) {
            return sourceAsMap.get(fieldToGet).toString();
        }
    }

    LOGGER.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, workflowIndexName);
    return null;
}
 
Example 10
Source File: MultiGetDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void name() throws Exception {
    MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
            .add("twitter", "tweet", "1")
            .add("twitter", "tweet", "2", "3", "4")
            .add("another", "type", "foo")
            .get();
    for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
        GetResponse response = itemResponse.getResponse();
        if (response.isExists()) {
            String json = response.getSourceAsString();
        }
    }
}
 
Example 11
Source File: ElasticSearchController.java    From springboot-learn with MIT License 5 votes vote down vote up
@GetMapping("/get/book/novel")
public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) {
    if (id.isEmpty()) {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    // 查询结果
    GetResponse result = transportClient.prepareGet(INDEX, TYPE, id)
            .get();
    if (!result.isExists()) {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity(result.getSource(), HttpStatus.OK);
}
 
Example 12
Source File: S3River.java    From es-amazon-s3-river with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Long getLastScanTimeFromRiver(String lastScanTimeField){
   Long result = null;
   try {
      // Do something.
      client.admin().indices().prepareRefresh("_river").execute().actionGet();
      GetResponse lastSeqGetResponse = client.prepareGet("_river", riverName().name(),
            lastScanTimeField).execute().actionGet();
      if (lastSeqGetResponse.isExists()) {
         Map<String, Object> fsState = (Map<String, Object>) lastSeqGetResponse.getSourceAsMap().get("amazon-s3");

         if (fsState != null){
            Object lastScanTime= fsState.get(lastScanTimeField);
            if (lastScanTime != null){
               try{
                  result = Long.parseLong(lastScanTime.toString());
               } catch (NumberFormatException nfe){
                  logger.warn("Last recorded lastScanTime is not a Long {}", lastScanTime.toString());
               }
            }
         }
      } else {
         // This is first call, just log in debug mode.
         if (logger.isDebugEnabled()){
            logger.debug("{} doesn't exist", lastScanTimeField);
         }
      }
   } catch (Exception e) {
      logger.warn("failed to get _lastScanTimeField, throttling....", e);
   }

   if (logger.isDebugEnabled()){
      logger.debug("lastScanTimeField: {}", result);
   }
   return result;
}
 
Example 13
Source File: AbstractESTest.java    From elasticsearch-migration with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
protected boolean checkDocumentExists(String indexName, String type, String id) {
    final ActionFuture<GetResponse> response = client.get(new GetRequest(indexName, type, id));
    client.admin().indices().prepareFlush(indexName).setWaitIfOngoing(true).setForce(true).execute().get();
    final GetResponse getFields = response.get();

    assertThat("Response should be done", response.isDone(), is(true));
    return getFields.isExists();
}
 
Example 14
Source File: MetadataQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Service searchService(String serviceCode) throws IOException {
    GetResponse response = getClient().get(
        ServiceTraffic.INDEX_NAME, IDManager.ServiceID.buildId(serviceCode, NodeType.Normal));
    if (response.isExists()) {
        Service service = new Service();
        service.setId((String) response.getSource().get(ServiceTraffic.ENTITY_ID));
        service.setName((String) response.getSource().get(ServiceTraffic.NAME));
        return service;
    } else {
        return null;
    }
}
 
Example 15
Source File: IndexAnomalyDetectorJobActionHandler.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private void onGetAnomalyDetectorJobForWrite(GetResponse response, AnomalyDetectorJob job) throws IOException {
    if (response.isExists()) {
        try (XContentParser parser = createXContentParser(channel, response.getSourceAsBytesRef())) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            AnomalyDetectorJob currentAdJob = AnomalyDetectorJob.parse(parser);
            if (currentAdJob.isEnabled()) {
                channel.sendResponse(new BytesRestResponse(RestStatus.OK, "Anomaly detector job is already running: " + detectorId));
                return;
            } else {
                AnomalyDetectorJob newJob = new AnomalyDetectorJob(
                    job.getName(),
                    job.getSchedule(),
                    job.getWindowDelay(),
                    job.isEnabled(),
                    Instant.now(),
                    currentAdJob.getDisabledTime(),
                    Instant.now(),
                    job.getLockDurationSeconds()
                );
                indexAnomalyDetectorJob(newJob, null);
            }
        } catch (IOException e) {
            String message = "Failed to parse anomaly detector job " + job.getName();
            logger.error(message, e);
            channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, message));
        }
    } else {
        indexAnomalyDetectorJob(job, null);
    }
}
 
Example 16
Source File: ElasticsearchClient.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Set<String> existBulkInternal(String indexName, final Collection<String> ids) {
    if (ids == null || ids.size() == 0) return new HashSet<>();
    MultiGetResponse multiGetItemResponses = elasticsearchClient.prepareMultiGet()
            .add(indexName, null, ids)
            .get();
    Set<String> er = new HashSet<>();
    for (MultiGetItemResponse itemResponse : multiGetItemResponses) { 
        GetResponse response = itemResponse.getResponse();
        if (response.isExists()) {
            er.add(response.getId());
        }
    }
    return er;
}
 
Example 17
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Product getProduct(ElasticSearchIndexConfig config, Long productId)
{
    GetResponse getResponse = searchClientService.getClient().prepareGet(config.getIndexAliasName(), config.getDocumentType(), String.valueOf(productId))
                                                             .setFields(SearchDocumentFieldName.productDocumentFields)
                                                             .get();
    if(getResponse.isExists())
    {
        Product product = new Product();
        product.setId(Long.valueOf(getResponse.getId()));
        product.setTitle(getResponse.getField(SearchDocumentFieldName.TITLE.getFieldName()).getValue().toString());
        product.setDescription(getResponse.getField(SearchDocumentFieldName.DESCRIPTION.getFieldName()).getValue().toString());
        product.setSoldOut(Boolean.valueOf(getResponse.getField(SearchDocumentFieldName.SOLD_OUT.getFieldName()).getValue().toString()));
        product.setAvailableOn(SearchDateUtils.getFormattedDate(getResponse.getField(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()).getValue().toString()));
        product.setKeywords(getListFieldValueOrNull(getResponse.getField(SearchDocumentFieldName.KEYWORDS.getFieldName())));
        product.setPrice(BigDecimal.valueOf(Double.valueOf(getResponse.getField(SearchDocumentFieldName.PRICE.getFieldName()).getValue().toString())));
        product.setBoostFactor(Float.valueOf(getResponse.getField(SearchDocumentFieldName.BOOSTFACTOR.getFieldName()).getValue().toString()));
        GetField catField = getResponse.getField(SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName());
        if(catField !=null)
        {
            for (Object ListOfMapValues : catField.getValues())
            {
                for (final java.util.Map.Entry<String, String> entry : ((Map<String, String>)ListOfMapValues).entrySet())
                {
                    //Only main facet values should be set.key ending with a number.
                    if(entry.getKey().matches("^.*.facet$"))
                    {
                        product.addCategory(new Category(entry.getValue(), null, entry.getKey().split("\\.facet")[0]));
                    }
                }
            }
        }
        
        return product;
    }
    return null;
}
 
Example 18
Source File: FetchElasticsearch5.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

    synchronized (esClient) {
        if(esClient.get() == null) {
            super.setup(context);
        }
    }

    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
    final String docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue();
    final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
    final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());

    final ComponentLog logger = getLogger();
    try {

        logger.debug("Fetching {}/{}/{} from Elasticsearch", new Object[]{index, docType, docId});
        GetRequestBuilder getRequestBuilder = esClient.get().prepareGet(index, docType, docId);
        final GetResponse getResponse = getRequestBuilder.execute().actionGet();

        if (getResponse == null || !getResponse.isExists()) {
            logger.warn("Failed to read {}/{}/{} from Elasticsearch: Document not found",
                    new Object[]{index, docType, docId});

            // We couldn't find the document, so penalize it and send it to "not found"
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_NOT_FOUND);
        } else {
            flowFile = session.putAllAttributes(flowFile, new HashMap<String, String>() {{
                put("filename", docId);
                put("es.index", index);
                put("es.type", docType);
            }});
            flowFile = session.write(flowFile, new OutputStreamCallback() {
                @Override
                public void process(OutputStream out) throws IOException {
                    out.write(getResponse.getSourceAsString().getBytes(charset));
                }
            });
            logger.debug("Elasticsearch document " + docId + " fetched, routing to success");
            // The document is JSON, so update the MIME type of the flow file
            flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
            session.getProvenanceReporter().fetch(flowFile, getResponse.remoteAddress().getAddress());
            session.transfer(flowFile, REL_SUCCESS);
        }
    } catch (NoNodeAvailableException
            | ElasticsearchTimeoutException
            | ReceiveTimeoutTransportException
            | NodeClosedException exceptionToRetry) {
        logger.error("Failed to read into Elasticsearch due to {}, this may indicate an error in configuration "
                        + "(hosts, username/password, etc.), or this issue may be transient. Routing to retry",
                new Object[]{exceptionToRetry.getLocalizedMessage()}, exceptionToRetry);
        session.transfer(flowFile, REL_RETRY);
        context.yield();

    } catch (Exception e) {
        logger.error("Failed to read {} from Elasticsearch due to {}", new Object[]{flowFile, e.getLocalizedMessage()}, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
 
Example 19
Source File: FetchElasticsearch.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
    final String docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue();
    final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
    final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());

    final ComponentLog logger = getLogger();
    try {

        logger.debug("Fetching {}/{}/{} from Elasticsearch", new Object[]{index, docType, docId});
        final long startNanos = System.nanoTime();

        GetRequestBuilder getRequestBuilder = esClient.get().prepareGet(index, docType, docId);
        if (authToken != null) {
            getRequestBuilder.putHeader("Authorization", authToken);
        }
        final GetResponse getResponse = getRequestBuilder.execute().actionGet();

        if (getResponse == null || !getResponse.isExists()) {
            logger.debug("Failed to read {}/{}/{} from Elasticsearch: Document not found",
                    new Object[]{index, docType, docId});

            // We couldn't find the document, so penalize it and send it to "not found"
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_NOT_FOUND);
        } else {
            flowFile = session.putAttribute(flowFile, "filename", docId);
            flowFile = session.putAttribute(flowFile, "es.index", index);
            flowFile = session.putAttribute(flowFile, "es.type", docType);
            flowFile = session.write(flowFile, new OutputStreamCallback() {
                @Override
                public void process(OutputStream out) throws IOException {
                    out.write(getResponse.getSourceAsString().getBytes(charset));
                }
            });
            logger.debug("Elasticsearch document " + docId + " fetched, routing to success");

            final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
            final String uri = context.getProperty(HOSTS).evaluateAttributeExpressions().getValue() + "/" + index + "/" + docType + "/" + docId;
            session.getProvenanceReporter().fetch(flowFile, uri, millis);
            session.transfer(flowFile, REL_SUCCESS);
        }
    } catch (NoNodeAvailableException
            | ElasticsearchTimeoutException
            | ReceiveTimeoutTransportException
            | NodeClosedException exceptionToRetry) {
        logger.error("Failed to read into Elasticsearch due to {}, this may indicate an error in configuration "
                        + "(hosts, username/password, etc.). Routing to retry",
                new Object[]{exceptionToRetry.getLocalizedMessage()}, exceptionToRetry);
        session.transfer(flowFile, REL_RETRY);
        context.yield();

    } catch (Exception e) {
        logger.error("Failed to read {} from Elasticsearch due to {}", new Object[]{flowFile, e.getLocalizedMessage()}, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
 
Example 20
Source File: IndexAnomalyDetectorJobActionHandler.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
private void onGetAnomalyDetectorResponse(GetResponse response) throws IOException {
    if (!response.isExists()) {
        XContentBuilder builder = channel
            .newErrorBuilder()
            .startObject()
            .field("Message", "AnomalyDetector is not found with id: " + detectorId)
            .endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, response.toXContent(builder, EMPTY_PARAMS)));
        return;
    }
    try (XContentParser parser = RestHandlerUtils.createXContentParser(channel, response.getSourceAsBytesRef())) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId(), response.getVersion());

        if (detector.getFeatureAttributes().size() == 0) {
            channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, "Can't start detector job as no features configured"));
            return;
        }

        IntervalTimeConfiguration interval = (IntervalTimeConfiguration) detector.getDetectionInterval();
        Schedule schedule = new IntervalSchedule(Instant.now(), (int) interval.getInterval(), interval.getUnit());
        Duration duration = Duration.of(interval.getInterval(), interval.getUnit());

        AnomalyDetectorJob job = new AnomalyDetectorJob(
            detector.getDetectorId(),
            schedule,
            detector.getWindowDelay(),
            true,
            Instant.now(),
            null,
            Instant.now(),
            duration.getSeconds()
        );

        getAnomalyDetectorJobForWrite(job);
    } catch (IOException e) {
        String message = "Failed to parse anomaly detector job " + detectorId;
        logger.error(message, e);
        channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, message));
    }
}