|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Collections;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.List;
|
| 23 | +import java.util.Map; |
22 | 24 | import java.util.Optional;
|
23 | 25 | import java.util.stream.Collectors;
|
24 | 26 | import java.util.stream.IntStream;
|
@@ -351,16 +353,32 @@ public List<Document> doSimilaritySearch(SearchRequest request) {
|
351 | 353 |
|
352 | 354 | CosmosPagedFlux<JsonNode> pagedFlux = this.container.queryItems(sqlQuerySpec, options, JsonNode.class);
|
353 | 355 |
|
| 356 | + |
354 | 357 | logger.info("Executing similarity search query: {}", query);
|
355 | 358 | try {
|
356 | 359 | // Collect documents from the paged flux
|
357 | 360 | List<JsonNode> documents = pagedFlux.byPage()
|
358 | 361 | .flatMap(page -> Flux.fromIterable(page.getResults()))
|
359 | 362 | .collectList()
|
360 | 363 | .block();
|
| 364 | + |
| 365 | + // Collect metadata fields from the documents |
| 366 | + Map<String, Object> docFields = new HashMap<>(); |
| 367 | + for (var doc : documents) { |
| 368 | + JsonNode metadata = doc.get("metadata"); |
| 369 | + metadata.fieldNames().forEachRemaining(field -> { |
| 370 | + JsonNode value = metadata.get(field); |
| 371 | + Object parsedValue = value.isTextual() ? value.asText() : |
| 372 | + value.isNumber() ? value.numberValue() : |
| 373 | + value.isBoolean() ? value.booleanValue() : |
| 374 | + value.toString(); |
| 375 | + docFields.put(field, parsedValue); |
| 376 | + }); |
| 377 | + } |
| 378 | + |
361 | 379 | // Convert JsonNode to Document
|
362 | 380 | List<Document> docs = documents.stream()
|
363 |
| - .map(doc -> Document.builder().id(doc.get("id").asText()).text(doc.get("content").asText()).build()) |
| 381 | + .map(doc -> Document.builder().id(doc.get("id").asText()).text(doc.get("content").asText()).metadata(docFields).build()) |
364 | 382 | .collect(Collectors.toList());
|
365 | 383 |
|
366 | 384 | return docs != null ? docs : List.of();
|
|
0 commit comments