-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More work on stateful pipeline processors
Added "context_prefix" convention to scope variables to avoid collisions. Let script processor have access to the request context. Added more unit tests. Signed-off-by: Michael Froh <[email protected]>
- Loading branch information
Showing
11 changed files
with
393 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...line-common/src/main/java/org/opensearch/search/pipeline/common/helpers/ContextUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.pipeline.common.helpers; | ||
|
||
/** | ||
* Helpers for working with request-scoped context. | ||
*/ | ||
public final class ContextUtils { | ||
private ContextUtils() {} | ||
|
||
/** | ||
* Parameter that can be passed to a stateful processor to avoid collisions between contextual variables by | ||
* prefixing them with distinct qualifiers. | ||
*/ | ||
public static final String CONTEXT_PREFIX_PARAMETER = "context_prefix"; | ||
|
||
/** | ||
* Replaces a "global" variable name with one scoped to a given context prefix (unless prefix is null or empty). | ||
* @param contextPrefix the prefix qualifier for the variable | ||
* @param variableName the generic "global" form of the context variable | ||
* @return the variableName prefixed with contextPrefix followed by ".", or just variableName if contextPrefix is null or empty | ||
*/ | ||
public static String applyContextPrefix(String contextPrefix, String variableName) { | ||
String contextVariable; | ||
if (contextPrefix != null && contextPrefix.isEmpty() == false) { | ||
contextVariable = contextPrefix + "." + variableName; | ||
} else { | ||
contextVariable = variableName; | ||
} | ||
return contextVariable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../src/test/java/org/opensearch/search/pipeline/common/OversampleRequestProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.pipeline.common; | ||
|
||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearch.search.pipeline.common.helpers.ContextUtils; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OversampleRequestProcessorTests extends OpenSearchTestCase { | ||
|
||
public void testEmptySource() { | ||
OversampleRequestProcessor.Factory factory = new OversampleRequestProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(Map.of(OversampleRequestProcessor.SAMPLE_FACTOR, 3.0)); | ||
OversampleRequestProcessor processor = factory.create(Collections.emptyMap(), null, null, false, config, null); | ||
|
||
SearchRequest request = new SearchRequest(); | ||
Map<String, Object> context = new HashMap<>(); | ||
SearchRequest transformedRequest = processor.processRequest(request, context); | ||
assertEquals(request, transformedRequest); | ||
assertTrue(context.isEmpty()); | ||
} | ||
|
||
public void testBasicBehavior() { | ||
OversampleRequestProcessor.Factory factory = new OversampleRequestProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(Map.of(OversampleRequestProcessor.SAMPLE_FACTOR, 3.0)); | ||
OversampleRequestProcessor processor = factory.create(Collections.emptyMap(), null, null, false, config, null); | ||
|
||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(10); | ||
SearchRequest request = new SearchRequest().source(sourceBuilder); | ||
Map<String, Object> context = new HashMap<>(); | ||
SearchRequest transformedRequest = processor.processRequest(request, context); | ||
assertEquals(30, transformedRequest.source().size()); | ||
assertEquals(1, context.size()); | ||
assertEquals(10, context.get("original_size")); | ||
} | ||
|
||
public void testContextPrefix() { | ||
OversampleRequestProcessor.Factory factory = new OversampleRequestProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>( | ||
Map.of(OversampleRequestProcessor.SAMPLE_FACTOR, 3.0, ContextUtils.CONTEXT_PREFIX_PARAMETER, "foo") | ||
); | ||
OversampleRequestProcessor processor = factory.create(Collections.emptyMap(), null, null, false, config, null); | ||
|
||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(10); | ||
SearchRequest request = new SearchRequest().source(sourceBuilder); | ||
Map<String, Object> context = new HashMap<>(); | ||
SearchRequest transformedRequest = processor.processRequest(request, context); | ||
assertEquals(30, transformedRequest.source().size()); | ||
assertEquals(1, context.size()); | ||
assertEquals(10, context.get("foo.original_size")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.