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 Summary
Modifier and TypeMethodDescriptionstatic Sourceempty(XContentType xContentType) An empty Source, represented as an empty mapdefault ObjectextractValue(String path, Object nullValue) For the provided path, return its value in the source.filter(SourceFilter sourceFilter) Apply a filter to this source, returning a new Sourcestatic SourcefromBytes(BytesReference bytes) Build a Source from a bytes representation with an unknown XContentTypestatic SourcefromBytes(BytesReference bytes, XContentType type) Build a Source from a bytes representation with a known XContentTypestatic SourcefromMap(Map<String, Object> map, XContentType xContentType) Build a Source from a Map representation.A byte representation of the sourcestatic SourceBuild a source lazily if one of its methods is calledsource()A map representation of the source.The content type of the source, if stored as bytesdefault SourcewithMutations(Consumer<Map<String, Object>> mutator) Returns a new Source with mutations applied to the map.
-
Method Details
-
sourceContentType
XContentType sourceContentType()The content type of the source, if stored as bytes -
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.567to adoublewhich 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
Apply a filter to this source, returning a new Source -
withMutations
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(includingLinkedHashMap), 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
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
An empty Source, represented as an empty map -
fromBytes
Build a Source from a bytes representation with an unknown XContentType -
fromBytes
Build a Source from a bytes representation with a known XContentType -
fromMap
Build a Source from a Map representation. Note thatnullis accepted as an input and interpreted as an empty map- Parameters:
map- the java Map to use as a sourcexContentType- the XContentType to serialize this source
-
lazy
Build a source lazily if one of its methods is called
-