@@ -41,7 +41,7 @@ class Documents implements \ArrayAccess
4141 public function __construct (string $ collectionName , ApiCall $ apiCall )
4242 {
4343 $ this ->collectionName = $ collectionName ;
44- $ this ->apiCall = $ apiCall ;
44+ $ this ->apiCall = $ apiCall ;
4545 }
4646
4747 /**
@@ -56,14 +56,35 @@ private function endPointPath(string $action = ''): string
5656
5757 /**
5858 * @param array $document
59- * @param array $options
6059 *
6160 * @return array
6261 * @throws TypesenseClientError|GuzzleException
6362 */
64- public function create (array $ document , array $ options = []): array
63+ public function create (array $ document ): array
64+ {
65+ return $ this ->apiCall ->post ($ this ->endPointPath ('' ), $ document , true );
66+ }
67+
68+ /**
69+ * @param array $document
70+ *
71+ * @return array
72+ * @throws TypesenseClientError|GuzzleException
73+ */
74+ public function upsert (array $ document ): array
75+ {
76+ return $ this ->apiCall ->post ($ this ->endPointPath ('' ), $ document , true , ['mode ' => 'upsert ' ]);
77+ }
78+
79+ /**
80+ * @param array $document
81+ *
82+ * @return array
83+ * @throws TypesenseClientError|GuzzleException
84+ */
85+ public function update (array $ document ): array
6586 {
66- return $ this ->apiCall ->post ($ this ->endPointPath ('' ), $ document , true , $ options );
87+ return $ this ->apiCall ->post ($ this ->endPointPath ('' ), $ document , true , [ ' mode ' => ' update ' ] );
6788 }
6889
6990 /**
@@ -75,32 +96,49 @@ public function create(array $document, array $options = []): array
7596 */
7697 public function createMany (array $ documents , array $ options = []): array
7798 {
78- $ res = $ this ->import (
79- implode (
80- "\n" ,
81- array_map (
82- static fn (array $ document ) => json_encode ($ document , JSON_THROW_ON_ERROR ),
83- $ documents
84- )
85- ),
86- $ options
99+ $ this ->apiCall ->getLogger ()->warning (
100+ "createMany is deprecated and will be removed in a future version. " .
101+ "Use import instead, which now takes both an array of documents or a JSONL string of documents "
87102 );
88- return array_map (static function ($ item ) {
89- return json_decode ($ item , true , 512 , JSON_THROW_ON_ERROR );
90- }, explode ("\n" , $ res ));
103+ return $ this ->import ($ documents , $ options );
91104 }
92105
93106 /**
94- * @param string $documents
107+ * @param string|array $documents
95108 * @param array $options
96109 *
97- * @return string
110+ * @return string|array
98111 * @throws TypesenseClientError
99112 * @throws GuzzleException
113+ * @throws \JsonException
100114 */
101- public function import (string $ documents , array $ options = []): string
115+ public function import ($ documents , array $ options = [])
102116 {
103- return $ this ->apiCall ->post ($ this ->endPointPath ('import ' ), $ documents , false , $ options );
117+ if (is_array ($ documents )) {
118+ $ documentsInJSONLFormat = implode (
119+ "\n" ,
120+ array_map (
121+ static fn (array $ document ) => json_encode ($ document , JSON_THROW_ON_ERROR ),
122+ $ documents
123+ )
124+ );
125+ } else {
126+ $ documentsInJSONLFormat = $ documents ;
127+ }
128+ $ resultsInJSONLFormat = $ this ->apiCall ->post (
129+ $ this ->endPointPath ('import ' ),
130+ $ documentsInJSONLFormat ,
131+ false ,
132+ $ options
133+ );
134+
135+ if (is_array ($ documents )) {
136+ return array_map (static function ($ item ) {
137+ return json_decode ($ item , true , 512 , JSON_THROW_ON_ERROR );
138+ }, explode ("\n" , $ resultsInJSONLFormat ));
139+ } else {
140+ return $ resultsInJSONLFormat ;
141+ }
104142 }
105143
106144 /**
0 commit comments