@@ -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  $ indexstring  $ idarray  $ bodybool 
56+     public  function  indexDocument (string  $ indexstring  $ idarray  $ bodyvoid 
5557    {
5658        $ url"{$ this host }/ {$ index/_doc/ {$ id ;
5759        $ responseput ($ url$ body
5860
59-         return  $ responsesuccessful ();
61+         if  (!$ responsesuccessful ()) {
62+             throw  new  \RuntimeException (
63+                 "Failed to index document to Elasticsearch [ {$ url]:  "  . $ responsebody ()
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  $ indexstring  $ idbool 
77+     public  function  deleteDocument (string  $ indexstring  $ idvoid 
7078    {
7179        $ url"{$ this host }/ {$ index/_doc/ {$ id ;
7280        $ responsedelete ($ url
7381
74-         return  $ responsesuccessful ();
82+         if  (!$ responsesuccessful ()) {
83+             throw  new  \RuntimeException (
84+                 "Failed to delete document from Elasticsearch [ {$ url]:  "  . $ responsebody ()
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  $ indexarray  $ queryarray 
8599    {
86100        $ url"{$ this host }/ {$ index/_search " ;
87101        $ responsepost ($ url$ query
88102
103+         if  (!$ responsesuccessful ()) {
104+             throw  new  \RuntimeException (
105+                 "Failed to search Elasticsearch [ {$ url]:  "  . $ responsebody ()
106+             );
107+         }
108+ 
89109        return  $ responsejson ('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  $ indexarray  $ querybool 
120+     {
121+         $ url"{$ this host }/ {$ index/_delete_by_query " ;
122+         $ responsepost ($ url$ query
123+ 
124+         return  $ responsesuccessful ();
125+     }
91126}
0 commit comments