|
45 | 45 | import org.apache.logging.log4j.LogManager; |
46 | 46 | import org.apache.logging.log4j.Logger; |
47 | 47 | import org.checkerframework.checker.nullness.qual.Nullable; |
| 48 | +import org.opensearch.search.fetch.subphase.highlight.HighlightBuilder; |
48 | 49 | import org.opensearch.search.sort.ScoreSortBuilder; |
49 | 50 | import org.opensearch.search.sort.ScriptSortBuilder.ScriptSortType; |
50 | 51 | import org.opensearch.search.sort.SortBuilder; |
51 | 52 | import org.opensearch.search.sort.SortBuilders; |
52 | 53 | import org.opensearch.search.sort.SortOrder; |
| 54 | +import org.opensearch.sql.calcite.CalcitePlanContext; |
53 | 55 | import org.opensearch.sql.calcite.plan.AliasFieldsWrappable; |
54 | 56 | import org.opensearch.sql.common.setting.Settings.Key; |
55 | 57 | import org.opensearch.sql.data.type.ExprType; |
56 | 58 | import org.opensearch.sql.opensearch.data.type.OpenSearchTextType; |
| 59 | +import org.opensearch.sql.opensearch.request.OpenSearchRequestBuilder; |
57 | 60 | import org.opensearch.sql.opensearch.request.PredicateAnalyzer; |
58 | 61 | import org.opensearch.sql.opensearch.storage.OpenSearchIndex; |
59 | 62 | import org.opensearch.sql.opensearch.storage.scan.context.AbstractAction; |
@@ -106,13 +109,55 @@ public RelDataType deriveRowType() { |
106 | 109 | public RelWriter explainTerms(RelWriter pw) { |
107 | 110 | String explainString = String.valueOf(pushDownContext); |
108 | 111 | if (pw instanceof RelWriterImpl) { |
109 | | - // Only add request builder to the explain plan |
110 | | - explainString += ", " + pushDownContext.createRequestBuilder(); |
| 112 | + OpenSearchRequestBuilder requestBuilder = pushDownContext.createRequestBuilder(); |
| 113 | + applyHighlightConfig(requestBuilder); |
| 114 | + explainString += ", " + requestBuilder; |
111 | 115 | } |
112 | 116 | return super.explainTerms(pw) |
113 | 117 | .itemIf("PushDownContext", explainString, !pushDownContext.isEmpty()); |
114 | 118 | } |
115 | 119 |
|
| 120 | + /** |
| 121 | + * Apply highlight configuration from the ThreadLocal to the OpenSearch request builder. The |
| 122 | + * highlight config is set on a ThreadLocal by the plan's execute() method (on the worker thread) |
| 123 | + * and forwarded as-is to OpenSearch. |
| 124 | + * |
| 125 | + * @param requestBuilder the OpenSearch request builder to attach the highlight clause to |
| 126 | + */ |
| 127 | + @SuppressWarnings("unchecked") |
| 128 | + protected static void applyHighlightConfig(OpenSearchRequestBuilder requestBuilder) { |
| 129 | + Map<String, Object> config = CalcitePlanContext.getHighlightConfig(); |
| 130 | + if (config == null) { |
| 131 | + return; |
| 132 | + } |
| 133 | + HighlightBuilder highlightBuilder = new HighlightBuilder(); |
| 134 | + Object fieldsObj = config.get("fields"); |
| 135 | + if (fieldsObj instanceof Map) { |
| 136 | + Map<String, Object> fields = (Map<String, Object>) fieldsObj; |
| 137 | + for (String fieldName : fields.keySet()) { |
| 138 | + highlightBuilder.field(new HighlightBuilder.Field(fieldName)); |
| 139 | + } |
| 140 | + } |
| 141 | + Object preTagsObj = config.get("pre_tags"); |
| 142 | + if (preTagsObj instanceof List) { |
| 143 | + List<String> preTags = (List<String>) preTagsObj; |
| 144 | + highlightBuilder.preTags(preTags.toArray(new String[0])); |
| 145 | + } |
| 146 | + Object postTagsObj = config.get("post_tags"); |
| 147 | + if (postTagsObj instanceof List) { |
| 148 | + List<String> postTags = (List<String>) postTagsObj; |
| 149 | + highlightBuilder.postTags(postTags.toArray(new String[0])); |
| 150 | + } |
| 151 | + Object fragmentSizeObj = config.get("fragment_size"); |
| 152 | + if (fragmentSizeObj instanceof Number) { |
| 153 | + int fragmentSize = ((Number) fragmentSizeObj).intValue(); |
| 154 | + for (HighlightBuilder.Field field : highlightBuilder.fields()) { |
| 155 | + field.fragmentSize(fragmentSize); |
| 156 | + } |
| 157 | + } |
| 158 | + requestBuilder.getSourceBuilder().highlighter(highlightBuilder); |
| 159 | + } |
| 160 | + |
116 | 161 | protected Integer getQuerySizeLimit() { |
117 | 162 | return osIndex.getSettings().getSettingValue(Key.QUERY_SIZE_LIMIT); |
118 | 163 | } |
|
0 commit comments