-
Notifications
You must be signed in to change notification settings - Fork 133
Add SemanticHighlighterQueryEnricherProcessor #1917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nomoa
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
nomoa:SemanticHighlighterQueryEnricherProcessor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
181 changes: 181 additions & 0 deletions
181
...java/org/opensearch/neuralsearch/processor/SemanticHighlighterQueryEnricherProcessor.java
This file contains hidden or 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,181 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.neuralsearch.processor; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| import org.apache.lucene.search.BooleanClause; | ||
| import org.opensearch.action.search.SearchRequest; | ||
| import org.opensearch.common.Nullable; | ||
| import org.opensearch.index.query.InnerHitBuilder; | ||
| import org.opensearch.index.query.NestedQueryBuilder; | ||
| import org.opensearch.index.query.QueryBuilder; | ||
| import org.opensearch.index.query.QueryBuilderVisitor; | ||
| import org.opensearch.ingest.ConfigurationUtils; | ||
| import org.opensearch.neuralsearch.highlight.SemanticHighlightingConstants; | ||
| import org.opensearch.neuralsearch.stats.events.EventStatName; | ||
| import org.opensearch.neuralsearch.stats.events.EventStatsManager; | ||
| import org.opensearch.search.builder.SearchSourceBuilder; | ||
| import org.opensearch.search.fetch.subphase.highlight.HighlightBuilder; | ||
| import org.opensearch.search.pipeline.AbstractProcessor; | ||
| import org.opensearch.search.pipeline.Processor; | ||
| import org.opensearch.search.pipeline.SearchRequestProcessor; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.opensearch.ingest.ConfigurationUtils.readOptionalStringProperty; | ||
|
|
||
| /** | ||
| * Query enricher that will populate the model_id option when the semantic highlighter is used and no model_id is | ||
| * specified in the search query body. | ||
| */ | ||
| @Getter | ||
| public class SemanticHighlighterQueryEnricherProcessor extends AbstractProcessor implements SearchRequestProcessor { | ||
| public static final String TYPE = SemanticHighlightingConstants.QUERY_ENRICHER_TYPE; | ||
|
|
||
| private final String modelId; | ||
| private final Map<String, Object> fieldDefaultIdMap; | ||
|
|
||
| private SemanticHighlighterQueryEnricherProcessor( | ||
| String tag, | ||
| String description, | ||
| boolean ignoreFailure, | ||
| @Nullable String modelId, | ||
| @Nullable Map<String, Object> fieldDefaultIdMap | ||
| ) { | ||
| super(tag, description, ignoreFailure); | ||
| this.modelId = modelId; | ||
| this.fieldDefaultIdMap = fieldDefaultIdMap; | ||
| } | ||
|
|
||
| @Override | ||
| public SearchRequest processRequest(SearchRequest searchRequest) { | ||
| EventStatsManager.increment(EventStatName.SEMANTIC_HIGHLIGHTING_QUERY_ENRICHER_EXECUTIONS); | ||
| Optional<SearchSourceBuilder> source = Optional.ofNullable(searchRequest.source()); | ||
| source.map(SearchSourceBuilder::highlighter).ifPresent(this::enrichHighlight); | ||
| source.map(SearchSourceBuilder::query).ifPresent(qb -> qb.visit(new NestedQueryHighlightVisitor())); | ||
| // NOTE: we explicitly do not enrich TopHitsAggregationBuilder highlighters because it is not useful yet — the batch path ignores | ||
| // aggregations entirely (HighlightConfigResolver only walks source.highlighter() and inner_hits, HighlightContextBuilder only reads | ||
| // response.getHits()), so under ext.semantic_highlighting_batch the model_id would be set but never used, and highlights would go | ||
| // missing silently. Needs the highlighting feature to support aggregations first. | ||
| return searchRequest; | ||
| } | ||
|
|
||
| private void enrichHighlight(HighlightBuilder hlBuilder) { | ||
| Map<String, Object> globalOptions = hlBuilder.options(); | ||
| boolean userSuppliedGlobalModelId = globalOptions != null && globalOptions.containsKey(SemanticHighlightingConstants.MODEL_ID); | ||
| if (userSuppliedGlobalModelId) { | ||
| // if the user provided a global model_id there's no need to enrich anything. | ||
| return; | ||
| } | ||
|
|
||
| boolean globalIsSemantic = false; | ||
| if (SemanticHighlightingConstants.HIGHLIGHTER_TYPE.equals(hlBuilder.highlighterType())) { | ||
| globalIsSemantic = true; | ||
| if (modelId != null) { | ||
| hlBuilder.options(enrichWithModelId(globalOptions, modelId)); | ||
| } | ||
| } | ||
| for (HighlightBuilder.Field field : Optional.ofNullable(hlBuilder.fields()).orElseGet(Collections::emptyList)) { | ||
| // Enrich if either: | ||
| // - the global type is semantic and the field specific type is unset | ||
| // - the field specific type is set to semantic | ||
| if ((globalIsSemantic && field.highlighterType() == null) | ||
| || SemanticHighlightingConstants.HIGHLIGHTER_TYPE.equals(field.highlighterType())) { | ||
| String fieldModelId = (String) Optional.ofNullable(this.fieldDefaultIdMap) | ||
| .orElseGet(Collections::emptyMap) | ||
| .getOrDefault(field.name(), modelId); | ||
| if (fieldModelId != null) { | ||
| field.options(enrichWithModelId(field.options(), fieldModelId)); | ||
| } | ||
| // else: no default model_id and no per-field override for this field, nothing to enrich | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Map<String, Object> enrichWithModelId(@Nullable Map<String, Object> options, String modelId) { | ||
| if (options != null && options.containsKey(SemanticHighlightingConstants.MODEL_ID)) { | ||
| return options; | ||
| } | ||
| Map<String, Object> enrichedOptions = options != null ? new HashMap<>(options) : new HashMap<>(); | ||
| enrichedOptions.put(SemanticHighlightingConstants.MODEL_ID, modelId); | ||
| return enrichedOptions; | ||
| } | ||
|
|
||
| private class NestedQueryHighlightVisitor implements QueryBuilderVisitor { | ||
| @Override | ||
| public void accept(QueryBuilder qb) { | ||
| if (qb instanceof NestedQueryBuilder nested) { | ||
| Optional.ofNullable(nested.innerHit()) | ||
| .map(InnerHitBuilder::getHighlightBuilder) | ||
| .ifPresent(SemanticHighlighterQueryEnricherProcessor.this::enrichHighlight); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return SemanticHighlightingConstants.QUERY_ENRICHER_TYPE; | ||
| } | ||
|
|
||
| public static class Factory implements Processor.Factory<SearchRequestProcessor> { | ||
| private static final String DEFAULT_MODEL_ID = "default_model_id"; | ||
| private static final String SEMANTIC_HIGHLIGHTER_FIELD_DEFAULT_ID = "semantic_highlighter_field_default_id"; | ||
|
|
||
| /** | ||
| * Create the processor object. | ||
| * | ||
| * @return {@link SemanticHighlighterQueryEnricherProcessor} | ||
| */ | ||
| @Override | ||
| public SemanticHighlighterQueryEnricherProcessor create( | ||
| Map<String, Processor.Factory<SearchRequestProcessor>> processorFactories, | ||
| String tag, | ||
| String description, | ||
| boolean ignoreFailure, | ||
| Map<String, Object> config, | ||
| PipelineContext pipelineContext | ||
| ) throws IllegalArgumentException { | ||
| String modelId = readOptionalStringProperty(TYPE, tag, config, DEFAULT_MODEL_ID); | ||
| Map<String, Object> fieldMap = ConfigurationUtils.readOptionalMap(TYPE, tag, config, SEMANTIC_HIGHLIGHTER_FIELD_DEFAULT_ID); | ||
|
|
||
| if (modelId == null && fieldMap == null) { | ||
| throw new IllegalArgumentException("[default_model_id] or [semantic_highlighter_field_default_id] should be provided"); | ||
| } | ||
|
|
||
| if (fieldMap != null) { | ||
| List<String> nonStringFields = fieldMap.entrySet() | ||
| .stream() | ||
| .filter(en -> !(en.getValue() instanceof String)) | ||
| .map(Map.Entry::getKey) | ||
| .toList(); | ||
| if (!nonStringFields.isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "Invalid type in [semantic_highlighter_field_default_id]: value for [" | ||
| + String.join(", ", nonStringFields) | ||
| + "] must be a model_id of type string" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return new SemanticHighlighterQueryEnricherProcessor( | ||
| tag, | ||
| description, | ||
| ignoreFailure, | ||
| modelId, | ||
| fieldMap != null ? Collections.unmodifiableMap(fieldMap) : null | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsure about this, if 3.8.0 has been released, this should probably be 3.9.0 but current dependencies still target 3.8