@@ -49,29 +49,41 @@ public function __construct(?string $host = null)
4949 * @param string $index The Elasticsearch index name.
5050 * @param string $id The unique document ID.
5151 * @param array<string, mixed> $body The document body to be stored.
52- * @return bool True if successful, false otherwise.
52+ * @return void
53+ *
54+ * @throws \RuntimeException if the request fails
5355 */
54- public function indexDocument (string $ index , string $ id , array $ body ): bool
56+ public function indexDocument (string $ index , string $ id , array $ body ): void
5557 {
5658 $ url = "{$ this ->host }/ {$ index }/_doc/ {$ id }" ;
5759 $ response = Http::put ($ url , $ body );
5860
59- return $ response ->successful ();
61+ if (!$ response ->successful ()) {
62+ throw new \RuntimeException (
63+ "Failed to index document to Elasticsearch [ {$ url }]: " . $ response ->body ()
64+ );
65+ }
6066 }
6167
6268 /**
6369 * Delete a document from Elasticsearch by its ID.
6470 *
6571 * @param string $index The Elasticsearch index name.
6672 * @param string $id The document ID to delete.
67- * @return bool True if successful, false otherwise.
73+ * @return void
74+ *
75+ * @throws \RuntimeException if the request fails
6876 */
69- public function deleteDocument (string $ index , string $ id ): bool
77+ public function deleteDocument (string $ index , string $ id ): void
7078 {
7179 $ url = "{$ this ->host }/ {$ index }/_doc/ {$ id }" ;
7280 $ response = Http::delete ($ url );
7381
74- return $ response ->successful ();
82+ if (!$ response ->successful ()) {
83+ throw new \RuntimeException (
84+ "Failed to delete document from Elasticsearch [ {$ url }]: " . $ response ->body ()
85+ );
86+ }
7587 }
7688
7789 /**
@@ -80,12 +92,35 @@ public function deleteDocument(string $index, string $id): bool
8092 * @param string $index The Elasticsearch index name.
8193 * @param array<string, mixed> $query The Elasticsearch query body.
8294 * @return array<int, mixed> The array of matching documents (hits).
95+ *
96+ * @throws \RuntimeException if the request fails
8397 */
8498 public function search (string $ index , array $ query ): array
8599 {
86100 $ url = "{$ this ->host }/ {$ index }/_search " ;
87101 $ response = Http::post ($ url , $ query );
88102
103+ if (!$ response ->successful ()) {
104+ throw new \RuntimeException (
105+ "Failed to search Elasticsearch [ {$ url }]: " . $ response ->body ()
106+ );
107+ }
108+
89109 return $ response ->json ('hits.hits ' , []);
90110 }
111+
112+ /**
113+ * Delete documents matching a query from an Elasticsearch index.
114+ *
115+ * @param string $index The Elasticsearch index name.
116+ * @param array<string, mixed> $query The Elasticsearch query body.
117+ * @return bool True if successful, false otherwise.
118+ */
119+ public function deleteByQuery (string $ index , array $ query ): bool
120+ {
121+ $ url = "{$ this ->host }/ {$ index }/_delete_by_query " ;
122+ $ response = Http::post ($ url , $ query );
123+
124+ return $ response ->successful ();
125+ }
91126}
0 commit comments