Skip to content

Commit

Permalink
remove unused code + add 2nd level nesting test
Browse files Browse the repository at this point in the history
Signed-off-by: krishy91 <crgkc.r@gmail.com>
  • Loading branch information
krishy91 committed Feb 26, 2024
1 parent 0485dd0 commit a006c25
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,7 @@ private void validateEmbeddingFieldsValue(IngestDocument ingestDocument) {
String sourceKey = embeddingFieldsEntry.getKey();
Class<?> sourceValueClass = sourceValue.getClass();
if (List.class.isAssignableFrom(sourceValueClass) || Map.class.isAssignableFrom(sourceValueClass)) {
if (Map.class.isAssignableFrom(embeddingFieldsEntry.getValue().getClass())) {
Map<String, Object> innerFieldsEntry = (Map<String, Object>) embeddingFieldsEntry.getValue();
for (Map.Entry<String, Object> innerKey : innerFieldsEntry.entrySet()) {
validateNestedTypeValue(innerKey.getKey(), sourceValue, () -> 2);
}
} else {
validateNestedTypeValue(sourceKey, sourceValue, () -> 1);
}
validateNestedTypeValue(sourceKey, sourceValue, () -> 1);
} else if (!String.class.isAssignableFrom(sourceValueClass)) {
throw new IllegalArgumentException("field [" + sourceKey + "] is neither string nor nested type, cannot process it");
} else if (StringUtils.isBlank(sourceValue.toString())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,21 @@ public void testBuildVectorOutput_withNestedList_successful() {
assertNotNull(nestedObj.get(1).get("vectorField"));
}

public void testBuildVectorOutput_withNestedList_Level2_successful() {
Map<String, Object> config = createNestedList2LevelConfiguration();
IngestDocument ingestDocument = create2LevelNestedListIngestDocument();
TextEmbeddingProcessor textEmbeddingProcessor = createInstanceWithNestedMapConfiguration(config);
Map<String, Object> knnMap = textEmbeddingProcessor.buildMapWithProcessorKeyAndOriginalValue(ingestDocument);
List<List<Float>> modelTensorList = createMockVectorResult();
textEmbeddingProcessor.buildNLPResult(knnMap, modelTensorList, ingestDocument.getSourceAndMetadata());
Map<String, Object> nestedLevel1 = (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("nestedField");
List<Map<String, Object>> nestedObj = (List<Map<String, Object>>) nestedLevel1.get("nestedField");
assertTrue(nestedObj.get(0).containsKey("vectorField"));
assertTrue(nestedObj.get(1).containsKey("vectorField"));
assertNotNull(nestedObj.get(0).get("vectorField"));
assertNotNull(nestedObj.get(1).get("vectorField"));
}

public void test_updateDocument_appendVectorFieldsToDocument_successful() {
Map<String, Object> config = createPlainStringConfiguration();
IngestDocument ingestDocument = createPlainIngestDocument();
Expand Down Expand Up @@ -544,6 +559,16 @@ private Map<String, Object> createNestedListConfiguration() {
return result;
}

private Map<String, Object> createNestedList2LevelConfiguration() {
Map<String, Object> nestedConfig = new HashMap<>();
nestedConfig.put("textField", "vectorField");
Map<String, Object> nestConfigLevel1 = new HashMap<>();
nestConfigLevel1.put("nestedField", nestedConfig);
Map<String, Object> result = new HashMap<>();
result.put("nestedField", nestConfigLevel1);
return result;
}

private IngestDocument createNestedListIngestDocument() {
HashMap<String, Object> nestedObj1 = new HashMap<>();
nestedObj1.put("textField", "This is a text field");
Expand All @@ -553,4 +578,16 @@ private IngestDocument createNestedListIngestDocument() {
nestedList.put("nestedField", Arrays.asList(nestedObj1, nestedObj2));
return new IngestDocument(nestedList, new HashMap<>());
}

private IngestDocument create2LevelNestedListIngestDocument() {
HashMap<String, Object> nestedObj1 = new HashMap<>();
nestedObj1.put("textField", "This is a text field");
HashMap<String, Object> nestedObj2 = new HashMap<>();
nestedObj2.put("textField", "This is another text field");
HashMap<String, Object> nestedList = new HashMap<>();
nestedList.put("nestedField", Arrays.asList(nestedObj1, nestedObj2));
HashMap<String, Object> nestedList1 = new HashMap<>();
nestedList1.put("nestedField", nestedList);
return new IngestDocument(nestedList1, new HashMap<>());
}
}

0 comments on commit a006c25

Please sign in to comment.