Skip to content

Commit

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

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>
(cherry picked from commit ccc896d)

# Conflicts:
#	x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichCache.java
#	x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichCacheTests.java

Co-authored-by: puppylpg <shininglhb@163.com>
  • Loading branch information
jbaiera and puppylpg committed Oct 2, 2023
1 parent 7288c24 commit 364c340
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
@@ -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 @@ -13,6 +13,7 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.cache.Cache;
import org.elasticsearch.common.cache.CacheBuilder;
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 @@ -11,6 +11,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.query.MatchQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESTestCase;
Expand All @@ -24,6 +25,7 @@
import java.util.Map;

import static org.elasticsearch.xpack.enrich.MatchProcessorTests.mapOf;
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 @@ -186,4 +188,22 @@ public void testDeepCopy() {
assertArrayEquals(new byte[] { 1, 2, 3 }, (byte[]) result.get("embedded_object"));
}

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

// Emulated search request on a non-exist enrich index that an enrich processor could generate
SearchRequest 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 = Collections.singletonList(Collections.singletonMap("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 364c340

Please sign in to comment.