|
18 | 18 |
|
19 | 19 | import java.util.ArrayList; |
20 | 20 | import java.util.Collections; |
| 21 | +import java.util.LinkedHashMap; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.Optional; |
@@ -239,10 +240,7 @@ public List<Document> doSimilaritySearch(SearchRequest request) { |
239 | 240 |
|
240 | 241 | final AzureSearchDocument entry = result.getDocument(AzureSearchDocument.class); |
241 | 242 |
|
242 | | - Map<String, Object> metadata = (StringUtils.hasText(entry.metadata())) |
243 | | - ? JSONObject.parseObject(entry.metadata(), new TypeReference<>() { |
244 | | - |
245 | | - }) : Map.of(); |
| 243 | + Map<String, Object> metadata = parseMetadataToMutable(entry.metadata()); |
246 | 244 |
|
247 | 245 | metadata.put(DocumentMetadata.DISTANCE.value(), 1.0 - result.getScore()); |
248 | 246 |
|
@@ -325,6 +323,21 @@ public <T> Optional<T> getNativeClient() { |
325 | 323 | return Optional.of(client); |
326 | 324 | } |
327 | 325 |
|
| 326 | + static Map<String, Object> parseMetadataToMutable(@Nullable String metadataJson) { |
| 327 | + if (!StringUtils.hasText(metadataJson)) { |
| 328 | + return new LinkedHashMap<>(); |
| 329 | + } |
| 330 | + try { |
| 331 | + Map<String, Object> parsed = JSONObject.parseObject(metadataJson, new TypeReference<Map<String, Object>>() { |
| 332 | + }); |
| 333 | + return (parsed == null) ? new LinkedHashMap<>() : new LinkedHashMap<>(parsed); |
| 334 | + } |
| 335 | + catch (Exception ex) { |
| 336 | + logger.warn("Failed to parse metadata JSON. Using empty metadata. json={}", metadataJson, ex); |
| 337 | + return new LinkedHashMap<>(); |
| 338 | + } |
| 339 | + } |
| 340 | + |
328 | 341 | public record MetadataField(String name, SearchFieldDataType fieldType) { |
329 | 342 |
|
330 | 343 | public static MetadataField text(String name) { |
|
0 commit comments