Class Metadata
java.lang.Object
dev.langchain4j.data.document.Metadata
Represents metadata of a
For a
For a
The metadata is stored as a key-value map, where the key is a
Document or a TextSegment.
For a
Document, the metadata could store information such as the source, creation date,
owner, or any other relevant details.
For a
TextSegment, in addition to metadata inherited from a Document, it can also include
segment-specific information, such as the page number, the position of the segment within the document, chapter, etc.
The metadata is stored as a key-value map, where the key is a
String and the value can be one of:
String, UUID, Integer, Long, Float, Double.
If you require additional types, please open an issue.
null values are not permitted.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(String key) Check whether thisMetadatacontains a given key.copy()Copies the metadata.booleanstatic MetadataConstructs a Metadata object from a single key-value pair.static MetadataConstructs a Metadata object from a map of key-value pairs.@Nullable DoubleReturns theDoublevalue associated with the given key.@Nullable FloatReturns theFloatvalue associated with the given key.@Nullable IntegergetInteger(String key) Returns theIntegervalue associated with the given key.@Nullable LongReturns theLongvalue associated with the given key.@Nullable StringReturns theStringvalue associated with the given key.@Nullable UUIDReturns theUUIDvalue associated with the given key.inthashCode()Merges the current Metadata object with another Metadata object.static MetadataConstructs a Metadata object from a single key-value pair.Adds a key-value pair to the metadata.Adds a key-value pair to the metadata.Adds a key-value pair to the metadata.Adds a key-value pair to the metadata.Adds a key-value pair to the metadata.Adds a key-value pair to the metadata.Removes the given key from the metadata.toMap()Get a copy of the metadata as a map of key-value pairs.toString()
-
Constructor Details
-
Metadata
public Metadata()Construct a Metadata object with an empty map of key-value pairs. -
Metadata
-
-
Method Details
-
getString
Returns theStringvalue associated with the given key.- Parameters:
key- the key- Returns:
- the
Stringvalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is not of type String
-
getUUID
Returns theUUIDvalue associated with the given key.- Parameters:
key- the key- Returns:
- the
UUIDvalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is not of type String
-
getInteger
Returns theIntegervalue associated with the given key.
SomeEmbeddingStoreimplementations (still) storeMetadatavalues asStrings. In this case, theStringvalue will be parsed into anIntegerwhen this method is called.
SomeEmbeddingStoreimplementations storeMetadatakey-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize anIntegerand then deserialize it as aLong. Or serialize aFloatand then deserialize it as aDouble, and so on. In such cases, the actual value will be cast to anIntegerwhen this method is called.- Parameters:
key- the key- Returns:
- the
Integervalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is notNumber
-
getLong
Returns theLongvalue associated with the given key.
SomeEmbeddingStoreimplementations (still) storeMetadatavalues asStrings. In this case, theStringvalue will be parsed into anLongwhen this method is called.
SomeEmbeddingStoreimplementations storeMetadatakey-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize anIntegerand then deserialize it as aLong. Or serialize aFloatand then deserialize it as aDouble, and so on. In such cases, the actual value will be cast to aLongwhen this method is called.- Parameters:
key- the key- Returns:
- the
Longvalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is notNumber
-
getFloat
Returns theFloatvalue associated with the given key.
SomeEmbeddingStoreimplementations (still) storeMetadatavalues asStrings. In this case, theStringvalue will be parsed into aFloatwhen this method is called.
SomeEmbeddingStoreimplementations storeMetadatakey-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize anIntegerand then deserialize it as aLong. Or serialize aFloatand then deserialize it as aDouble, and so on. In such cases, the actual value will be cast to aFloatwhen this method is called.- Parameters:
key- the key- Returns:
- the
Floatvalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is notNumber
-
getDouble
Returns theDoublevalue associated with the given key.
SomeEmbeddingStoreimplementations (still) storeMetadatavalues asStrings. In this case, theStringvalue will be parsed into aDoublewhen this method is called.
SomeEmbeddingStoreimplementations storeMetadatakey-value pairs as JSON. In this case, type information is lost when serializing to JSON and then deserializing back from JSON. JSON libraries can, for example, serialize anIntegerand then deserialize it as aLong. Or serialize aFloatand then deserialize it as aDouble, and so on. In such cases, the actual value will be cast to aDoublewhen this method is called.- Parameters:
key- the key- Returns:
- the
Doublevalue associated with the given key, ornullif the key is not present. - Throws:
RuntimeException- if the value is notNumber
-
containsKey
Check whether thisMetadatacontains a given key.- Parameters:
key- the key- Returns:
trueif this metadata contains a given key;falseotherwise.
-
put
-
put
-
put
-
put
-
put
-
put
-
putAll
-
remove
-
copy
-
toMap
-
equals
-
hashCode
-
toString
-
from
-
from
-
metadata
-
merge
Merges the current Metadata object with another Metadata object. The two Metadata objects must not have any common keys.- Parameters:
another- The Metadata object to be merged with the current Metadata object.- Returns:
- A new Metadata object that contains all key-value pairs from both Metadata objects.
- Throws:
IllegalArgumentException- if there are common keys between the two Metadata objects.
-