org.elasticsearch.client.RequestOptions Java Examples

The following examples show how to use org.elasticsearch.client.RequestOptions. 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: CaseController.java    From skywalking with Apache License 2.0 7 votes vote down vote up
private void search(String indexName) throws IOException {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery("author", "Marker"));
    sourceBuilder.from(0);
    sourceBuilder.size(10);

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices(indexName);
    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    int length = searchResponse.getHits().getHits().length;
    if (!(length > 0)) {
        String message = "elasticsearch search data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #2
Source File: BulkProcessorConfiguration.java    From ElasticUtils with MIT License 7 votes vote down vote up
public BulkProcessor build(final RestHighLevelClient client) {

        // Taken from:
        //
        //      * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-bulk.html#java-rest-high-document-bulk-processor
        //
        return BulkProcessor.builder((request, bulkListener) -> {
            client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
        }, listener)
                .setConcurrentRequests(options.getConcurrentRequests())
                .setBulkActions(options.getBulkActions())
                .setBulkSize(options.getBulkSize())
                .setFlushInterval(options.getFlushInterval())
                .setBackoffPolicy(options.getBackoffPolicy())
                .build();
    }
 
Example #3
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 #4
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteMappingsByDetectorUUID_successful() throws Exception {
    val id = "adsvade8^szx";
    val detectorUuid = UUID.randomUUID();
    val searchIndex = "2";
    val lookUpTime = 100;

    val deleteResponse = mockDeleteResponse(id);
    val searchResponse = mockSearchResponse(searchIndex, lookUpTime, detectorUuid.toString());
    when(legacyElasticSearchClient.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);
    when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(new DeleteResponse());
    repoUnderTest.deleteMappingsByDetectorUUID(detectorUuid);
    verify(legacyElasticSearchClient, atLeastOnce()).delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT));
    assertEquals(id, deleteResponse.getId());
    assertEquals(elasticSearchProperties.getIndexName(), deleteResponse.getIndex());
    assertEquals("DELETED", deleteResponse.getResult().toString());
}
 
Example #5
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 6 votes vote down vote up
private static AcknowledgedResponse internalUpdateMapping(RestHighLevelClient client, String indexName, IElasticSearchMapping mapping) throws IOException {

        // An update apparently only takes the "properties" field in Elasticsearch 7.0.
        //
        // So... First let's get a Map:
        Map<String, Object> objectMap = convertToMap(mapping);

        // Supress Warnings, because the cast is safe to do at this point (at least I hope so!):
        @SuppressWarnings("unchecked")
        Map<String, Object> innerMap = (Map<String, Object>) objectMap.get(mapping.getIndexType());

        final PutMappingRequest putMappingRequest = new PutMappingRequest(indexName)
                .source(innerMap);

        final AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);

        if(log.isDebugEnabled()) {
            log.debug("PutMappingResponse: isAcknowledged {}", putMappingResponse.isAcknowledged());
        }

        return putMappingResponse;
    }
 
Example #6
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void putSettings() throws IOException {
    ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest();
    String transientSettingKey =
        RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey();
    int transientSettingValue = 10;
    Settings transientSettings = Settings.builder()
            .put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES)
            .build();
    request.transientSettings(transientSettings);
    ClusterUpdateSettingsResponse response = client.cluster().putSettings(request, RequestOptions.DEFAULT);
    if (response == null) {
        String message = "elasticsearch put settings fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #7
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Test
public void findLastUpdated_successful() throws IOException {
    List<DetectorMapping> tagsList = new ArrayList<>();
    Map<String, String> tags = new HashMap<>();
    Long LastModifiedTimeInMillis = new Long(1554828886);
    Long CreatedTimeInMillis = new Long(1554828886);
    val searchIndex = "2";
    val lookUpTime = 100;
    val detectorUuid = "aeb4d849-847a-45c0-8312-dc0fcf22b639";
    int TimeinSeconds = 60;
    SearchResponse searchResponse = mockSearchResponse(searchIndex, lookUpTime, detectorUuid);
    when(legacyElasticSearchClient.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);
    tagsList = repoUnderTest.findLastUpdated(TimeinSeconds);
    verify(legacyElasticSearchClient, atLeastOnce()).search(any(SearchRequest.class), eq(RequestOptions.DEFAULT));
    assertNotNull("Response can't be null", tagsList);
    assertEquals(1, tagsList.size());
    assertEquals(UUID.fromString(detectorUuid), tagsList.get(0).getDetector().getUuid());
    assertEquals("test-user", tagsList.get(0).getUser().getId());
    assertEquals(LastModifiedTimeInMillis, Long.valueOf(tagsList.get(0).getLastModifiedTimeInMillis()));
    assertEquals(CreatedTimeInMillis, Long.valueOf(tagsList.get(0).getCreatedTimeInMillis()));
    assertTrue(tagsList.get(0).isEnabled());
}
 
Example #8
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Generated
public void updateIndexMappings(Set<String> newFieldMappings, String indexName, String docType) {
    PutMappingRequest request = new PutMappingRequest(indexName);

    Map<String, Object> type = new HashMap<>();
    type.put("type", "keyword");

    Map<String, Object> properties = new HashMap<>();
    newFieldMappings.forEach(field -> {
        properties.put(field, type);
    });

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("properties", properties);
    request.source(jsonMap);
    request.type(docType);

    try {
        legacyElasticSearchClient.indices().putMapping(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error("Error updating mappings", e);
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: LegacyDetectorRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Override
public void trustDetector(String uuid, Boolean trusted) {
    MDC.put("DetectorUuid", uuid);
    val updateRequest = new UpdateRequest(DETECTOR_INDEX, DETECTOR_DOC_TYPE, uuid);
    Date nowDate = DateUtil.now();
    String nowValue = DateUtil.toDateString(nowDate.toInstant());

    Map<String, Object> jsonMap = new HashMap<>();
    Map<String, Object> metaMap = new HashMap<>();
    metaMap.put("dateUpdated", nowDate);
    jsonMap.put("lastUpdateTimestamp", nowValue);
    jsonMap.put("trusted", trusted);
    jsonMap.put("meta", metaMap);
    updateRequest.doc(jsonMap);
    try {
        val updateResponse = legacyElasticSearchClient.update(updateRequest, RequestOptions.DEFAULT);
        if (elasticsearchUtil.checkNullResponse(updateResponse.getResult())) {
            throw new RecordNotFoundException("Invalid request: " + uuid);
        }
    } catch (IOException e) {
        log.error("Updating elastic search failed", e);
        throw new RuntimeException(e);
    } finally {
        MDC.remove("DetectorUuid");
    }
}
 
Example #10
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Generated
public Set<String> removeFieldsHavingExistingMapping(Set<String> fields, String indexName, String docType) {
    GetMappingsRequest request = new GetMappingsRequest();
    request.indices(indexName);

    try {
        GetMappingsResponse mappingsResponse = legacyElasticSearchClient.indices().getMapping(request, RequestOptions.DEFAULT);
        Map<String, String> mapProperties = ((Map<String, String>) mappingsResponse.getMappings()
                .get(indexName)
                .get(docType)
                .sourceAsMap().get("properties"));

        Set<String> mappedFields = new HashSet<>(mapProperties.keySet());
        fields.removeAll(mappedFields);
        return fields;
    } catch (IOException e) {
        log.error("Error finding mappings", e);
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: EsUtil.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return boolean
 * @Author pancm
 * @Description //批量删除数据
 * 根据ID进行批量删除
 * @Date 2019/3/21
 * @Param []
 **/
public static boolean deleteByIds(String index, String type, Set<String> ids) throws IOException {
    if (index == null || type == null || ids == null) {
        return true;
    }
    try {
        BulkRequest requestBulk = new BulkRequest();
        ids.forEach(id -> {
            DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
            requestBulk.add(deleteRequest);
        });
        // 同步删除
        client.bulk(requestBulk, RequestOptions.DEFAULT);
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return false;
}
 
Example #12
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * Calculate statistics about the Facts bound to Objects. For each Object specified in the statistics criteria it is
 * calculated how many Facts of each FactType are bound to the Object and when a Fact of that FactType was last added
 * and last seen.
 * <p>
 * Both 'currentUserID' (identifying the calling user) and 'availableOrganizationID' (identifying the Organizations
 * the calling user has access to) must be set in the statistics criteria in order to apply access control to Facts.
 * Only statistics for Objects bound to Facts accessible to the calling user will be returned, and only accessible
 * Facts will be included in the returned statistics.
 *
 * @param criteria Criteria to specify for which Objects statistics should be calculated
 * @return Result container with the calculated statistics for each Object
 */
public ObjectStatisticsContainer calculateObjectStatistics(ObjectStatisticsCriteria criteria) {
  if (criteria == null) return ObjectStatisticsContainer.builder().build();

  SearchResponse response;
  try {
    response = clientFactory.getClient().search(buildObjectStatisticsSearchRequest(criteria), RequestOptions.DEFAULT);
  } catch (ElasticsearchException | IOException ex) {
    throw logAndExit(ex, "Could not perform request to calculate Object statistics.");
  }

  if (response.status() != RestStatus.OK) {
    LOGGER.warning("Could not calculate Object statistics (response code %s).", response.status());
    return ObjectStatisticsContainer.builder().build();
  }

  ObjectStatisticsContainer result = retrieveObjectStatisticsResult(response);

  LOGGER.info("Successfully retrieved statistics for %d Objects.", result.getStatisticsCount());
  return result;
}
 
Example #13
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @return void
 * @Author pancm
 * @Description 模糊查询
 * @Date 2019/9/12
 * @Param []
 **/
private static void likeSearch() throws IOException {
    String type = "_doc";
    String index = "test1";
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices(index);
    searchRequest.types(type);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    /**
     *  SELECT * FROM p_test where  message like '%xu%';
     * */
    boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "*xu*"));
    searchSourceBuilder.query(boolQueryBuilder);
    System.out.println("模糊查询语句:" + searchSourceBuilder.toString());
    searchRequest.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    searchResponse.getHits().forEach(documentFields -> {
        System.out.println("模糊查询结果:" + documentFields.getSourceAsMap());
    });
    System.out.println("\n=================\n");
}
 
Example #14
Source File: EsUtil.java    From bookmark with MIT License 6 votes vote down vote up
/**
 * Description: 搜索
 *
 * @param index   index
 * @param builder 查询参数
 * @param c       结果类对象
 * @return java.util.ArrayList
 * @author fanxb
 * @date 2019/7/25 13:46
 */
public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) {
    if (!status) {
        return null;
    }
    SearchRequest request = new SearchRequest(index);
    request.source(builder);
    try {
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        List<T> res = new ArrayList<>(hits.length);
        for (SearchHit hit : hits) {
            res.add(JSON.parseObject(hit.getSourceAsString(), c));
        }
        return res;
    } catch (Exception e) {
        throw new EsException(e);
    }
}
 
Example #15
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Test
public void search_successful() throws IOException {
    List<DetectorMapping> tagsList = new ArrayList<>();
    Map<String, String> tags = new HashMap<>();
    Long LastModifiedTimeInMillis = new Long(1554828886);
    Long CreatedTimeInMillis = new Long(1554828886);
    val searchIndex = "2";
    val lookUpTime = 100;
    val detectorUuid = "aeb4d849-847a-45c0-8312-dc0fcf22b639";
    SearchMappingsRequest searchMappingsRequest = new SearchMappingsRequest();
    SearchResponse searchResponse = mockSearchResponse(searchIndex, lookUpTime, detectorUuid);
    when(legacyElasticSearchClient.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);
    Mockito.when(elasticsearchUtil.getSourceBuilder(any(QueryBuilder.class))).thenReturn(new SearchSourceBuilder());

    tagsList = repoUnderTest.search(searchMappingsRequest);
    verify(legacyElasticSearchClient, atLeastOnce()).search(any(SearchRequest.class), eq(RequestOptions.DEFAULT));
    assertNotNull("Response can't be null", tagsList);
    assertEquals(1, tagsList.size());
    assertEquals(UUID.fromString(detectorUuid), tagsList.get(0).getDetector().getUuid());
    assertEquals("test-user", tagsList.get(0).getUser().getId());
    assertEquals(LastModifiedTimeInMillis, Long.valueOf(tagsList.get(0).getLastModifiedTimeInMillis()));
    assertEquals(CreatedTimeInMillis, Long.valueOf(tagsList.get(0).getCreatedTimeInMillis()));
    assertTrue(tagsList.get(0).isEnabled());
}
 
Example #16
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
    final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
        .field("tags")
        .order(BucketOrder.count(false));

    final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
    final SearchRequest searchRequest = new SearchRequest().indices("blog")
        .source(builder);

    final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

    final Map<String, Aggregation> results = response.getAggregations()
        .asMap();
    final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");

    final List<String> keys = topTags.getBuckets()
        .stream()
        .map(MultiBucketsAggregation.Bucket::getKeyAsString)
        .collect(toList());
    assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
 
Example #17
Source File: ElasticSearchTypeImpl.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> analyze(AnalyzeRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    try {
        var analyzeRequest = org.elasticsearch.client.indices.AnalyzeRequest.withIndexAnalyzer(index, request.analyzer, request.text);
        AnalyzeResponse response = elasticSearch.client().indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
        return response.getTokens().stream().map(AnalyzeResponse.AnalyzeToken::getTerm).collect(Collectors.toList());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed);
        logger.debug("analyze, index={}, analyzer={}, elapsed={}", index, request.analyzer, elapsed);
        checkSlowOperation(elapsed);
    }
}
 
Example #18
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @Author pancm
 * @Description  范围查询
 * @Date  2019/9/30
 * @Param []
 * @return void
 **/
private static void rangeSearch() throws IOException{
    String type = "_doc";
    String index = "test1";
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // 设置查询条件
    sourceBuilder.query(QueryBuilders.rangeQuery("sendtime").gte("2019-01-01 00:00:00").lte("2019-12-31 23:59:59"));
    searchRequest.source(sourceBuilder);
    System.out.println("范围查询的DSL语句:"+sourceBuilder.toString());
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    // 结果
    searchResponse.getHits().forEach(hit -> {
        String string = hit.getSourceAsString();
        System.out.println("范围查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
}
 
Example #19
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 * 
 * @throws IOException
 */
private static void update() throws IOException {
	String type = "_doc";
	String index = "test1";
	// 唯一编号
	String id = "1";
	UpdateRequest upateRequest = new UpdateRequest();
	upateRequest.id(id);
	upateRequest.index(index);
	upateRequest.type(type);

	// 依旧可以使用Map这种集合作为更新条件
	Map<String, Object> jsonMap = new HashMap<>();
	jsonMap.put("uid", 12345);
	jsonMap.put("phone", 123456789019L);
	jsonMap.put("msgcode", 2);
	jsonMap.put("sendtime", "2019-03-14 01:57:04");
	jsonMap.put("message", "xuwujing study Elasticsearch");
	upateRequest.doc(jsonMap);
	// upsert 方法表示如果数据不存在,那么就新增一条
	upateRequest.docAsUpsert(true);
	client.update(upateRequest, RequestOptions.DEFAULT);
	System.out.println("更新成功!");

}
 
Example #20
Source File: ElasticsearchIndexInitializer.java    From staccato with Apache License 2.0 6 votes vote down vote up
private boolean createTemplate(CollectionMetadata collection) throws Exception {
    String searchAlias = collection.getId() + "-search";
    String indexTemplateName = collection.getId() + "-template";
    String indexTemplatePattern = collection.getId() + "-*";

    log.debug("Attempting to initialize template for collection '" + collection.getId() + "'");

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(indexTemplateName)
            .patterns(Arrays.asList(indexTemplatePattern))
            .alias(new Alias(searchAlias))
            .mapping("_doc", buildMappings(collection));

    client.indices().putTemplate(request, RequestOptions.DEFAULT);

    log.info("Template '" + indexTemplateName + "' for collection '" + collection.getId() + "' initialized");
    return true;
}
 
Example #21
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void update(String indexName) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, "1");
    Map<String, Object> parameters = singletonMap("title", "c++ programing.");
    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters);
    request.script(inline);

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    if (updateResponse.getVersion() != 2) {
        String message = "elasticsearch update data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #22
Source File: DeleteByQueryPerformer.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Mono<BulkResponse> deleteRetrievedIds(SearchResponse searchResponse, RoutingKey routingKey) {
    BulkRequest request = new BulkRequest();

    for (SearchHit hit : searchResponse.getHits()) {
        request.add(
            new DeleteRequest(aliasName.getValue())
                .type(NodeMappingFactory.DEFAULT_MAPPING_NAME)
                .id(hit.getId())
                .routing(routingKey.asString()));
    }

    return client.bulk(request, RequestOptions.DEFAULT);
}
 
Example #23
Source File: EsHighLevelRestTest2.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
	 * 用于更改正在运行的重索引、逐查询更新或逐查询删除任务的当前节流,或完全禁用任务的节流。
	 * @throws IOException
	 */
	private static void rethrottleByQuery() throws IOException {
		TaskId taskId=new TaskId("1");
		//用于更改正在运行的重索引、逐查询更新或逐查询删除任务的当前节流,或完全禁用任务的节流。
		//并且将请求将任务的节流更改为每秒100个请求
		RethrottleRequest request = new RethrottleRequest(taskId,100.0f);

		// 同步设置需要更改的流
//		client.reindexRethrottle(request, RequestOptions.DEFAULT);       
//		client.updateByQueryRethrottle(request, RequestOptions.DEFAULT); 
//		client.deleteByQueryRethrottle(request, RequestOptions.DEFAULT); 
		
		
		ActionListener<ListTasksResponse> listener = new ActionListener<ListTasksResponse>() {
		    @Override
		    public void onResponse(ListTasksResponse response) {
		        System.out.println("===="+response.getTasks().toString());
		    }

		    @Override
		    public void onFailure(Exception e) {
		    	 System.out.println("====---"+e.getMessage());
		    }
		};
		
		// 异步设置要更改的流
		client.reindexRethrottleAsync(request, RequestOptions.DEFAULT, listener);       
		client.updateByQueryRethrottleAsync(request, RequestOptions.DEFAULT, listener); 
		client.deleteByQueryRethrottleAsync(request, RequestOptions.DEFAULT, listener);

		System.out.println("已成功设置!");

	}
 
Example #24
Source File: IndexCreationFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void createIndexIfNeeded(ReactorElasticSearchClient client, IndexName indexName, XContentBuilder settings) throws IOException {
    try {
        client.indices()
            .create(
                new CreateIndexRequest(indexName.getValue())
                    .source(settings), RequestOptions.DEFAULT);
    } catch (ElasticsearchStatusException exception) {
        if (exception.getMessage().contains(INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE)) {
            LOGGER.info("Index [{}] already exists", indexName.getValue());
        } else {
            throw exception;
        }
    }
}
 
Example #25
Source File: ElasticSearchUtils.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Indexes a document.
 *
 * @param indexName
 * @param id          unique identifier of the document
 * @param document
 * @throws Exception
 */
public void indexDocument(String indexName, String id, String document) {
    IndexRequest request = new IndexRequest(indexName).id(id).source(document, XContentType.JSON);
    try {
        client.index(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new ElasticsearchException("Error indexing document");
    }
}
 
Example #26
Source File: ElasticSearchConnection.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
public static ElasticSearchConnection getConnection(Map stormConf,
        String boltType, BulkProcessor.Listener listener) {

    String flushIntervalString = ConfUtils.getString(stormConf,
            "es." + boltType + ".flushInterval", "5s");

    TimeValue flushInterval = TimeValue.parseTimeValue(flushIntervalString,
            TimeValue.timeValueSeconds(5), "flushInterval");

    int bulkActions = ConfUtils.getInt(stormConf,
            "es." + boltType + ".bulkActions", 50);

    int concurrentRequests = ConfUtils.getInt(stormConf,
            "es." + boltType + ".concurrentRequests", 1);

    RestHighLevelClient client = getClient(stormConf, boltType);

    boolean sniff = ConfUtils.getBoolean(stormConf,
            "es." + boltType + ".sniff", true);
    Sniffer sniffer = null;
    if (sniff) {
        sniffer = Sniffer.builder(client.getLowLevelClient()).build();
    }

    BulkProcessor bulkProcessor = BulkProcessor
            .builder((request, bulkListener) -> client.bulkAsync(request,
                    RequestOptions.DEFAULT, bulkListener), listener)
            .setFlushInterval(flushInterval).setBulkActions(bulkActions)
            .setConcurrentRequests(concurrentRequests).build();

    return new ElasticSearchConnection(client, bulkProcessor, sniffer);
}
 
Example #27
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * 删除索引
 *
 * @throws IOException
 */
private static void deleteIndex() throws IOException {
	String index = "userindex";
	DeleteIndexRequest  request = new DeleteIndexRequest(index);
	// 同步删除
	client.indices().delete(request,RequestOptions.DEFAULT);
	System.out.println("删除索引库成功!"+index);

}
 
Example #28
Source File: ElasticSearchUtils.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a document by its id.
 *
 * @param indexName
 *
 * @param id
 */
public void deleteDocument(String indexName, String id) {
    DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
    try {

        client.delete(deleteRequest, RequestOptions.DEFAULT);

    } catch (IOException e) {
        throw new ElasticsearchException("Error deleting document");
    }
}
 
Example #29
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void get(String indexName) throws IOException {
    GetRequest getRequest = new GetRequest(indexName, "1");
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);

    if (!getResponse.isExists()) {
        String message = "elasticsearch get data fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #30
Source File: ElasticsearchAdminHandler.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void createSchema() throws IOException {
    IndicesClient indices = getClient().indices();

    if (indices.exists(new GetIndexRequest(settings.getIndexId()), RequestOptions.DEFAULT)) {
        logger.info("Index {} already exists", settings.getIndexId());

        // update mapping
        Integer version = getCurrentVersion();
        logger.info("Elasticsearch schema version is {}", version);
        if (version == null) {
            throw new ConfigurationError("Database inconsistency. Metadata version not found in type %s",
                    MetadataDataMapping.METADATA_TYPE_NAME);
        }
        if (version != schemas.getSchemaVersion()) {
            throw new ConfigurationError(
                    "Database schema version inconsistency. Version numbers don't match. "
                            + "Database version number %d <-> Application version number %d",
                    version, schemas.getSchemaVersion());
        }
        addUuidToMetadataIfNeeded(settings.getUuid());
    } else {
        logger.info("Index {} not exists creating a new one now.", settings.getIndexId());
        // create metadata table and index table table
        Map<String, Object> mapping = new HashMap<>();
        mapping.put(MetadataDataMapping.METADATA_TYPE_NAME, schemas.getMetadataSchema());
        mapping.put(settings.getTypeId(), schemas.getSchema());
        CreateIndexResponse response = indices
                .create(new CreateIndexRequest(settings.getIndexId()).mapping(mapping), RequestOptions.DEFAULT);
        logger.debug("Created indices: {}", response);
        // insert metadata values
        createMetadataType(schemas.getSchemaVersion());
    }
}