Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show concrete error when enrich index not exist rather than NPE #99604

Merged
merged 4 commits into from Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/99604.yaml
@@ -0,0 +1,5 @@
pr: 99604
summary: Show concrete error when enrich index not exist rather than NPE
area: Ingest Node
type: enhancement
issues: []
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.cache.Cache;
import org.elasticsearch.common.cache.CacheBuilder;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction;

Expand Down Expand Up @@ -88,6 +89,9 @@ public EnrichStatsAction.Response.CacheStats getStats(String localNodeId) {
private String getEnrichIndexKey(SearchRequest searchRequest) {
String alias = searchRequest.indices()[0];
IndexAbstraction ia = metadata.getIndicesLookup().get(alias);
if (ia == null) {
throw new IndexNotFoundException("no generated enrich index [" + alias + "]");
}
return ia.getIndices().get(0).getName();
}

Expand Down
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
Expand All @@ -21,6 +22,7 @@
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -183,4 +185,22 @@ public void testDeepCopy() {
assertArrayEquals(new byte[] { 1, 2, 3 }, (byte[]) result.get("embedded_object"));
}

public void testEnrichIndexNotExist() {
// Emulate cluster metadata:
var metadata = Metadata.builder().build();

// Emulated search request on a non-exist enrich index that an enrich processor could generate
var searchRequest = new SearchRequest(EnrichPolicy.getBaseName("policy-enrich-index-not-generated")).source(
new SearchSourceBuilder().query(new MatchQueryBuilder("test", "query"))
);
// Emulated search response (content doesn't matter, since it isn't used, it just a cache entry)
List<Map<?, ?>> searchResponse = List.of(Map.of("test", "entry"));

EnrichCache enrichCache = new EnrichCache(1);
enrichCache.setMetadata(metadata);

IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> enrichCache.put(searchRequest, searchResponse));
assertThat(e.getMessage(), containsString("no generated enrich index [.enrich-policy-enrich-index-not-generated]"));
}

}