Skip to content

Commit

Permalink
Merge pull request #158 from aodn/bugs/read-vocabs-index-schema
Browse files Browse the repository at this point in the history
Update method that read the resource's files
  • Loading branch information
HavierD authored Oct 22, 2024
2 parents a476585 + 59fc68a commit d324ea0
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;


@Slf4j
Expand All @@ -37,25 +41,27 @@ protected void deleteIndexStore(String indexName) {
}
}

public void createIndexFromMappingJSONFile(String indexMappingFile, String indexName) {
public void createIndexFromMappingJSONFile(String indexMappingFile, String indexName) throws IOException {

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

Resource resource = new ClassPathResource("config_files/" + indexMappingFile);
InputStream fStream = resource.getInputStream();

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

log.info("Creating index: {}", indexName);
try (InputStream input = resource.getInputStream()) {
try ( BufferedReader reader = new BufferedReader(
new InputStreamReader(fStream)) ) {
CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index(indexName)
.withJson(input)
.withJson(reader)
);
CreateIndexResponse response = portalElasticsearchClient.indices().create(req);
log.info(response.toString());
}
catch (ElasticsearchException | IOException e) {
} catch (ElasticsearchException | IOException e) {
log.error("Failed to create index: {} | {}", indexName, e.getMessage());
throw new CreateIndexException("Failed to elastic index from schema file: " + indexName + " | " + e.getMessage());
}
Expand Down

0 comments on commit d324ea0

Please sign in to comment.