Skip to content

Commit

Permalink
Add includeFields parameter to the method extractFieldNamesTypes (#376)
Browse files Browse the repository at this point in the history
* Add includeFields parameter to the method extractFieldNamesTypes

Signed-off-by: gaobinlong <gbinlong@amazon.com>

* Remove empty line

Signed-off-by: gaobinlong <gbinlong@amazon.com>

---------

Signed-off-by: gaobinlong <gbinlong@amazon.com>
(cherry picked from commit 5a9dbcd)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Aug 2, 2024
1 parent 0fbce60 commit 0c7ba87
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)

// flatten all the fields in the mapping
Map<String, String> fieldsToType = new HashMap<>();
ToolHelper.extractFieldNamesTypes(mappingSource, fieldsToType, "");
ToolHelper.extractFieldNamesTypes(mappingSource, fieldsToType, "", true);

// find all date type fields from the mapping
final Set<String> dateFields = findDateTypeFields(fieldsToType);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/opensearch/agent/tools/PPLTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private String constructTableInfo(SearchHit[] searchHits, Map<String, MappingMet
);
}
Map<String, String> fieldsToType = new HashMap<>();
ToolHelper.extractFieldNamesTypes(mappingSource, fieldsToType, "");
ToolHelper.extractFieldNamesTypes(mappingSource, fieldsToType, "", false);
StringJoiner tableInfoJoiner = new StringJoiner("\n");
List<String> sortedKeys = new ArrayList<>(fieldsToType.keySet());
Collections.sort(sortedKeys);
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/org/opensearch/agent/tools/utils/ToolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ public class ToolHelper {
* @param mappingSource the mappings of an index
* @param fieldsToType the result containing the field to fieldType mapping
* @param prefix the parent field path
* @param includeFields whether include the `fields` in a text type field, for some use case like PPLTool, `fields` in a text type field
* cannot be included, but for CreateAnomalyDetectorTool, `fields` must be included.
*/
public static void extractFieldNamesTypes(Map<String, Object> mappingSource, Map<String, String> fieldsToType, String prefix) {
public static void extractFieldNamesTypes(
Map<String, Object> mappingSource,
Map<String, String> fieldsToType,
String prefix,
boolean includeFields
) {
if (prefix.length() > 0) {
prefix += ".";
}
Expand All @@ -26,15 +33,17 @@ public static void extractFieldNamesTypes(Map<String, Object> mappingSource, Map
if (v instanceof Map) {
Map<String, Object> vMap = (Map<String, Object>) v;
if (vMap.containsKey("type")) {
if (!((vMap.getOrDefault("type", "")).equals("alias"))) {
String fieldType = (String) vMap.getOrDefault("type", "");
// no need to extract alias into the result, and for object field, extract the subfields only
if (!fieldType.equals("alias") && !fieldType.equals("object")) {
fieldsToType.put(prefix + n, (String) vMap.get("type"));
}
}
if (vMap.containsKey("properties")) {
extractFieldNamesTypes((Map<String, Object>) vMap.get("properties"), fieldsToType, prefix + n);
extractFieldNamesTypes((Map<String, Object>) vMap.get("properties"), fieldsToType, prefix + n, includeFields);
}
if (vMap.containsKey("fields")) {
extractFieldNamesTypes((Map<String, Object>) vMap.get("fields"), fieldsToType, prefix + n);
if (includeFields && vMap.containsKey("fields")) {
extractFieldNamesTypes((Map<String, Object>) vMap.get("fields"), fieldsToType, prefix + n, true);
}
}
}
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/org/opensearch/agent/tools/ToolHelperTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.agent.tools;

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.opensearch.agent.tools.utils.ToolHelper;

import lombok.extern.log4j.Log4j2;

@Log4j2
public class ToolHelperTests {
@Test
public void TestExtractFieldNamesTypes() {
Map<String, Object> indexMappings = Map
.of(
"response",
Map.of("type", "integer"),
"responseLatency",
Map.of("type", "float"),
"date",
Map.of("type", "date"),
"objectA",
Map.of("type", "object", "properties", Map.of("subA", Map.of("type", "keyword"))),
"objectB",
Map.of("properties", Map.of("subB", Map.of("type", "keyword"))),
"textC",
Map.of("type", "text", "fields", Map.of("subC", Map.of("type", "keyword"))),
"aliasD",
Map.of("type", "alias", "path", "date")
);
Map<String, String> result = new HashMap<>();
ToolHelper.extractFieldNamesTypes(indexMappings, result, "", true);
assertMapEquals(
result,
Map
.of(
"response",
"integer",
"responseLatency",
"float",
"date",
"date",
"objectA.subA",
"keyword",
"objectB.subB",
"keyword",
"textC",
"text",
"textC.subC",
"keyword"
)
);

Map<String, String> result1 = new HashMap<>();
ToolHelper.extractFieldNamesTypes(indexMappings, result1, "", false);
assertMapEquals(
result1,
Map
.of(
"response",
"integer",
"responseLatency",
"float",
"date",
"date",
"objectA.subA",
"keyword",
"objectB.subB",
"keyword",
"textC",
"text"
)
);
}

private void assertMapEquals(Map<String, String> expected, Map<String, String> actual) {
assertEquals(expected.size(), actual.size());
for (Map.Entry<String, String> entry : expected.entrySet()) {
assertEquals(entry.getValue(), actual.get(entry.getKey()));
}
}
}

0 comments on commit 0c7ba87

Please sign in to comment.