Package dev.langchain4j.data.document
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 TypeMethodDescriptionDeprecated.Deprecated.as of 0.31.0, useput(String, String),put(String, int),put(String, long),put(String, float),put(String, double)instead.asMap()Deprecated.as of 0.31.0, usetoMap()instead.booleancontainsKey(String key) Check whether thisMetadatacontains a given key.copy()Copies the metadata.booleanstatic MetadataDeprecated.Usefrom(String, String)insteadstatic MetadataConstructs a Metadata object from a single key-value pair.static MetadataConstructs a Metadata object from a map of key-value pairs.Deprecated.as of 0.31.0, usegetString(String),getInteger(String),getLong(String),getFloat(String),getDouble(String)instead.Returns theDoublevalue associated with the given key.Returns theFloatvalue associated with the given key.getInteger(String key) Returns theIntegervalue associated with the given key.Returns theLongvalue associated with the given key.Returns theStringvalue associated with the given key.Returns theUUIDvalue associated with the given key.inthashCode()static MetadataDeprecated.Usemetadata(String, String)insteadstatic 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
Constructs a Metadata object from a map of key-value pairs.
-
-
Method Details
-
get
Deprecated.as of 0.31.0, usegetString(String),getInteger(String),getLong(String),getFloat(String),getDouble(String)instead.Returns the value associated with the given key.- Parameters:
key- the key- Returns:
- the value associated with the given key, or
nullif the key is not present.
-
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.
-
add
Deprecated.as of 0.31.0, useput(String, String),put(String, int),put(String, long),put(String, float),put(String, double)instead.Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
add
Deprecated.as of 0.31.0, useput(String, String),put(String, int),put(String, long),put(String, float),put(String, double)instead.Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
put
Adds a key-value pair to the metadata.- Parameters:
key- the keyvalue- the value- Returns:
this
-
remove
Removes the given key from the metadata.- Parameters:
key- the key- Returns:
this
-
copy
Copies the metadata.- Returns:
- a copy of this Metadata object.
-
asMap
Deprecated.as of 0.31.0, usetoMap()instead.Get a copy of the metadata as a map of key-value pairs.- Returns:
- the metadata as a map of key-value pairs.
-
toMap
Get a copy of the metadata as a map of key-value pairs.- Returns:
- the metadata as a map of key-value pairs.
-
equals
-
hashCode
public int hashCode() -
toString
-
from
Constructs a Metadata object from a single key-value pair.- Parameters:
key- the keyvalue- the value- Returns:
- a Metadata object
-
from
Deprecated.Usefrom(String, String)instead- Parameters:
key- the keyvalue- the value- Returns:
- a Metadata object
-
from
Constructs a Metadata object from a map of key-value pairs.- Parameters:
metadata- the map of key-value pairs- Returns:
- a Metadata object
-
metadata
Constructs a Metadata object from a single key-value pair.- Parameters:
key- the keyvalue- the value- Returns:
- a Metadata object
-
metadata
Deprecated.Usemetadata(String, String)instead- Parameters:
key- the keyvalue- the value- Returns:
- a Metadata object
-
put(String, String),put(String, int),put(String, long),put(String, float),put(String, double)instead.