@@ -31,13 +31,12 @@ impl<'a> Documents<'a> {
31
31
32
32
/// Indexes a document in the collection.
33
33
///
34
- /// If the document has an 'id' field, it will be used as the document's ID.
35
- /// Otherwise, Typesense will auto-generate an ID.
34
+
36
35
///
37
36
/// # Arguments
38
37
/// * `document` - A `serde_json::Value` representing the document.
39
- /// * `action` - The indexing action to perform (e.g., "create", "upsert", "update" ).
40
- pub async fn index (
38
+ /// * `action` - The indexing action to perform (e.g., "create", "upsert").
39
+ async fn index (
41
40
& self ,
42
41
document : serde_json:: Value ,
43
42
action : & str ,
@@ -56,67 +55,50 @@ impl<'a> Documents<'a> {
56
55
. await
57
56
}
58
57
59
- /// Fetches an individual document from the collection by its ID.
58
+ /// Creates a new document in the collection.
59
+ /// Fails if a document with the same id already exists
60
+ ///
61
+ /// If the document has an `id` field of type `string`, it will be used as the document's ID.
62
+ /// Otherwise, Typesense will auto-generate an ID.
60
63
///
61
64
/// # Arguments
62
- /// * `document_id ` - The ID of the document to retrieve .
63
- pub async fn retrieve (
65
+ /// * `document ` - A `serde_json::Value` representing the document to create .
66
+ pub async fn create (
64
67
& self ,
65
- document_id : & str ,
66
- ) -> Result < serde_json:: Value , Error < documents_api:: GetDocumentError > > {
67
- let params = documents_api:: GetDocumentParams {
68
- collection_name : self . collection_name . to_string ( ) ,
69
- document_id : document_id. to_string ( ) ,
70
- } ;
71
-
72
- self . client
73
- . execute ( |config : Arc < configuration:: Configuration > | {
74
- let params_for_move = params. clone ( ) ;
75
- async move { documents_api:: get_document ( & config, params_for_move) . await }
76
- } )
77
- . await
68
+ document : serde_json:: Value ,
69
+ ) -> Result < serde_json:: Value , Error < documents_api:: IndexDocumentError > > {
70
+ self . index ( document, "create" ) . await
78
71
}
79
72
80
- /// Updates an individual document from the collection by its ID. The update can be partial.
73
+ /// Creates a new document or updates an existing document if a document with the same id already exists.
74
+ /// Requires the whole document to be sent. For partial updates, use the `update()` action.
81
75
///
82
76
/// # Arguments
83
- /// * `document_id` - The ID of the document to update.
84
- /// * `document` - A `serde_json::Value` containing the fields to update.
85
- pub async fn update (
77
+ /// * `document` - A `serde_json::Value` representing the document to upsert.
78
+ pub async fn upsert (
86
79
& self ,
87
- document_id : & str ,
88
80
document : serde_json:: Value ,
89
- ) -> Result < serde_json:: Value , Error < documents_api:: UpdateDocumentError > > {
90
- let params = documents_api:: UpdateDocumentParams {
91
- collection_name : self . collection_name . to_string ( ) ,
92
- document_id : document_id. to_string ( ) ,
93
- body : document,
94
- dirty_values : None ,
95
- } ;
96
- self . client
97
- . execute ( |config : Arc < configuration:: Configuration > | {
98
- let params_for_move = params. clone ( ) ;
99
- async move { documents_api:: update_document ( & config, params_for_move) . await }
100
- } )
101
- . await
81
+ ) -> Result < serde_json:: Value , Error < documents_api:: IndexDocumentError > > {
82
+ self . index ( document, "upsert" ) . await
102
83
}
103
84
104
- /// Deletes an individual document from the collection by its ID.
85
+ /// Fetches an individual document from the collection by its ID.
105
86
///
106
87
/// # Arguments
107
- /// * `document_id` - The ID of the document to delete .
108
- pub async fn delete (
88
+ /// * `document_id` - The ID of the document to retrieve .
89
+ pub async fn retrieve (
109
90
& self ,
110
91
document_id : & str ,
111
- ) -> Result < serde_json:: Value , Error < documents_api:: DeleteDocumentError > > {
112
- let params = documents_api:: DeleteDocumentParams {
92
+ ) -> Result < serde_json:: Value , Error < documents_api:: GetDocumentError > > {
93
+ let params = documents_api:: GetDocumentParams {
113
94
collection_name : self . collection_name . to_string ( ) ,
114
95
document_id : document_id. to_string ( ) ,
115
96
} ;
97
+
116
98
self . client
117
99
. execute ( |config : Arc < configuration:: Configuration > | {
118
100
let params_for_move = params. clone ( ) ;
119
- async move { documents_api:: delete_document ( & config, params_for_move) . await }
101
+ async move { documents_api:: get_document ( & config, params_for_move) . await }
120
102
} )
121
103
. await
122
104
}
0 commit comments