java.lang.Object
org.elasticsearch.xpack.core.ml.utils.MapHelper

public final class MapHelper extends Object
  • Method Details

    • dig

      @Nullable public static Object dig(String path, Map<String,Object> map)
      This eagerly digs (depth first search, longer keys first) through the map by tokenizing the provided path on '.'. It is possible for ES _source docs to have "mixed" path formats. So, we should search all potential paths given the current knowledge of the map. Examples: The following maps would return `2` given the path "a.b.c.d" { "a.b.c.d" : 2 } { "a" :{"b": {"c": {"d" : 2}}} } { "a" :{"b.c": {"d" : 2}}} } { "a" :{"b": {"c": {"d" : 2}}}, "a.b" :{"c": {"d" : 5}} // we choose the first one found, we go down longer keys first } { "a" :{"b": {"c": {"NOT_d" : 2, "d": 2}}} } Conceptual "Worse case" 5 potential paths explored for "a.b.c.d" until 2 is finally returned { "a.b.c": {"not_d": 2}, "a.b": {"c": {"not_d": 2}}, "a": {"b.c": {"not_d": 2}}, "a": {"b" :{ "c.not_d": 2}}, "a" :{"b": {"c": {"not_d" : 2}}}, "a" :{"b": {"c": {"d" : 2}}}, } We don't exhaustively create all potential paths. If we did, this would result in 2^n-1 total possible paths, where n = path.split("\\.").length. Instead we lazily create potential paths once we know that they are possibilities.
      Parameters:
      path - Dot delimited path containing the field desired. Assumes that the path contains no empty strings
      map - The Map map to dig
      Returns:
      The found object. Returns null if not found
    • dotCollapse

      public static Map<String,Object> dotCollapse(Map<String,Object> map, Collection<String> pathsToCollapse)
      Collapses dot delimited fields so that the map is a single layer. Example: { "a" :{"b": {"c": {"d" : 2}}} } becomes: { "a.b.c.d": 2 }
      Parameters:
      map - The map that has nested and/or collapsed paths
      pathsToCollapse - The desired paths to collapse
      Returns:
      A fully collapsed map