Skip to content

Commit

Permalink
Show concrete error when enrich index not exist rather than NPE (elas…
Browse files Browse the repository at this point in the history
…tic#99604)

There should be NullPointerException check and throw index not found exception to the response 
so the user can understand what happens with the enrich index

---------

Co-authored-by: James Baiera <james.baiera@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent b7798d1 commit ccc896d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/99604.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 99604
summary: Show concrete error when enrich index not exist rather than NPE
area: Ingest Node
type: enhancement
issues: []
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]"));
}

}

0 comments on commit ccc896d

Please sign in to comment.