From 6864d141c2141d003aed9e01eac5349cdd9718cd Mon Sep 17 00:00:00 2001 From: zhichao-aws Date: Mon, 26 Feb 2024 17:41:39 +0800 Subject: [PATCH] fix: add non-null check in NeuralQueryEnricherProcessor Signed-off-by: zhichao-aws --- .../NeuralQueryEnricherProcessor.java | 5 +++- .../NeuralQueryEnricherProcessorIT.java | 27 +++++++++++++++++++ .../NeuralQueryEnricherProcessorTests.java | 9 +++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessor.java b/src/main/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessor.java index b20b889cb..3ee212ec7 100644 --- a/src/main/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessor.java +++ b/src/main/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessor.java @@ -67,7 +67,10 @@ private NeuralQueryEnricherProcessor( @Override public SearchRequest processRequest(SearchRequest searchRequest) { QueryBuilder queryBuilder = searchRequest.source().query(); - queryBuilder.visit(new NeuralSearchQueryVisitor(modelId, neuralFieldDefaultIdMap)); + /* Use null check for the case where users are using empty query body. i.e. GET /index_name/_search */ + if (queryBuilder != null) { + queryBuilder.visit(new NeuralSearchQueryVisitor(modelId, neuralFieldDefaultIdMap)); + } return searchRequest; } diff --git a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorIT.java b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorIT.java index 6d6ea37c8..cd8300928 100644 --- a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorIT.java +++ b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorIT.java @@ -11,8 +11,14 @@ import java.util.Collections; import java.util.Map; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.junit.Before; +import org.opensearch.client.Request; +import org.opensearch.client.Response; import org.opensearch.common.settings.Settings; +import org.opensearch.common.xcontent.XContentHelper; +import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.rest.RestStatus; import org.opensearch.neuralsearch.BaseNeuralSearchIT; import org.opensearch.neuralsearch.query.HybridQueryBuilder; import org.opensearch.neuralsearch.query.NeuralQueryBuilder; @@ -55,6 +61,27 @@ public void testNeuralQueryEnricherProcessor_whenNoModelIdPassed_thenSuccess() { } } + @SneakyThrows + public void testNeuralQueryEnricherProcessor_whenGetEmptyQueryBody_thenSuccess() { + String modelId = null; + try { + initializeIndexIfNotExist(index); + modelId = prepareModel(); + createSearchRequestProcessor(modelId, search_pipeline); + createPipelineProcessor(modelId, ingest_pipeline, ProcessorType.TEXT_EMBEDDING); + updateIndexSettings(index, Settings.builder().put("index.search.default_pipeline", search_pipeline)); + Request request = new Request("POST", "/" + index + "/_search"); + Response response = client().performRequest(request); + assertEquals(request.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); + String responseBody = EntityUtils.toString(response.getEntity()); + Map responseInMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), responseBody, false); + assertFalse(responseInMap.isEmpty()); + assertEquals(3, ((Map) responseInMap.get("hits")).size()); + } finally { + wipeOfTestResources(index, ingest_pipeline, modelId, search_pipeline); + } + } + @SneakyThrows public void testNeuralQueryEnricherProcessor_whenHybridQueryBuilderAndNoModelIdPassed_thenSuccess() { String modelId = null; diff --git a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorTests.java b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorTests.java index df5c4ad60..b811766f6 100644 --- a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorTests.java +++ b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryEnricherProcessorTests.java @@ -46,6 +46,15 @@ public void testProcessRequest_whenVisitingQueryBuilder_thenSuccess() throws Exc assertEquals(processSearchRequest, searchRequest); } + public void testProcessRequest_whenVisitingEmptyQueryBody_thenSuccess() throws Exception { + NeuralQueryEnricherProcessor.Factory factory = new NeuralQueryEnricherProcessor.Factory(); + SearchRequest searchRequest = new SearchRequest(); + searchRequest.source(new SearchSourceBuilder()); + NeuralQueryEnricherProcessor processor = createTestProcessor(factory); + SearchRequest processSearchRequest = processor.processRequest(searchRequest); + assert (processSearchRequest == searchRequest); + } + public void testType() throws Exception { NeuralQueryEnricherProcessor.Factory factory = new NeuralQueryEnricherProcessor.Factory(); NeuralQueryEnricherProcessor processor = createTestProcessor(factory);