Class ElasticsearchUtil


  • public final class ElasticsearchUtil
    extends Object
    Utilities for working with Elasticsearch.
    • Field Detail

      • OBJECT_MAPPER

        public static final com.fasterxml.jackson.databind.ObjectMapper OBJECT_MAPPER
        Jackson ObjectMapper for working with JSON.
      • NORMALIZED_FIELD

        protected static final String NORMALIZED_FIELD
        JSON field-name of metadata to assist in de-normalization.
        See Also:
        Constant Field Values
      • FIELD_NAMES_FIELD

        protected static final String FIELD_NAMES_FIELD
        JSON field-name of normalized field-names.
        See Also:
        Constant Field Values
    • Method Detail

      • normalizeJson

        public static String normalizeJson​(JsonValue value)
                                    throws IOException
        Normalizes JSON to conform to Elasticsearch data-format restrictions. The following normalizations are performed,
        • Periods in JSON fields (keys) are converted to underscore characters
        The following metadata, for example, is added to the Normalized JSON, to facilitate de-normalization,
             "_normalized" : {
                 "fieldNames" : {
                     "key_1" : "key.1",
                     "key_2" : "key.2"
                 }
             }
         
        Parameters:
        value - JSON value
        Returns:
        Resulting JSON, with _normalized field if any normalization was necessary
        Throws:
        IOException - If unable to parse the json.
      • replaceKeyPeriodsWithUnderscores

        protected static String replaceKeyPeriodsWithUnderscores​(String json)
                                                          throws IOException
        Replaces all period-characters in JSON keys with underscore-characters [ref]. If normalization is required, the fieldNames field will be added to the normalized metadata.
        Parameters:
        json - JSON String input
        Returns:
        Resulting JSON String
        Throws:
        IOException - If unable to parse the json.
      • renameField

        public static boolean renameField​(JsonValue jsonValue,
                                          String oldKey,
                                          String newKey)
        Renames a field within the given JsonValue.
        Parameters:
        jsonValue - JsonValue to have a top-level field renamed
        oldKey - Old field name
        newKey - New field name (field must not already exist)
        Returns:
        true if field was found and renamed, and false otherwise