Skip to content

Commit

Permalink
Don't fail if index does not exist while getting folder content
Browse files Browse the repository at this point in the history
  • Loading branch information
dadoonet committed Oct 24, 2023
1 parent 1547da8 commit 785a6d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,33 @@ public Collection<String> getFileDirectory(String path)
}

Collection<String> files = new ArrayList<>();
ESSearchResponse response = client.search(
new ESSearchRequest()
.withIndex(settings.getElasticsearch().getIndex())
.withSize(REQUEST_SIZE)
.addStoredField("file.filename")
.withESQuery(new ESTermQuery("path.root", SignTool.sign(path))));

if (response.getHits() != null) {
for (ESSearchHit hit : response.getHits()) {
String name;
if (hit.getStoredFields() != null
&& hit.getStoredFields().get("file.filename") != null) {
// In case someone disabled _source which is not recommended
name = hit.getStoredFields().get("file.filename").get(0);
} else {
// Houston, we have a problem ! We can't get the old files from ES
logger.warn("Can't find stored field name to check existing filenames in path [{}]. " +
"Please set store: true on field [file.filename]", path);
throw new RuntimeException("Mapping is incorrect: please set stored: true on field [file.filename].");
try {
ESSearchResponse response = client.search(
new ESSearchRequest()
.withIndex(settings.getElasticsearch().getIndex())
.withSize(REQUEST_SIZE)
.addStoredField("file.filename")
.withESQuery(new ESTermQuery("path.root", SignTool.sign(path))));

if (response.getHits() != null) {
for (ESSearchHit hit : response.getHits()) {
String name;
if (hit.getStoredFields() != null
&& hit.getStoredFields().get("file.filename") != null) {
// In case someone disabled _source which is not recommended
name = hit.getStoredFields().get("file.filename").get(0);
} else {
// Houston, we have a problem ! We can't get the old files from ES
logger.warn("Can't find stored field name to check existing filenames in path [{}]. " +
"Please set store: true on field [file.filename]", path);
throw new RuntimeException("Mapping is incorrect: please set stored: true on field [file.filename].");
}
files.add(name);
}
files.add(name);
}
} catch (ElasticsearchClientException e) {
logger.debug("Index [{}] doesn't exist.", settings.getElasticsearch().getIndex());
}

logger.trace("We found: {}", files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,6 @@ public ESSearchResponse search(ESSearchRequest request) throws ElasticsearchClie
logger.trace("Elasticsearch query to run: {}", query);

try {

String response = httpPost(url, query, new AbstractMap.SimpleImmutableEntry<>("version", "true"));
ESSearchResponse esSearchResponse = new ESSearchResponse(response);

Expand Down

0 comments on commit 785a6d0

Please sign in to comment.