@@ -480,6 +480,9 @@ type Result struct {
480480// There can be fewer results if a filter is applied.
481481// - where: Conditional filtering on metadata. Optional.
482482// - whereDocument: Conditional filtering on documents. Optional.
483+ //
484+ // The [Result]'s [Document]-related fields are a deep copy of the original document's
485+ // fields, so they can be safely modified without affecting the collection.
483486func (c * Collection ) Query (ctx context.Context , queryText string , nResults int , where , whereDocument map [string ]string ) ([]Result , error ) {
484487 if queryText == "" {
485488 return nil , errors .New ("queryText is empty" )
@@ -496,6 +499,9 @@ func (c *Collection) Query(ctx context.Context, queryText string, nResults int,
496499// QueryWithOptions performs an exhaustive nearest neighbor search on the collection.
497500//
498501// - options: The options for the query. See [QueryOptions] for more information.
502+ //
503+ // The [Result]'s [Document]-related fields are a deep copy of the original document's
504+ // fields, so they can be safely modified without affecting the collection.
499505func (c * Collection ) QueryWithOptions (ctx context.Context , options QueryOptions ) ([]Result , error ) {
500506 if options .QueryText == "" && len (options .QueryEmbedding ) == 0 {
501507 return nil , errors .New ("QueryText and QueryEmbedding options are empty" )
@@ -553,6 +559,9 @@ func (c *Collection) QueryWithOptions(ctx context.Context, options QueryOptions)
553559// There can be fewer results if a filter is applied.
554560// - where: Conditional filtering on metadata. Optional.
555561// - whereDocument: Conditional filtering on documents. Optional.
562+ //
563+ // The [Result]'s [Document]-related fields are a deep copy of the original document's
564+ // fields, so they can be safely modified without affecting the collection.
556565func (c * Collection ) QueryEmbedding (ctx context.Context , queryEmbedding []float32 , nResults int , where , whereDocument map [string ]string ) ([]Result , error ) {
557566 return c .queryEmbedding (ctx , queryEmbedding , nil , 0 , nResults , where , whereDocument )
558567}
@@ -611,11 +620,12 @@ func (c *Collection) queryEmbedding(ctx context.Context, queryEmbedding, negativ
611620
612621 res := make ([]Result , 0 , len (nMaxDocs ))
613622 for i := 0 ; i < len (nMaxDocs ); i ++ {
623+ doc := c .documents [nMaxDocs [i ].docID ]
614624 res = append (res , Result {
615625 ID : nMaxDocs [i ].docID ,
616- Metadata : c . documents [ nMaxDocs [ i ]. docID ]. Metadata ,
617- Embedding : c . documents [ nMaxDocs [ i ]. docID ]. Embedding ,
618- Content : c . documents [ nMaxDocs [ i ]. docID ] .Content ,
626+ Metadata : maps . Clone ( doc . Metadata ) ,
627+ Embedding : slices . Clone ( doc . Embedding ) ,
628+ Content : doc .Content ,
619629 Similarity : nMaxDocs [i ].similarity ,
620630 })
621631 }
0 commit comments