Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includeFields parameter to the method extractFieldNamesTypes #376

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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 @@ -402,7 +402,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 @@ -40,8 +40,15 @@ public static Map<String, String> loadDefaultPromptDictFromFile(Class<?> source,
* @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 @@ -53,15 +60,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()));
}
}
}
Loading