org.elasticsearch.action.DocWriteResponse Java Examples

The following examples show how to use org.elasticsearch.action.DocWriteResponse. 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: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 7 votes vote down vote up
@Override
protected void unlock(TransactionDefinition definition) {
    // 尝试解锁
    String key = definition.getName();
    Long value = definition.getMost().toEpochMilli();
    Map<String, Object> document = new HashMap<>();
    document.put(NAME, key);
    document.put(MOST, value);
    document.put(NOW, Instant.now().toEpochMilli());
    UpdateRequest request = new UpdateRequest().index(index).type(type)

            .id(key)

            .script(new Script(ScriptType.INLINE, SCRIPT, UNLOCK_SCRIPT, document));
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionUnlockException();
        }
    } catch (Exception exception) {
        throw new TransactionUnlockException(exception);
    }
}
 
Example #2
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    try {
        long startTime = Instant.now().toEpochMilli();
        DeleteRequest request = new DeleteRequest(workflowIndexName, WORKFLOW_DOC_TYPE, workflowId);
        DeleteResponse response = elasticSearchClient.delete(request).actionGet();
        if (response.getResult() == DocWriteResponse.Result.DELETED) {
            LOGGER.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        LOGGER.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Throwable e) {
        LOGGER.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(CLASS_NAME, "remove");
    }
}
 
Example #3
Source File: ElasticSearchSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void write(Record<byte[]> record) {
    KeyValue<String, byte[]> keyValue = extractKeyValue(record);
    IndexRequest indexRequest = Requests.indexRequest(elasticSearchConfig.getIndexName());
    indexRequest.type(elasticSearchConfig.getTypeName());
    indexRequest.source(keyValue.getValue(), XContentType.JSON);

    try {
    IndexResponse indexResponse = getClient().index(indexRequest);
        if (indexResponse.getResult().equals(DocWriteResponse.Result.CREATED)) {
            record.ack();
        } else {
            record.fail();
        }
    } catch (final IOException ex) {
        record.fail();
    }
}
 
Example #4
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * Index a Fact into ElasticSearch.
 *
 * @param fact Fact to index
 * @return Indexed Fact
 */
public FactDocument indexFact(FactDocument fact) {
  if (fact == null || fact.getId() == null) return null;
  IndexResponse response;

  try {
    IndexRequest request = new IndexRequest(INDEX_NAME, TYPE_NAME, fact.getId().toString())
            .setRefreshPolicy(isTestEnvironment ? WriteRequest.RefreshPolicy.IMMEDIATE : WriteRequest.RefreshPolicy.NONE)
            .source(FACT_DOCUMENT_WRITER.writeValueAsBytes(fact), XContentType.JSON);
    response = clientFactory.getClient().index(request, RequestOptions.DEFAULT);
  } catch (ElasticsearchException | IOException ex) {
    throw logAndExit(ex, String.format("Could not perform request to index Fact with id = %s.", fact.getId()));
  }

  if (response.status() != RestStatus.OK && response.status() != RestStatus.CREATED) {
    LOGGER.warning("Could not index Fact with id = %s.", fact.getId());
  } else if (response.getResult() == DocWriteResponse.Result.CREATED) {
    LOGGER.info("Successfully indexed Fact with id = %s.", fact.getId());
  } else if (response.getResult() == DocWriteResponse.Result.UPDATED) {
    LOGGER.info("Successfully re-indexed existing Fact with id = %s.", fact.getId());
  }

  return fact;
}
 
Example #5
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    try {
        long startTime = Instant.now().toEpochMilli();
        DeleteRequest request = new DeleteRequest(indexName, WORKFLOW_DOC_TYPE, workflowId);
        DeleteResponse response = elasticSearchClient.delete(request).actionGet();
        if (response.getResult() == DocWriteResponse.Result.DELETED) {
            logger.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        logger.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(className, "remove");
    }
}
 
Example #6
Source File: ElasticSearchTypeImpl.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(DeleteRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    boolean deleted = false;
    try {
        var deleteRequest = new org.elasticsearch.action.delete.DeleteRequest(index, request.id);
        DeleteResponse response = elasticSearch.client().delete(deleteRequest, RequestOptions.DEFAULT);
        deleted = response.getResult() == DocWriteResponse.Result.DELETED;
        return deleted;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, 0, deleted ? 1 : 0);
        logger.debug("delete, index={}, id={}, elapsed={}", index, request.id, elapsed);
        checkSlowOperation(elapsed);
    }
}
 
Example #7
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    long startTime = Instant.now().toEpochMilli();
    DeleteRequest request = new DeleteRequest(indexName, WORKFLOW_DOC_TYPE, workflowId);

    try {
        DeleteResponse response = elasticSearchClient.delete(request);

        if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
            logger.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        logger.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(className, "remove");
    }
}
 
Example #8
Source File: ElasticsearchBulkDocumentWriterTest.java    From metron with Apache License 2.0 6 votes vote down vote up
private void setupElasticsearchToSucceed() throws IOException {
    final String documentId = UUID.randomUUID().toString();
    final boolean isFailed = false;
    final int itemID = 0;

    // the write response will contain what is used as the document ID
    DocWriteResponse writeResponse = mock(DocWriteResponse.class);
    when(writeResponse.getId()).thenReturn(documentId);

    // define the item level response
    BulkItemResponse itemResponse = mock(BulkItemResponse.class);
    when(itemResponse.isFailed()).thenReturn(isFailed);
    when(itemResponse.getItemId()).thenReturn(itemID);
    when(itemResponse.getResponse()).thenReturn(writeResponse);
    List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);

    // define the bulk response to indicate success
    BulkResponse response = mock(BulkResponse.class);
    when(response.iterator()).thenReturn(itemsResponses.iterator());
    when(response.hasFailures()).thenReturn(isFailed);

    // have the client return the mock response
    when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
 
Example #9
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
private DeleteResponse mockDeleteResponse(String id) {
    DeleteResponse deleteResponse = mock(DeleteResponse.class);
    Result ResultOpt;
    when(deleteResponse.getId()).thenReturn(id);
    String indexName = elasticSearchProperties.getIndexName();
    when(deleteResponse.getIndex()).thenReturn(indexName);
    try {
        byte[] byteopt = new byte[]{2}; // 2 - DELETED, DeleteResponse.Result
        ByteBuffer byteBuffer = ByteBuffer.wrap(byteopt);
        ByteBufferStreamInput byteoptbytebufferstream = new ByteBufferStreamInput(byteBuffer);
        ResultOpt = DocWriteResponse.Result.readFrom(byteoptbytebufferstream);
        when(deleteResponse.getResult()).thenReturn(ResultOpt);
    } catch (IOException e) {
    }
    return deleteResponse;
}
 
Example #10
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    long startTime = Instant.now().toEpochMilli();
    String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
    DeleteRequest request = new DeleteRequest(workflowIndexName, docType, workflowId);

    try {
        DeleteResponse response = elasticSearchClient.delete(request);

        if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
            logger.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (IOException e) {
        logger.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(className, "remove");
    }
}
 
Example #11
Source File: LegacyDetectorRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
private DeleteResponse mockDeleteResponse(String id) {
    DeleteResponse deleteResponse = mock(DeleteResponse.class);
    Result ResultOpt;
    when(deleteResponse.getId()).thenReturn(id);
    String indexName = "index";
    when(deleteResponse.getIndex()).thenReturn(indexName);
    try {
        byte[] byteOpt = new byte[]{2}; // 2 - DELETED, DeleteResponse.Result
        ByteBuffer byteBuffer = ByteBuffer.wrap(byteOpt);
        ByteBufferStreamInput byteOptBufferStream = new ByteBufferStreamInput(byteBuffer);
        ResultOpt = DocWriteResponse.Result.readFrom(byteOptBufferStream);
        when(deleteResponse.getResult()).thenReturn(ResultOpt);
    } catch (IOException e) {
    }
    return deleteResponse;
}
 
Example #12
Source File: BaseIntegrationTest.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public FeatureStoreResponse addElement(StorableElement element,
                                       @Nullable FeatureValidation validation,
                                       String store) throws ExecutionException, InterruptedException {
    FeatureStoreRequestBuilder builder =
        new FeatureStoreRequestBuilder(client(), FeatureStoreAction.INSTANCE);
    builder.request().setStorableElement(element);
    builder.request().setAction(FeatureStoreAction.FeatureStoreRequest.Action.CREATE);
    builder.request().setStore(store);
    builder.request().setValidation(validation);
    FeatureStoreResponse response = builder.execute().get();
    assertEquals(1, response.getResponse().getVersion());
    assertEquals(IndexFeatureStore.ES_TYPE, response.getResponse().getType());
    assertEquals(DocWriteResponse.Result.CREATED, response.getResponse().getResult());
    assertEquals(element.id(), response.getResponse().getId());
    assertEquals(store, response.getResponse().getIndex());
    return response;
}
 
Example #13
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 存储通用对象
 *
 * @param esObject es通用存储数据结构
 * @return <code>true</code> 保存成功,<code>false</code>保存失败;
 */
public SearchBaseResult<Boolean> save(SaveESObject esObject) {
    try {
        final IndexRequestBuilder indexRequest = getIndexRequest(esObject);
        SearchLogger.log(indexRequest);
        if (esObject.isRefresh()) {
            indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        IndexResponse indexResponse = indexRequest.execute().get();
        SearchLogger.log(indexResponse);
        return SearchBaseResult.success(DocWriteResponse.Result.CREATED == indexResponse.getResult(), Boolean.class);
    } catch (Exception ex) {
        SearchLogger.error("save", ex);
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage());
    }
}
 
Example #14
Source File: AddFeaturesToSetActionIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testFailuresOnDuplicates() throws Exception {
    addElement(randomFeature("duplicated"));

    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("duplicated_set");
    builder.request().setFeatureNameQuery("duplicated*");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    AddFeaturesToSetResponse resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.CREATED, resp.getResponse().getResult());
    assertEquals(1, resp.getResponse().getVersion());


    AddFeaturesToSetRequestBuilder builder2= new AddFeaturesToSetRequestBuilder(client());
    builder2.request().setFeatureSet("duplicated_set");
    builder2.request().setFeatureNameQuery("duplicated");
    builder2.request().setStore(IndexFeatureStore.DEFAULT_STORE);

    Throwable iae = unwrap(expectThrows(ExecutionException.class, () -> builder2.execute().get()),
            IllegalArgumentException.class);
    assertNotNull(iae);
    assertThat(iae.getMessage(), containsString("defined twice in this set"));
}
 
Example #15
Source File: RestDeleteAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void deleteAnomalyDetectorJobDoc(NodeClient client, String detectorId, RestChannel channel) {
    logger.info("Delete anomaly detector job {}", detectorId);
    DeleteRequest deleteRequest = new DeleteRequest(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, detectorId)
        .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    client.delete(deleteRequest, ActionListener.wrap(response -> {
        if (response.getResult() == DocWriteResponse.Result.DELETED || response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
            deleteAnomalyDetectorDoc(client, detectorId, channel);
        } else {
            logger.error("Fail to delete anomaly detector job {}", detectorId);
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            deleteAnomalyDetectorDoc(client, detectorId, channel);
        } else {
            logger.error("Failed to delete anomaly detector job", exception);
            try {
                channel.sendResponse(new BytesRestResponse(channel, exception));
            } catch (IOException e) {
                logger.error("Failed to send response of delete anomaly detector job exception", e);
            }
        }
    }));
}
 
Example #16
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 根据文档唯一Id删除指定的文档
 *
 * @param esObject 删除参数
 * @return <code>true</code>文档删除成功,<code>true</code>未找到对应文档.
 */
public SearchBaseResult<Boolean> delete(final DeleteESObject esObject) {
    final DeleteRequestBuilder deleteRequest = getDeleteRequest(esObject);
    try {
        SearchLogger.log(deleteRequest);
        if (esObject.isRefresh()) {
            deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        DeleteResponse deleteResponse = deleteRequest.execute().actionGet();
        SearchLogger.log(deleteRequest);
        if (DocWriteResponse.Result.DELETED == deleteResponse.getResult()) {
            return SearchBaseResult.success(Boolean.TRUE, Boolean.class);
        }
        if (DocWriteResponse.Result.NOT_FOUND == deleteResponse.getResult()) {
            return SearchBaseResult.success(Boolean.FALSE, Boolean.class);
        }
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "ES返回结果不在预期范围内");
    } catch (Exception ex) {
        SearchLogger.error("delete", ex);
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + ex.getMessage());
    }

}
 
Example #17
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 *
 * @param esObject es通用更新请求参数
 * @return <code>true</code> 保存或更新成功; 否则更新失败
 */
public SearchBaseResult<Boolean> update(UpdateESObject esObject) {
    final UpdateRequestBuilder updateRequest = esObject.nestedUpdate() ? getNestedListUpdateRequest(esObject)
            : getUpdateRequest(esObject);
    SearchLogger.log(updateRequest);
    try {
        updateRequest.setDetectNoop(false);
        if (esObject.isRefresh()) {
            updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        UpdateResponse updateResponse = updateRequest.execute().get();
        SearchLogger.log(updateResponse);
        final DocWriteResponse.Result result = updateResponse.getResult();
        return SearchBaseResult.success(DocWriteResponse.Result.UPDATED == result, Boolean.class);
    } catch (Exception ex) {
        SearchLogger.error("update", ex);
        final String message = ex.getMessage();
        if (message != null && message.contains("document missing")) {
            return SearchBaseResult.faild(ESErrorCode.DOC_NOT_EXIST_ERROR_CODE, "更新文档不存在");
        }
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + message);
    }
}
 
Example #18
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean tagUntag(Project prj, String documentId, String rootDocument, Script untagScript) throws IOException {
    UpdateRequest update = new UpdateRequest(prj.getId(), esCfg.indexType, documentId).routing(rootDocument);
    update.script(untagScript);
    update.setRefreshPolicy(esCfg.refreshPolicy);
    UpdateResponse updateResponse = client.update(update);
    return updateResponse.status() == RestStatus.OK && updateResponse.getResult() == DocWriteResponse.Result.UPDATED;
}
 
Example #19
Source File: AddFeaturesToSetActionIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testAddToSetWithQuery() throws Exception {
    int nFeature = random().nextInt(99) + 1;
    List<StoredFeature> features = new ArrayList<>(nFeature);
    for (int i = 0; i < nFeature; i++) {
        StoredFeature feat = randomFeature("feature" + i);
        features.add(feat);
        addElement(feat);
    }

    addElement(randomFeature("another"));

    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatureNameQuery("feature*");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    AddFeaturesToSetResponse resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.CREATED, resp.getResponse().getResult());
    assertEquals(1, resp.getResponse().getVersion());
    StoredFeatureSet set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertNotNull(set);
    assertEquals("new_feature_set", set.name());
    assertEquals(features.size(), set.size());
    assertTrue(features.stream().map(StoredFeature::name).allMatch(set::hasFeature));

    builder = new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatureNameQuery("another");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    resp = builder.execute().get();
    assertEquals(2, resp.getResponse().getVersion());
    assertEquals(DocWriteResponse.Result.UPDATED, resp.getResponse().getResult());
    set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertTrue(set.hasFeature("another"));
}
 
Example #20
Source File: AddFeaturesToSetActionIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testAddToSetWithList() throws Exception {
    int nFeature = random().nextInt(99) + 1;
    List<StoredFeature> features = new ArrayList<>(nFeature);
    for (int i = 0; i < nFeature; i++) {
        StoredFeature feat = randomFeature("feature" + i);
        features.add(feat);
    }

    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatures(features);
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    AddFeaturesToSetResponse resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.CREATED, resp.getResponse().getResult());
    assertEquals(1, resp.getResponse().getVersion());
    StoredFeatureSet set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertNotNull(set);
    assertEquals("new_feature_set", set.name());
    assertEquals(features.size(), set.size());
    assertTrue(features.stream().map(StoredFeature::name).allMatch(set::hasFeature));

    builder = new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatures(Collections.singletonList(randomFeature("another_feature")));
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    resp = builder.execute().get();
    assertEquals(2, resp.getResponse().getVersion());
    assertEquals(DocWriteResponse.Result.UPDATED, resp.getResponse().getResult());
    set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertEquals(features.size()+1, set.size());
    assertTrue(set.hasFeature("another_feature"));
}
 
Example #21
Source File: AddFeaturesToSetActionIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testMergeWithQuery() throws Exception {
    addElement(randomFeature("duplicated"));
    addElement(randomFeature("new_feature"));

    AddFeaturesToSetRequestBuilder builder= new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("merged_set");
    builder.request().setFeatureNameQuery("duplicated*");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    builder.request().setMerge(true);
    AddFeaturesToSetResponse resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.CREATED, resp.getResponse().getResult());
    assertEquals(1, resp.getResponse().getVersion());

    builder= new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("merged_set");
    builder.request().setFeatureNameQuery("*");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    builder.request().setMerge(true);
    resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.UPDATED, resp.getResponse().getResult());
    assertEquals(2, resp.getResponse().getVersion());
    StoredFeatureSet set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "merged_set");
    assertEquals(2, set.size());
    assertEquals(0, set.featureOrdinal("duplicated"));
    assertEquals(1, set.featureOrdinal("new_feature"));
}
 
Example #22
Source File: AddFeaturesToSetActionIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testMergeWithList() throws Exception {
    int nFeature = random().nextInt(99) + 1;
    List<StoredFeature> features = new ArrayList<>(nFeature);
    for (int i = 0; i < nFeature; i++) {
        StoredFeature feat = randomFeature("feature" + i);
        features.add(feat);
    }

    AddFeaturesToSetRequestBuilder builder= new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatures(features);
    builder.request().setMerge(true);
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    AddFeaturesToSetResponse resp = builder.execute().get();

    assertEquals(DocWriteResponse.Result.CREATED, resp.getResponse().getResult());
    assertEquals(1, resp.getResponse().getVersion());
    StoredFeatureSet set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertNotNull(set);
    assertEquals("new_feature_set", set.name());
    assertEquals(features.size(), set.size());
    assertTrue(features.stream().map(StoredFeature::name).allMatch(set::hasFeature));

    builder= new AddFeaturesToSetRequestBuilder(client());
    builder.request().setFeatureSet("new_feature_set");
    builder.request().setFeatures(asList(randomFeature("another_feature"), randomFeature("feature0")));
    builder.request().setMerge(true);
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    resp = builder.execute().get();
    assertEquals(2, resp.getResponse().getVersion());
    assertEquals(DocWriteResponse.Result.UPDATED, resp.getResponse().getResult());
    set = getElement(StoredFeatureSet.class, StoredFeatureSet.TYPE, "new_feature_set");
    assertEquals(features.size()+1, set.size());
    assertTrue(set.hasFeature("another_feature"));
    assertEquals(0, set.featureOrdinal("feature0"));
}
 
Example #23
Source File: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    try {
        Map<String, Object> lockObject = lockObject(lockConfiguration.getName(),
            lockConfiguration.getLockAtMostUntil(),
            now());
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                UPDATE_SCRIPT,
                lockObject)
            )
            .upsert(lockObject)
            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        UpdateResponse res = highLevelClient.update(ur, RequestOptions.DEFAULT);
        if (res.getResult() != DocWriteResponse.Result.NOOP) {
            return Optional.of(new ElasticsearchSimpleLock(lockConfiguration));
        } else {
            return Optional.empty();
        }
    } catch (IOException | ElasticsearchException e) {
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            return Optional.empty();
        } else {
            throw new LockException("Unexpected exception occurred", e);
        }
    }
}
 
Example #24
Source File: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void lock(TransactionDefinition definition) {
    // 尝试加锁
    String key = definition.getName();
    Long value = definition.getMost().toEpochMilli();
    Map<String, Object> document = new HashMap<>();
    document.put(NAME, key);
    document.put(MOST, value);
    document.put(NOW, Instant.now().toEpochMilli());
    UpdateRequest request = new UpdateRequest().index(index).type(type)

            .id(key)

            .script(new Script(ScriptType.INLINE, SCRIPT, LOCK_SCRIPT, document))

            .upsert(document)

            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionLockException();
        }
    } catch (Exception exception) {
        throw new TransactionLockException(exception);
    }

}
 
Example #25
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RecordNotFoundException.class)
public void deleteDetectorMapping_illegal_args() throws Exception {
    val deleteResponse = mockDeleteResponse("1");
    Mockito.when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(deleteResponse);
    Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND);
    Mockito.when(elasticsearchUtil.checkNullResponse(deleteResponse.getResult())).thenReturn(true);
    repoUnderTest.deleteDetectorMapping("1");
}
 
Example #26
Source File: LegacyDetectorRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RecordNotFoundException.class)
public void trustDetector_illegal_args() {
    val updateResponse = mock(UpdateResponse.class);
    val result = updateResponse.getResult();
    Mockito.when(result).thenReturn(DocWriteResponse.Result.NOT_FOUND);
    Mockito.when(elasticsearchUtil.checkNullResponse(result)).thenReturn(true);
    repoUnderTest.trustDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true);
}
 
Example #27
Source File: LegacyDetectorRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RecordNotFoundException.class)
public void toggleDetector_illegal_args() {
    val updateResponse = mock(UpdateResponse.class);
    val result = updateResponse.getResult();
    Mockito.when(result).thenReturn(DocWriteResponse.Result.NOT_FOUND);
    Mockito.when(elasticsearchUtil.checkNullResponse(result)).thenReturn(true);
    repoUnderTest.toggleDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true);
}
 
Example #28
Source File: DataFormatPluginTest.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
private static void indexing() {
    final String index0 = "dataset0";
    final String type0 = "_doc";

    // create an index
    runner.createIndex(index0, (Settings) null);

    if (!runner.indexExists(index0)) {
        Assert.fail();
    }

    // create documents
    for (int i = 1; i <= docNumber; i++) {
        final IndexResponse indexResponse0 = runner.insert(index0, type0, String.valueOf(i),
                "{" +
                        "\"aaa\":\"test " + i + "\"," +
                        "\"bbb\":" + i + "," +
                        "\"ccc\":\"2012-01-01:00:00.000Z\"," +
                        "\"eee\":{\"fff\":\"TEST " + i + "\", \"ggg\":" + i + ", \"hhh\":\"2013-01-01:00:00.000Z\"}" +
                        "}");
        assertEquals(DocWriteResponse.Result.CREATED, indexResponse0.getResult());
    }
    runner.refresh();

    // search documents to verify
    SearchResponse searchResponse = runner.search(index0, type0, null, null, 0, 10);
    assertEquals(docNumber, searchResponse.getHits().getTotalHits().value);
}
 
Example #29
Source File: LegacyDetectorRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RecordNotFoundException.class)
public void updateDetector_illegal_args() {
    val updateResponse = mock(UpdateResponse.class);
    val result = updateResponse.getResult();
    val mom = ObjectMother.instance();
    val document = mom.buildDetectorDocument();
    document.setUuid(UUID.randomUUID());
    Mockito.when(result).thenReturn(DocWriteResponse.Result.NOT_FOUND);
    Mockito.when(elasticsearchUtil.checkNullResponse(result)).thenReturn(true);
    repoUnderTest.updateDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", document);
}
 
Example #30
Source File: TestESResponse.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Test
/**
 * Coverage purpose only. Data not verified.
 */
public void testUpdateResponse() {
    Index idx = new Index("idxName", "idxUuid");
    ShardId shardId = new ShardId(idx, 0);
    UpdateResponse updRes = new UpdateResponse(shardId, "doc", "id", 1L, DocWriteResponse.Result.NOT_FOUND);
    ESResponse esResp = new ESResponse(updRes);
    assertNotNull(esResp);
}