|
5 | 5 | package org.opensearch.neuralsearch.processor;
|
6 | 6 |
|
7 | 7 | import java.util.ArrayList;
|
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.HashMap; |
8 | 12 | import java.util.LinkedHashMap;
|
9 | 13 | import java.util.List;
|
10 | 14 | import java.util.Map;
|
11 | 15 | import java.util.Objects;
|
12 | 16 | import java.util.function.BiConsumer;
|
| 17 | +import java.util.function.Consumer; |
13 | 18 | import java.util.function.Supplier;
|
14 | 19 | import java.util.stream.Collectors;
|
15 | 20 | import java.util.stream.IntStream;
|
16 | 21 |
|
| 22 | +import lombok.AllArgsConstructor; |
| 23 | +import lombok.Getter; |
17 | 24 | import org.apache.commons.lang3.StringUtils;
|
| 25 | +import org.opensearch.common.collect.Tuple; |
| 26 | +import org.opensearch.core.common.util.CollectionUtils; |
18 | 27 | import org.opensearch.env.Environment;
|
19 | 28 | import org.opensearch.index.mapper.MapperService;
|
20 | 29 | import org.opensearch.ingest.AbstractProcessor;
|
21 | 30 | import org.opensearch.ingest.IngestDocument;
|
| 31 | +import org.opensearch.ingest.IngestDocumentWrapper; |
22 | 32 | import org.opensearch.neuralsearch.ml.MLCommonsClientAccessor;
|
23 | 33 |
|
24 | 34 | import com.google.common.annotations.VisibleForTesting;
|
@@ -119,6 +129,121 @@ public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Ex
|
119 | 129 | }
|
120 | 130 | }
|
121 | 131 |
|
| 132 | + /** |
| 133 | + * This is the function which does actual inference work for batchExecute interface. |
| 134 | + * @param inferenceList a list of String for inference. |
| 135 | + * @param handler a callback handler to handle inference results which is a list of objects. |
| 136 | + * @param onException an exception callback to handle exception. |
| 137 | + */ |
| 138 | + abstract void doBatchExecute(List<String> inferenceList, Consumer<List<?>> handler, Consumer<Exception> onException); |
| 139 | + |
| 140 | + @Override |
| 141 | + public void batchExecute(List<IngestDocumentWrapper> ingestDocumentWrappers, Consumer<List<IngestDocumentWrapper>> handler) { |
| 142 | + if (CollectionUtils.isEmpty(ingestDocumentWrappers)) { |
| 143 | + handler.accept(Collections.emptyList()); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + List<DataForInference> dataForInferences = getDataForInference(ingestDocumentWrappers); |
| 148 | + List<String> inferenceList = constructInferenceTexts(dataForInferences); |
| 149 | + if (inferenceList.isEmpty()) { |
| 150 | + handler.accept(ingestDocumentWrappers); |
| 151 | + return; |
| 152 | + } |
| 153 | + Tuple<List<String>, Map<Integer, Integer>> sortedResult = sortByLengthAndReturnOriginalOrder(inferenceList); |
| 154 | + inferenceList = sortedResult.v1(); |
| 155 | + Map<Integer, Integer> originalOrder = sortedResult.v2(); |
| 156 | + doBatchExecute(inferenceList, results -> { |
| 157 | + int startIndex = 0; |
| 158 | + results = restoreToOriginalOrder(results, originalOrder); |
| 159 | + for (DataForInference dataForInference : dataForInferences) { |
| 160 | + if (dataForInference.getIngestDocumentWrapper().getException() != null |
| 161 | + || CollectionUtils.isEmpty(dataForInference.getInferenceList())) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + List<?> inferenceResults = results.subList(startIndex, startIndex + dataForInference.getInferenceList().size()); |
| 165 | + startIndex += dataForInference.getInferenceList().size(); |
| 166 | + setVectorFieldsToDocument( |
| 167 | + dataForInference.getIngestDocumentWrapper().getIngestDocument(), |
| 168 | + dataForInference.getProcessMap(), |
| 169 | + inferenceResults |
| 170 | + ); |
| 171 | + } |
| 172 | + handler.accept(ingestDocumentWrappers); |
| 173 | + }, exception -> { |
| 174 | + for (IngestDocumentWrapper ingestDocumentWrapper : ingestDocumentWrappers) { |
| 175 | + // The IngestDocumentWrapper might already run into exception and not sent for inference. So here we only |
| 176 | + // set exception to IngestDocumentWrapper which doesn't have exception before. |
| 177 | + if (ingestDocumentWrapper.getException() == null) { |
| 178 | + ingestDocumentWrapper.update(ingestDocumentWrapper.getIngestDocument(), exception); |
| 179 | + } |
| 180 | + } |
| 181 | + handler.accept(ingestDocumentWrappers); |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + private Tuple<List<String>, Map<Integer, Integer>> sortByLengthAndReturnOriginalOrder(List<String> inferenceList) { |
| 186 | + List<Tuple<Integer, String>> docsWithIndex = new ArrayList<>(); |
| 187 | + for (int i = 0; i < inferenceList.size(); ++i) { |
| 188 | + docsWithIndex.add(Tuple.tuple(i, inferenceList.get(i))); |
| 189 | + } |
| 190 | + docsWithIndex.sort(Comparator.comparingInt(t -> t.v2().length())); |
| 191 | + List<String> sortedInferenceList = docsWithIndex.stream().map(Tuple::v2).collect(Collectors.toList()); |
| 192 | + Map<Integer, Integer> originalOrderMap = new HashMap<>(); |
| 193 | + for (int i = 0; i < docsWithIndex.size(); ++i) { |
| 194 | + originalOrderMap.put(i, docsWithIndex.get(i).v1()); |
| 195 | + } |
| 196 | + return Tuple.tuple(sortedInferenceList, originalOrderMap); |
| 197 | + } |
| 198 | + |
| 199 | + private List<?> restoreToOriginalOrder(List<?> results, Map<Integer, Integer> originalOrder) { |
| 200 | + List<Object> sortedResults = Arrays.asList(results.toArray()); |
| 201 | + for (int i = 0; i < results.size(); ++i) { |
| 202 | + if (!originalOrder.containsKey(i)) continue; |
| 203 | + int oldIndex = originalOrder.get(i); |
| 204 | + sortedResults.set(oldIndex, results.get(i)); |
| 205 | + } |
| 206 | + return sortedResults; |
| 207 | + } |
| 208 | + |
| 209 | + private List<String> constructInferenceTexts(List<DataForInference> dataForInferences) { |
| 210 | + List<String> inferenceTexts = new ArrayList<>(); |
| 211 | + for (DataForInference dataForInference : dataForInferences) { |
| 212 | + if (dataForInference.getIngestDocumentWrapper().getException() != null |
| 213 | + || CollectionUtils.isEmpty(dataForInference.getInferenceList())) { |
| 214 | + continue; |
| 215 | + } |
| 216 | + inferenceTexts.addAll(dataForInference.getInferenceList()); |
| 217 | + } |
| 218 | + return inferenceTexts; |
| 219 | + } |
| 220 | + |
| 221 | + private List<DataForInference> getDataForInference(List<IngestDocumentWrapper> ingestDocumentWrappers) { |
| 222 | + List<DataForInference> dataForInferences = new ArrayList<>(); |
| 223 | + for (IngestDocumentWrapper ingestDocumentWrapper : ingestDocumentWrappers) { |
| 224 | + Map<String, Object> processMap = null; |
| 225 | + List<String> inferenceList = null; |
| 226 | + try { |
| 227 | + validateEmbeddingFieldsValue(ingestDocumentWrapper.getIngestDocument()); |
| 228 | + processMap = buildMapWithProcessorKeyAndOriginalValue(ingestDocumentWrapper.getIngestDocument()); |
| 229 | + inferenceList = createInferenceList(processMap); |
| 230 | + } catch (Exception e) { |
| 231 | + ingestDocumentWrapper.update(ingestDocumentWrapper.getIngestDocument(), e); |
| 232 | + } finally { |
| 233 | + dataForInferences.add(new DataForInference(ingestDocumentWrapper, processMap, inferenceList)); |
| 234 | + } |
| 235 | + } |
| 236 | + return dataForInferences; |
| 237 | + } |
| 238 | + |
| 239 | + @Getter |
| 240 | + @AllArgsConstructor |
| 241 | + private static class DataForInference { |
| 242 | + private final IngestDocumentWrapper ingestDocumentWrapper; |
| 243 | + private final Map<String, Object> processMap; |
| 244 | + private final List<String> inferenceList; |
| 245 | + } |
| 246 | + |
122 | 247 | @SuppressWarnings({ "unchecked" })
|
123 | 248 | private List<String> createInferenceList(Map<String, Object> knnKeyMap) {
|
124 | 249 | List<String> texts = new ArrayList<>();
|
|
0 commit comments