Skip to content

Commit

Permalink
Fixed field value from nested mapping when train model
Browse files Browse the repository at this point in the history
Signed-off-by: Junqiu Lei <[email protected]>
  • Loading branch information
junqiu-lei committed Nov 21, 2023
1 parent af8c9fd commit 30ddc20
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Enhancements
### Bug Fixes
* Fix use-after-free case on nmslib search path [#1305](https://github.com/opensearch-project/k-NN/pull/1305)
* Fixed field value from nested mapping when train model [#1318](https://github.com/opensearch-project/k-NN/pull/1318)
### Infrastructure
* Upgrade gradle to 8.4 [1289](https://github.com/opensearch-project/k-NN/pull/1289)
### Documentation
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/opensearch/knn/index/IndexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ public static int getFileSizeInKB(String filePath) {
return Math.toIntExact((file.length() / BYTES_PER_KILOBYTES) + 1L); // Add one so that integer division rounds up
}

/**
* Retrieves a field mapping from properties map by recursively searching through the map.
*
* @param properties A map representing properties, where each key is a property name and
* the value is either a sub-map of properties or the property value itself.
* @param field The name of the field to retrieve.
* @return The value of the field if found, or null if the field is not present in the map.
*/
public static Object getFieldMapping(Map<String, Object> properties, String field) {
if (properties.containsKey(field)) {
return properties.get(field);
} else {
for (Map.Entry<String, Object> entry : properties.entrySet()) {
Object value = entry.getValue();
if (value instanceof Map) {
Object result = getFieldMapping((Map<String, Object>) value, field);
if (result != null) {
return result;
}
}
}

Check warning on line 82 in src/main/java/org/opensearch/knn/index/IndexUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/knn/index/IndexUtil.java#L82

Added line #L82 was not covered by tests
}
return null;

Check warning on line 84 in src/main/java/org/opensearch/knn/index/IndexUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/knn/index/IndexUtil.java#L84

Added line #L84 was not covered by tests
}

/**
* Validate that a field is a k-NN vector field and has the expected dimension
*
Expand Down Expand Up @@ -100,7 +125,7 @@ public static ValidationException validateKnnField(
return exception;
}

Object fieldMapping = properties.get(field);
Object fieldMapping = getFieldMapping(properties, field);

// Check field existence
if (fieldMapping == null) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/opensearch/knn/index/IndexUtilTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.opensearch.knn.KNNTestCase;
import org.opensearch.knn.index.util.KNNEngine;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -67,4 +69,26 @@ public void testGetLoadParameters() {
assertEquals(spaceType2.getValue(), loadParameters.get(SPACE_TYPE));
assertEquals(efSearchValue, loadParameters.get(HNSW_ALGO_EF_SEARCH));
}

public void testGetFieldMappingNonNestedField() {
Map<String, Object> properties = new HashMap<>();
properties.put("top_level_field", "top_level_value");

Object result = IndexUtil.getFieldMapping(properties, "top_level_field");
assertEquals("top_level_value", result);
}

public void testGetFieldMappingNestedField() {
Map<String, Object> deepNestedProperties = new HashMap<>();
deepNestedProperties.put("deep_inner_field", "deep_value");

Map<String, Object> nestedProperties = new HashMap<>();
nestedProperties.put("nested", Collections.singletonMap("properties", deepNestedProperties));

Map<String, Object> properties = new HashMap<>();
properties.put("top", Collections.singletonMap("properties", nestedProperties));

Object deepResult = IndexUtil.getFieldMapping(properties, "deep_inner_field");
assertEquals("deep_value", deepResult);
}
}

0 comments on commit 30ddc20

Please sign in to comment.