Skip to content

Commit 364c340

Browse files
jbaierapuppylpg
andauthored
Show concrete error when enrich index not exist rather than NPE (#99604) (#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 <[email protected]> Co-authored-by: Elastic Machine <[email protected]> (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 <[email protected]>
1 parent 7288c24 commit 364c340

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

docs/changelog/99604.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 99604
2+
summary: Show concrete error when enrich index not exist rather than NPE
3+
area: Ingest Node
4+
type: enhancement
5+
issues: []

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichCache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.cluster.metadata.Metadata;
1414
import org.elasticsearch.common.cache.Cache;
1515
import org.elasticsearch.common.cache.CacheBuilder;
16+
import org.elasticsearch.index.IndexNotFoundException;
1617
import org.elasticsearch.search.SearchHit;
1718
import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction;
1819

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

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichCacheTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.cluster.metadata.AliasMetadata;
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
1313
import org.elasticsearch.cluster.metadata.Metadata;
14+
import org.elasticsearch.index.IndexNotFoundException;
1415
import org.elasticsearch.index.query.MatchQueryBuilder;
1516
import org.elasticsearch.search.builder.SearchSourceBuilder;
1617
import org.elasticsearch.test.ESTestCase;
@@ -24,6 +25,7 @@
2425
import java.util.Map;
2526

2627
import static org.elasticsearch.xpack.enrich.MatchProcessorTests.mapOf;
28+
import static org.hamcrest.Matchers.containsString;
2729
import static org.hamcrest.Matchers.equalTo;
2830
import static org.hamcrest.Matchers.not;
2931
import static org.hamcrest.Matchers.notNullValue;
@@ -186,4 +188,22 @@ public void testDeepCopy() {
186188
assertArrayEquals(new byte[] { 1, 2, 3 }, (byte[]) result.get("embedded_object"));
187189
}
188190

191+
public void testEnrichIndexNotExist() {
192+
// Emulate cluster metadata:
193+
Metadata metadata = Metadata.builder().build();
194+
195+
// Emulated search request on a non-exist enrich index that an enrich processor could generate
196+
SearchRequest searchRequest = new SearchRequest(EnrichPolicy.getBaseName("policy-enrich-index-not-generated")).source(
197+
new SearchSourceBuilder().query(new MatchQueryBuilder("test", "query"))
198+
);
199+
// Emulated search response (content doesn't matter, since it isn't used, it just a cache entry)
200+
List<Map<?, ?>> searchResponse = Collections.singletonList(Collections.singletonMap("test", "entry"));
201+
202+
EnrichCache enrichCache = new EnrichCache(1);
203+
enrichCache.setMetadata(metadata);
204+
205+
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> enrichCache.put(searchRequest, searchResponse));
206+
assertThat(e.getMessage(), containsString("no generated enrich index [.enrich-policy-enrich-index-not-generated]"));
207+
}
208+
189209
}

0 commit comments

Comments
 (0)