Interface Source


public interface Source
A read-only view of a document's source.

This interface provides access to source content as either bytes or a parsed map. A Source instance represents a stable snapshot of a particular document's data.

Note: The Map returned by source() may or may not be immutable. Use withMutations(Consumer) for any mutations on the Map returned by source().

  • Method Details

    • sourceContentType

      XContentType sourceContentType()
      The content type of the source, if stored as bytes
    • source

      Map<String,Object> source()
      A map representation of the source.

      IMPORTANT: The returned map may be immutable. To modify the source, use withMutations(java.util.function.Consumer) instead.

      This can lose precision on numbers with a decimal point. It converts numbers like "n": 1234.567 to a double which only has 52 bits of precision in the mantissa. This will come up most frequently when folks write nanosecond precision dates as a decimal number.

    • internalSourceRef

      BytesReference internalSourceRef()
      A byte representation of the source
    • filter

      Source filter(SourceFilter sourceFilter)
      Apply a filter to this source, returning a new Source
    • withMutations

      default Source withMutations(Consumer<Map<String,Object>> mutator)
      Returns a new Source with mutations applied to the map.

      Use this when you need to add, remove, or update fields on a map that may or may not be mutable.

      WARNING: If the underlying source map is already a HashMap (including LinkedHashMap), the original map will be mutated directly rather than copied. This is intentional for performance, but callers should be aware that the original Source's map may be modified as a side effect.

         Source modified = source.withMutations(map -> map.put("field", "value"));
       
      Parameters:
      mutator - a function that modifies the map
      Returns:
      a new Source with the mutations applied. There is no guarantee about the mutability of the returned value's source() map.
    • extractValue

      default Object extractValue(String path, @Nullable Object nullValue)
      For the provided path, return its value in the source. Both array and object values can be returned.
      Parameters:
      path - the value's path in the source.
      nullValue - a value to return if the path exists, but the value is 'null'. This helps in distinguishing between a path that doesn't exist vs. a value of 'null'.
      Returns:
      the value associated with the path in the source or 'null' if the path does not exist.
    • empty

      static Source empty(XContentType xContentType)
      An empty Source, represented as an empty map
    • fromBytes

      static Source fromBytes(BytesReference bytes)
      Build a Source from a bytes representation with an unknown XContentType
    • fromBytes

      static Source fromBytes(BytesReference bytes, XContentType type)
      Build a Source from a bytes representation with a known XContentType
    • fromMap

      static Source fromMap(Map<String,Object> map, XContentType xContentType)
      Build a Source from a Map representation. Note that null is accepted as an input and interpreted as an empty map
      Parameters:
      map - the java Map to use as a source
      xContentType - the XContentType to serialize this source
    • lazy

      static Source lazy(Supplier<Source> sourceSupplier)
      Build a source lazily if one of its methods is called