Skip to content

Commit

Permalink
Merge pull request elastic#2 from carlosdelest/carlosdelest/bug-fix-a…
Browse files Browse the repository at this point in the history
…pp-not-found-suggestions

Bugfix suggestions from @carlosdelest
  • Loading branch information
kderusso committed Mar 9, 2023
2 parents 6ddb2d3 + 5757c75 commit 066b8df
Showing 1 changed file with 52 additions and 52 deletions.
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
Expand Down Expand Up @@ -75,6 +74,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
Expand Down Expand Up @@ -186,27 +186,15 @@ private static XContentBuilder getIndexMappings() {
*/
public void getSearchApplication(String resourceName, ActionListener<SearchApplication> listener) {
final GetRequest getRequest = new GetRequest(SEARCH_APPLICATION_ALIAS_NAME).id(resourceName).realtime(true);
clientWithOrigin.get(getRequest, new ActionListener<>() {
@Override
public void onResponse(GetResponse getResponse) {
if (getResponse.isExists() == false) {
listener.onFailure(new ResourceNotFoundException(resourceName));
return;
}
final BytesReference source = getResponse.getSourceInternal();
final SearchApplication res = parseSearchApplicationBinaryFromSource(source);
listener.onResponse(res);
}

@Override
public void onFailure(Exception e) {
if (e instanceof IndexNotFoundException) {
listener.onFailure(new ResourceNotFoundException(resourceName));
return;
}
listener.onFailure(e);
clientWithOrigin.get(getRequest, new DelegatingIndexNotFoundActionListener<>(resourceName, listener, (l, getResponse) -> {
if (getResponse.isExists() == false) {
l.onFailure(new ResourceNotFoundException(resourceName));
return;
}
});
final BytesReference source = getResponse.getSourceInternal();
final SearchApplication res = parseSearchApplicationBinaryFromSource(source);
l.onResponse(res);
}));
}

private static String getSearchAliasName(SearchApplication app) {
Expand Down Expand Up @@ -306,25 +294,16 @@ private void deleteSearchApplication(String resourceName, ActionListener<DeleteR
try {
final DeleteRequest deleteRequest = new DeleteRequest(SEARCH_APPLICATION_ALIAS_NAME).id(resourceName)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
clientWithOrigin.delete(deleteRequest, new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
clientWithOrigin.delete(
deleteRequest,
new DelegatingIndexNotFoundActionListener<>(resourceName, listener, (l, deleteResponse) -> {
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
listener.onFailure(new ResourceNotFoundException(resourceName));
l.onFailure(new ResourceNotFoundException(resourceName));
return;
}
listener.onResponse(deleteResponse);
}

@Override
public void onFailure(Exception e) {
if (e instanceof IndexNotFoundException) {
listener.onFailure(new ResourceNotFoundException(resourceName));
return;
}
listener.onFailure(e);
}
});
l.onResponse(deleteResponse);
})
);
} catch (Exception e) {
listener.onFailure(e);
}
Expand All @@ -339,21 +318,16 @@ private void removeAlias(String searchAliasName, ActionListener<AcknowledgedResp
IndicesAliasesRequest.AliasActions.remove().aliases(searchAliasName).indices("*")
);

clientWithOrigin.admin().indices().aliases(aliasesRequest, new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
listener.onResponse(AcknowledgedResponse.TRUE);
}

@Override
public void onFailure(Exception e) {
if (e instanceof IndexNotFoundException) {
listener.onFailure(new ResourceNotFoundException(searchAliasName));
return;
}
listener.onFailure(e);
}
});
clientWithOrigin.admin()
.indices()
.aliases(
aliasesRequest,
new DelegatingIndexNotFoundActionListener<>(
searchAliasName,
listener,
(l, acknowledgedResponse) -> l.onResponse(AcknowledgedResponse.TRUE)
)
);
}

/**
Expand Down Expand Up @@ -488,5 +462,31 @@ static void writeSearchApplicationBinaryWithVersion(SearchApplication app, Outpu
}
}

static class DelegatingIndexNotFoundActionListener<T, R> extends ActionListener.Delegating<T, R> {

private final BiConsumer<ActionListener<R>, T> bc;
private final String resourceName;

DelegatingIndexNotFoundActionListener(String resourceName, ActionListener<R> delegate, BiConsumer<ActionListener<R>, T> bc) {
super(delegate);
this.bc = bc;
this.resourceName = resourceName;
}

@Override
public void onResponse(T t) {
bc.accept(delegate, t);
}

@Override
public void onFailure(Exception e) {
if (e instanceof IndexNotFoundException) {
delegate.onFailure(new ResourceNotFoundException(resourceName, e));
return;
}
delegate.onFailure(e);
}
}

public record SearchApplicationResult(List<SearchApplicationListItem> items, long totalResults) {}
}

0 comments on commit 066b8df

Please sign in to comment.