org.elasticsearch.client.indices.CreateIndexResponse Java Examples

The following examples show how to use org.elasticsearch.client.indices.CreateIndexResponse. 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: DataServiceIndexTestsIT.java    From java-11-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataServiceIndexCreateDelete() throws IOException {
    try {
        dataService.deleteIndex(INDEX_NAME);
    } catch (Exception e) {
    }

    Optional<CreateIndexResponse> createIndexResponse = dataService.createIndex(INDEX_NAME, Utils.createMapping());
    Assert.assertTrue(createIndexResponse.isPresent());
    Assert.assertTrue(createIndexResponse.get().isAcknowledged());

    Optional<Boolean> isCreated = dataService.isIndexCreated(INDEX_NAME);
    Assert.assertNotNull(isCreated.isPresent());
    Assert.assertTrue(isCreated.get());

    Optional<AcknowledgedResponse> deleteIndexResponse = dataService.deleteIndex(INDEX_NAME);
    Assert.assertTrue(deleteIndexResponse.isPresent());
    Assert.assertTrue(deleteIndexResponse.get().isAcknowledged());

    isCreated = dataService.isIndexCreated(INDEX_NAME);
    Assert.assertNotNull(isCreated.isPresent());
    Assert.assertFalse(isCreated.get());
}
 
Example #2
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
private void createIndex() {
  CreateIndexResponse response;

  try (InputStream payload = FactSearchManager.class.getClassLoader().getResourceAsStream(MAPPINGS_JSON);
       InputStreamReader reader = new InputStreamReader(payload)) {
    CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME)
            .source(CharStreams.toString(reader), XContentType.JSON);
    response = clientFactory.getClient().indices().create(request, RequestOptions.DEFAULT);
  } catch (ElasticsearchException | IOException ex) {
    throw logAndExit(ex, "Could not perform request to create index.");
  }

  if (!response.isAcknowledged()) {
    String msg = String.format("Could not create index '%s'.", INDEX_NAME);
    LOGGER.error(msg);
    throw new IllegalStateException(msg);
  }

  LOGGER.info("Successfully created index '%s'.", INDEX_NAME);
}
 
Example #3
Source File: KibanaImporter.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
public void importJson(String jsonString) throws JsonParseException, JsonMappingException, IOException {
    Objects.requireNonNull(jsonString);

    // delete .kibana index
    try {
        client.delete(new DeleteRequest(kibanaIndexName), RequestOptions.DEFAULT);
    } catch (ElasticsearchException ex) {
        LOG.debug("Tried to delete kibana index " + kibanaIndexName + " but it is not exists", ex);
    }

    ObjectMapper mapper = new ObjectMapper();
    KibanaConfigHolderDto holder = mapper.readValue(jsonString, KibanaConfigHolderDto.class);

    for (KibanaConfigEntryDto dto : holder.getEntries()) {
        processDto(dto);
        LOG.debug("Importing {}", dto);
        CreateIndexResponse response =
                client.indices().create(new CreateIndexRequest(kibanaIndexName), RequestOptions.DEFAULT);
        // client.prepareIndex(kibanaIndexName, dto.getType(), dto.getId())
        // .setSource(dto.getSource()).get();
    }
}
 
Example #4
Source File: DataServiceImpl.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CreateIndexResponse> createIndex(String indexName, XContentBuilder mapping) {
    try {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        createIndexRequest.mapping(mapping);
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        return Optional.of(createIndexResponse);
    } catch (IOException e) {
        return Optional.empty();
    }
}
 
Example #5
Source File: DataServiceDocumentTestsIT.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void testInit() throws IOException {
    try {
        dataService.deleteIndex(INDEX_NAME);
    } catch (Exception e) {
    }
    Optional<CreateIndexResponse> createIndexResponse = dataService.createIndex(INDEX_NAME, Utils.createMapping());
    Assert.assertTrue(createIndexResponse.isPresent());
    Assert.assertTrue(createIndexResponse.get().isAcknowledged());
}
 
Example #6
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());
    }
}
 
Example #7
Source File: BaseElasticsearchService.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * create elasticsearch index (asyc)
 *
 * @param index elasticsearch index
 * @author fxbin
 */
protected void createIndexRequest(String index) {
    try {
        CreateIndexRequest request = new CreateIndexRequest(index);
        // Settings for this index
        request.settings(Settings.builder().put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()).put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas()));

        CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS);

        log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged());
        log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", createIndexResponse.isShardsAcknowledged());
    } catch (IOException e) {
        throw new ElasticsearchException("创建索引 {" + index + "} 失败");
    }
}
 
Example #8
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName) throws IOException {
    indexName = formatIndexName(indexName);

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example #9
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName, Map<String, Object> settings,
                           Map<String, Object> mapping) throws IOException {
    indexName = formatIndexName(indexName);
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(settings);
    request.mapping(mapping);
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example #10
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void createIndex(String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("author");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
            builder.startObject("title");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    request.mapping(builder);

    request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));

    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    if (createIndexResponse.isAcknowledged() == false) {
        String message = "elasticsearch create index fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #11
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void createIndex(RestHighLevelClient client, String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("author");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
            builder.startObject("title");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    request.mapping(builder);

    request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));

    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    if (createIndexResponse.isAcknowledged() == false) {
        String message = "elasticsearch create index fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #12
Source File: ElasticVindClient.java    From vind with Apache License 2.0 4 votes vote down vote up
public CreateIndexResponse createIndex(String indexName) throws IOException {
    return client.indices().create(ElasticRequestUtils.getCreateIndexRequest(indexName), RequestOptions.DEFAULT);
}
 
Example #13
Source File: ElasticsearchIndexManager.java    From syncope with Apache License 2.0 4 votes vote down vote up
public void createIndex(final String domain, final AnyTypeKind kind)
        throws InterruptedException, ExecutionException, IOException {

    XContentBuilder settings = XContentFactory.jsonBuilder().
            startObject().
            startObject("analysis").
            startObject("analyzer").
            startObject("string_lowercase").
            field("type", "custom").
            field("tokenizer", "standard").
            field("filter").
            startArray().
            value("lowercase").
            endArray().
            endObject().
            endObject().
            endObject().
            startObject("index").
            field("number_of_shards", elasticsearchUtils.getNumberOfShards()).
            field("number_of_replicas", elasticsearchUtils.getNumberOfReplicas()).
            endObject().
            endObject();

    XContentBuilder mapping = XContentFactory.jsonBuilder().
            startObject().
            startArray("dynamic_templates").
            startObject().
            startObject("strings").
            field("match_mapping_type", "string").
            startObject("mapping").
            field("type", "keyword").
            field("analyzer", "string_lowercase").
            endObject().
            endObject().
            endObject().
            endArray().
            endObject();

    CreateIndexResponse response = client.indices().create(
            new CreateIndexRequest(ElasticsearchUtils.getContextDomainName(domain, kind)).
                    settings(settings).
                    mapping(mapping), RequestOptions.DEFAULT);
    LOG.debug("Successfully created {} for {}: {}",
            ElasticsearchUtils.getContextDomainName(domain, kind), kind.name(), response);
}
 
Example #14
Source File: DataAdmin.java    From java-11-examples with Apache License 2.0 2 votes vote down vote up
/**
 * Create index with mapping.
 * @param indexName
 * @param mapping
 * @return
 */
Optional<CreateIndexResponse> createIndex(String indexName, XContentBuilder mapping);