Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -239,10 +240,7 @@ public List<Document> doSimilaritySearch(SearchRequest request) {

final AzureSearchDocument entry = result.getDocument(AzureSearchDocument.class);

Map<String, Object> metadata = (StringUtils.hasText(entry.metadata()))
? JSONObject.parseObject(entry.metadata(), new TypeReference<>() {

}) : Map.of();
Map<String, Object> metadata = parseMetadataToMutable(entry.metadata());

metadata.put(DocumentMetadata.DISTANCE.value(), 1.0 - result.getScore());

Expand Down Expand Up @@ -325,6 +323,21 @@ public <T> Optional<T> getNativeClient() {
return Optional.of(client);
}

static Map<String, Object> parseMetadataToMutable(@Nullable String metadataJson) {
if (!StringUtils.hasText(metadataJson)) {
return new LinkedHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason you used a LinkedHashMap?
It seems like a regular HashMap would work just as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question! I used LinkedHashMap to preserve iteration order so that the parsed JSON key order is kept and the injected distance appears last. This gives deterministic logging/serialization and makes debugging easier. There’s no functional dependency on ordering though—if the project prefers HashMap, I’m happy to switch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it doesn’t affect the outcome, there may not be a significant difference, but since HashMap is more efficient, it seems better to change it to a HashMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point—updated to use HashMap since ordering isn’t required. The helper now returns a new HashMap<> and tests still pass. PTAL. ✅ (4b44359)

}
try {
Map<String, Object> parsed = JSONObject.parseObject(metadataJson, new TypeReference<Map<String, Object>>() {
});
return (parsed == null) ? new LinkedHashMap<>() : new LinkedHashMap<>(parsed);
}
catch (Exception ex) {
logger.warn("Failed to parse metadata JSON. Using empty metadata. json={}", metadataJson, ex);
return new LinkedHashMap<>();
}
}

public record MetadataField(String name, SearchFieldDataType fieldType) {

public static MetadataField text(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.springframework.ai.vectorstore.azure;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;

import org.junit.jupiter.api.Test;

class AzureVectorStoreMetadataTests {

@Test
void returnsMutableMapForBlankOrNull() {
Map<String, Object> m1 = AzureVectorStore.parseMetadataToMutable(null);
m1.put("distance", 0.1);
assertThat(m1).containsEntry("distance", 0.1);

Map<String, Object> m2 = AzureVectorStore.parseMetadataToMutable("");
m2.put("distance", 0.2);
assertThat(m2).containsEntry("distance", 0.2);

Map<String, Object> m3 = AzureVectorStore.parseMetadataToMutable(" ");
m3.put("distance", 0.3);
assertThat(m3).containsEntry("distance", 0.3);
}

@Test
void wrapsParsedJsonInLinkedHashMapSoItIsMutable() {
Map<String, Object> map = AzureVectorStore.parseMetadataToMutable("{\"k\":\"v\"}");
assertThat(map).containsEntry("k", "v");
map.put("distance", 0.4);
assertThat(map).containsEntry("distance", 0.4);
}

}