Skip to content

Commit

Permalink
improve handling for non-flat-string context fields
Browse files Browse the repository at this point in the history
Signed-off-by: HenryL27 <[email protected]>
  • Loading branch information
HenryL27 committed Dec 18, 2023
1 parent 72ff43b commit 3c13ee0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.opensearch.search.pipeline.Processor;
import org.opensearch.search.pipeline.SearchResponseProcessor;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;

/**
Expand Down Expand Up @@ -79,8 +78,7 @@ public SearchResponseProcessor create(
}
}

@VisibleForTesting
RerankType findRerankType(final Map<String, Object> config) throws IllegalArgumentException {
private RerankType findRerankType(final Map<String, Object> config) throws IllegalArgumentException {
// Set of rerank type labels in the config
Set<String> rerankTypes = Sets.intersection(config.keySet(), RerankType.labelMap().keySet());
// A rerank type must be provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.ObjectPath;
import org.opensearch.search.SearchHit;

/**
Expand Down Expand Up @@ -65,9 +66,11 @@ public void fetchContext(SearchRequest searchRequest, SearchResponse searchRespo

private String contextFromSearchHit(final SearchHit hit, final String field) {
if (hit.getFields().containsKey(field)) {
return (String) hit.field(field).getValue();
Object fieldValue = hit.field(field).getValue();
return String.valueOf(fieldValue);
} else if (hit.hasSource() && hit.getSourceAsMap().containsKey(field)) {
return (String) hit.getSourceAsMap().get(field);
Object sourceValue = ObjectPath.eval(field, hit.getSourceAsMap());
return String.valueOf(sourceValue);
} else {
return "";
}
Expand All @@ -91,7 +94,7 @@ public static DocumentContextSourceFetcher create(Object config) {
if (fields.size() == 0) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "%s must be nonempty", NAME));
}
List<String> strfields = fields.stream().map(field -> (String) field).collect(Collectors.toList());
return new DocumentContextSourceFetcher(strfields);
List<String> fieldsAsStrings = fields.stream().map(field -> (String) field).collect(Collectors.toList());
return new DocumentContextSourceFetcher(fieldsAsStrings);
}
}

0 comments on commit 3c13ee0

Please sign in to comment.