java.lang.Object
com.datastax.astra.client.collections.definition.documents.Document
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Filter, Update

public class Document extends Object implements Serializable
Represents a document without schema constraints as a Map<String, Object>.(key/value)
See Also:
  • Field Details Link icon

    • SERIALIZER Link icon

      protected static final DataAPISerializer SERIALIZER
      Serializer
    • documentMap Link icon

      public transient Map<String,Object> documentMap
      Data to be used in the document.
  • Constructor Details Link icon

    • Document Link icon

      public Document()
      Default Constructor.
    • Document Link icon

      public Document(Object id)
      Default Constructor.
      Parameters:
      id - provide the unique identifier.
    • Document Link icon

      public Document(Map<String,?> map)
      Creates a Document instance initialized with the given map.
      Parameters:
      map - initial map
  • Method Details Link icon

    • getDocumentMap Link icon

      public Map<String,Object> getDocumentMap()
      Getter for the document map.
      Returns:
      document map
    • setProperty Link icon

      public void setProperty(String key, Object value)
      Setter for a property in the document map
      Parameters:
      key - property name
      value - property value
    • create Link icon

      public static Document create(Object id)
      Create a document with an id.
      Parameters:
      id - document identifier
      Returns:
      instance of document
    • create Link icon

      public static Document create()
      Create a document with no attributes.
      Returns:
      instance of document
    • map Link icon

      public <T> T map(Class<T> clazz)
      Marshall as a document if needed.
      Type Parameters:
      T - current type
      Parameters:
      clazz - target class
      Returns:
      instance of pojo
    • parse Link icon

      public static Document parse(String json)
      Parses a string in MongoDB Extended JSON format to a Document
      Parameters:
      json - the JSON string
      Returns:
      the document
    • append Link icon

      public Document append(String[] keys, Object value)
      Put the given key/value pair into this Document and return this. Useful for chaining puts in a single expression, e.g.
       doc.append("a", 1).append("b", 2)}
       
      Parameters:
      keys - list of keys to add to a document
      value - value
      Returns:
      this
    • append Link icon

      public Document append(String key, Object value)
      Put the given key/value pair into this Document and return this. Useful for chaining puts in a single expression, e.g.
       doc.append("a", 1).append("b", 2)}
       
      Parameters:
      key - key
      value - value
      Returns:
      this
    • appendIfNotNull Link icon

      public Document appendIfNotNull(String key, Object value)
      Put the given key/value pair into this Document and return this only if the value is not null.
       doc.append("a", 1).append("b", 2)}
       
      Parameters:
      key - key
      value - value
      Returns:
      this
    • get Link icon

      public <T> T get(@NonNull @NonNull String key, @NonNull @NonNull Class<T> clazz)
      Gets the value of the given key, casting it to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same. So to get the value of a key that is of type String, you would write String name = doc.get("name", String.class) instead of String name = (String) doc.get("x") .
      Type Parameters:
      T - the type of the class
      Parameters:
      key - the key
      clazz - the non-null class to cast the value to
      Returns:
      the value of the given key, or null if the instance does not contain this key.
      Throws:
      ClassCastException - if the value of the given key is not of type T
    • getId Link icon

      public <T> T getId(@NonNull @NonNull Class<T> clazz)
      Access the document identifier
      Type Parameters:
      T - type of id
      Parameters:
      clazz - can be different type
      Returns:
      value for object
    • id Link icon

      public <T> Document id(T id)
      Set value for the identifier.
      Type Parameters:
      T - type of id
      Parameters:
      id - id value
      Returns:
      self reference
    • vectorize Link icon

      public Document vectorize(String passage)
      Add a vectorize attribute to the document.
      Parameters:
      passage - value for the vectorize attribute
      Returns:
      self reference
    • vectorize Link icon

      public Document vectorize(Vectorize vectorize)
      Add a vectorize attribute to the document.
      Parameters:
      vectorize - value for the vectorize attribute
      Returns:
      self reference
    • vectorize Link icon

      public Document vectorize(String passage, String setPassage)
      Add a vectorize attribute to the document.
      Parameters:
      passage - string to converted to vector
      setPassage - field name in the document to store the vectorize string
      Returns:
      self reference
    • hybrid Link icon

      public Document hybrid(Hybrid hybrid)
      Add a hybrid attribute in the documentW
      Parameters:
      hybrid - value for hybrid
      Returns:
      self reference
    • lexical Link icon

      public Document lexical(String text)
      Add a vectorize attribute to the document.
      Parameters:
      text - value for the vectorize attribute
      Returns:
      self reference
    • getLexical Link icon

      public Optional<String> getLexical()
      Access attribute with vectorize name if any.
      Returns:
      value for vectorize
    • getVectorize Link icon

      public Optional<String> getVectorize()
      Access attribute with vectorize name if any.
      Returns:
      value for vectorize
    • getVector Link icon

      public Optional<DataAPIVector> getVector()
      Get value for vector.
      Returns:
      vector list
    • vector Link icon

      public Document vector(float[] vector)
      Set the vector value.
      Parameters:
      vector - vector value
      Returns:
      self reference
    • vector Link icon

      public Document vector(DataAPIVector vector)
      Set the vector value.
      Parameters:
      vector - vector value
      Returns:
      self reference
    • getSimilarity Link icon

      public Optional<Double> getSimilarity()
      Get value for similarity.
      Returns:
      vector list
    • getInteger Link icon

      public Integer getInteger(String key)
      Gets the value of the given key as an Integer.
      Parameters:
      key - the key
      Returns:
      the value as an integer, which may be null
      Throws:
      ClassCastException - if the value is not an integer
    • getLong Link icon

      public Long getLong(String key)
      Gets the value of the given key as a Long.
      Parameters:
      key - the key
      Returns:
      the value as a long, which may be null
      Throws:
      ClassCastException - if the value is not a long
    • getDouble Link icon

      public Double getDouble(String key)
      Gets the value of the given key as a Double.
      Parameters:
      key - the key
      Returns:
      the value as a double, which may be null
      Throws:
      ClassCastException - if the value is not a double
    • getString Link icon

      public String getString(String key)
      Gets the value of the given key as a String.
      Parameters:
      key - the key
      Returns:
      the value as a String, which may be null
      Throws:
      ClassCastException - if the value is not a String
    • getBoolean Link icon

      public Boolean getBoolean(String key)
      Gets the value of the given key as a Boolean.
      Parameters:
      key - the key
      Returns:
      the value as a Boolean, which may be null
      Throws:
      ClassCastException - if the value is not a boolean
    • getArray Link icon

      public <K> K[] getArray(String k, Class<K> itemClass)
      Return an Array of items.
      Type Parameters:
      K - type of item
      Parameters:
      k - key
      itemClass - expected class
      Returns:
      list of items
    • getObjectId Link icon

      public ObjectId getObjectId(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getUUID Link icon

      public UUID getUUID(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getFloat Link icon

      public Float getFloat(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getShort Link icon

      public Short getShort(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getByte Link icon

      public Byte getByte(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getCharacter Link icon

      public Character getCharacter(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • ge Link icon

      public Date ge(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getCalendar Link icon

      public Calendar getCalendar(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getInstant Link icon

      public Instant getInstant(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getDate Link icon

      public Date getDate(String k)
      Access element from the map
      Parameters:
      k - current configuration key
      Returns:
      configuration value
    • getList Link icon

      public <T> List<T> getList(@NonNull @NonNull String key, @NonNull @NonNull Class<T> clazz)
      Gets the list value of the given key, casting the list elements to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same.
      Type Parameters:
      T - the type of the class
      Parameters:
      key - the key
      clazz - the non-null class to cast the list value to
      Returns:
      the list value of the given key, or null if the instance does not contain this key.
      Throws:
      ClassCastException - if the elements in the list value of the given key is not of type T or the value is not a list
      Since:
      3.10
    • getMap Link icon

      public <K, V> Map<K,V> getMap(@NonNull @NonNull String key, @NonNull @NonNull Class<K> keyClass, @NonNull @NonNull Class<V> valueClass)
      Gets the list value of the given key, casting the list elements to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same.
      Type Parameters:
      K - type of the key
      V - type of the value
      Parameters:
      key - the key
      keyClass - the non-null class to cast the list value to
      valueClass - the type of the value class
      Returns:
      the list value of the given key, or null if the instance does not contain this key.
      Throws:
      ClassCastException - if the elements in the list value of the given key is not of type T or the value is not a list
      Since:
      3.10
    • toString Link icon

      public String toString()
      Serialization with Jackson.
      Overrides:
      toString in class Object
      Returns:
      json string
    • toJson Link icon

      public String toJson()
      Serialization with Jackson.
      Returns:
      json string
    • containsKey Link icon

      public boolean containsKey(String key)
      Check if the given dot-delimited path exists as a key (final segment). e.g. containsKey("foo.bar") returns true if "bar" is present in the Map located at "foo".
      Parameters:
      key - for key
      Returns:
      if the key is present
    • get Link icon

      public Object get(String[] fieldPathSegment)
      Check if the given dot-delimited path exists as a key (final segment). e.g. containsKey("foo.bar") returns true if "bar" is present in the Map located at "foo".
      Parameters:
      fieldPathSegment - for key
      Returns:
      if the key is present
    • get Link icon

      public Object get(String key)
      Retrieves the value associated with the specified key from the document.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value associated with the specified key, or null if the key is not found
    • put Link icon

      public Document put(String key, Object value)
      Associates the specified value with the specified key in the document. If the key already has a value, the old value is replaced.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the previous value associated with the key, or null if there was no mapping for the key
    • put Link icon

      public Document put(String[] keys, Object value)
      Associates the specified value with the specified key in the document. If the key already has a value, the old value is replaced.
      Parameters:
      keys - the keys for the field segment
      value - the value to be associated with the specified key
      Returns:
      the previous value associated with the key, or null if there was no mapping for the key
    • remove Link icon

      public Document remove(String key)
      Removes the mapping for a key from the document if it is present.
      Parameters:
      key - the key whose mapping is to be removed
      Returns:
      the value that was associated with the key, or null if the key was not mapped
    • putAll Link icon

      public void putAll(Map<? extends String,?> map)
      Copies all mappings from the specified map to this document. Existing mappings will be replaced with mappings from the provided map.
      Parameters:
      map - the map containing mappings to be copied to this document
    • clear Link icon

      public void clear()
      Removes all mappings from this document. The document will be empty after this operation.
    • equals Link icon

      public boolean equals(Object o)
      Compares this document to another object for equality. Two documents are considered equal if their underlying maps are equal.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with this document
      Returns:
      true if the specified object is equal to this document, false otherwise
    • hashCode Link icon

      public int hashCode()
      Returns the hash code value for this document. The hash code is computed based on the underlying map.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this document