Skip to content

Commit 97145a0

Browse files
committed
Update filtering to support HashSet-based approach for map keys with dots
Signed-off-by: hye-on <[email protected]>
1 parent 9c60c7e commit 97145a0

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

server/src/main/java/org/opensearch/common/xcontent/support/XContentMapValues.java

+8-17
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,10 @@ public static Map<String, Object> filter(Map<String, ?> map, String[] includes,
219219
* @see #filter(Map, String[], String[]) for details
220220
*/
221221
public static Function<Map<String, ?>, Map<String, Object>> filter(String[] includes, String[] excludes) {
222-
223-
return (map) -> {
224-
if (hasNoDottedKeys(map) && hasNoWildcardsOrDots(includes) && hasNoWildcardsOrDots(excludes)) {
225-
return createSetBasedFilter(includes, excludes).apply(map);
226-
}
227-
return createAutomatonFilter(includes, excludes).apply(map);
228-
};
229-
}
230-
231-
private static boolean hasNoDottedKeys(Map<String, ?> map) {
232-
for (String key : map.keySet()) {
233-
if (key.indexOf('.') != -1) {
234-
return false;
235-
}
222+
if (hasNoWildcardsOrDots(includes) && hasNoWildcardsOrDots(excludes)) {
223+
return createSetBasedFilter(includes, excludes);
236224
}
237-
return true;
225+
return createAutomatonFilter(includes, excludes);
238226
}
239227

240228
private static boolean hasNoWildcardsOrDots(String[] fields) {
@@ -254,7 +242,6 @@ private static boolean hasNoWildcardsOrDots(String[] fields) {
254242
* Creates a simple HashSet-based filter for exact field name matching
255243
*/
256244
private static Function<Map<String, ?>, Map<String, Object>> createSetBasedFilter(String[] includes, String[] excludes) {
257-
258245
Set<String> includeSet = (includes == null || includes.length == 0) ? null : new HashSet<>(Arrays.asList(includes));
259246
Set<String> excludeSet = (excludes == null || excludes.length == 0)
260247
? Collections.emptySet()
@@ -264,8 +251,12 @@ private static boolean hasNoWildcardsOrDots(String[] fields) {
264251
Map<String, Object> filtered = new HashMap<>();
265252
for (Map.Entry<String, ?> entry : map.entrySet()) {
266253
String key = entry.getKey();
254+
int dotPos = key.indexOf('.');
255+
if (dotPos > 0) {
256+
key = key.substring(0, dotPos);
257+
}
267258
if ((includeSet == null || includeSet.contains(key)) && !excludeSet.contains(key)) {
268-
filtered.put(key, entry.getValue());
259+
filtered.put(entry.getKey(), entry.getValue());
269260
}
270261
}
271262
return filtered;

0 commit comments

Comments
 (0)