Skip to content

Commit

Permalink
update resource reading method
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyengit committed Oct 22, 2024
1 parent 088c7d2 commit a1a67d0
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;


@Slf4j
Expand All @@ -37,20 +40,20 @@ protected void deleteIndexStore(String indexName) {
}
}

public void createIndexFromMappingJSONFile(String indexMappingFile, String indexName) {
public void createIndexFromMappingJSONFile(String indexMappingFile, String indexName) throws IOException {
// delete the existing index if found first
this.deleteIndexStore(indexName);

// AppConstants.PORTAL_RECORDS_MAPPING_JSON_FILE
log.info("Reading index schema definition from JSON file: {}", indexMappingFile);
ClassPathResource resource = new ClassPathResource("config_files/" + indexMappingFile);

// delete the existing index if found first
this.deleteIndexStore(indexName);

log.info("Creating index: {}", indexName);
try (InputStream input = resource.getInputStream()) {
// https://www.baeldung.com/java-classpath-resource-cannot-be-opened#resources
try (InputStream inputStream = getClass().getResourceAsStream("config_files/" + indexMappingFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream)))) {
log.info("Creating index: {}", indexName);
CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index(indexName)
.withJson(input)
.withJson(reader)
);
CreateIndexResponse response = portalElasticsearchClient.indices().create(req);
log.info(response.toString());
Expand Down

0 comments on commit a1a67d0

Please sign in to comment.