title: Get all data from index elasticsearch in php
body: <p>I am doing a test with elasticsearch to implement it in my project.</p>
  
  <p>I use CodeIgniter for my website. I found this link:<a href="https://github.com/confact/elasticsearch-codeigniter-library" rel="nofollow noreferrer">confact/elasticsearch-codeigniter-library</a></p>
  
  <p>I use this library in my project. Which works perfectly. Now my question is how can I return all my data from the index?</p>
  
  <p>In principle, when I use Postman I type this link for my search: <a href="http://localhost:9200/my_index/_search?q=test" rel="nofollow noreferrer">http://localhost:9200/my_index/_search?q=test</a> and I find my data well.</p>
  
  <p>In my code, this is what we have:</p>
  
  <p>my library elasticsearch.php :</p>
  
  <pre><code>    &lt;?php
  /**
   * Elasticsearch Library
   *
   * @package OpenLibs
   * 
   */
  class ElasticSearch
  {
      public $index;
  
      /**
       * constructor setting the config variables for server ip and index.
       */
  
      public function __construct()
      {
          $ci = &amp;get_instance();
          $ci -&gt; config -&gt; load("elasticsearch");
          $this -&gt; server = $ci -&gt; config -&gt; item('es_server');
          $this -&gt; index = $ci -&gt; config -&gt; item('index');
      }
      /**
       * Handling the call for every function with curl
       * 
       * @param type $path
       * @param type $method
       * @param type $data
       * 
       * @return type
       * @throws Exception
       */
  
      private function call($path, $method = 'GET', $data = null)
      {
          if (!$this -&gt; index) {
              throw new Exception('$this-&gt;index needs a value');
          }
  
          $url = $this -&gt; server . '/' . $this -&gt; index . '/' . $path;
  
          $headers = array('Accept: application/json', 'Content-Type: application/json', );
  
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  
          switch($method) {
              case 'GET' :
                  break;
              case 'POST' :
                  curl_setopt($ch, CURLOPT_POST, true);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                  break;
              case 'PUT' :
                  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                  break;
              case 'DELETE' :
                  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                  break;
          }
  
          $response = curl_exec($ch);
          $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  
          return json_decode($response, true);
      }
  
  
      /**
       * create a index with mapping or not
       * 
       * @param json $map
       */
  
      public function create($map = false)
      {
          if (!$map) {
              $this -&gt; call(null, 'PUT');
          } else {
              $this -&gt; call(null, 'PUT', $map);
          }
      }
  
      /**
       * get status
       * 
       * @return array
       */
  
      public function status()
      {
          return $this -&gt; call('_status');
      }
  
      /**
       * count how many indexes it exists
       * 
       * @param string $type
       * 
       * @return array
       */
  
      public function count($type)
      {
          return $this -&gt; call($type . '/_count?' . http_build_query(array(null =&gt; '{matchAll:{}}')));
      }
  
      /**
       * set the mapping for the index
       * 
       * @param string $type
       * @param json   $data
       * 
       * @return array
       */
  
      public function map($type, $data)
      {
          return $this -&gt; call($type . '/_mapping', 'PUT', $data);
      }
  
      /**
       * set the mapping for the index
       * 
       * @param type $type
       * @param type $id
       * @param type $data
       * 
       * @return type
       */
  
      public function add($type, $id, $data)
      {
          return $this -&gt; call($type . '/' . $id, 'PUT', $data);
      }
  
      /**
       * delete a index
       * 
       * @param type $type 
       * @param type $id 
       * 
       * @return type 
       */
  
      public function delete($type, $id)
      {
          return $this -&gt; call($type . '/' . $id, 'DELETE');
      }
  
      /**
       * make a simple search query
       * 
       * @param type $type
       * @param type $q
       * 
       * @return type
       */
  
      public function query($type, $q)
      {
          return $this -&gt; call($type . '/_search?' . http_build_query(array('q' =&gt; $q)));
      }
  
      /**
       * make a advanced search query with json data to send
       * 
       * @param type $type
       * @param type $query
       * 
       * @return type
       */
  
      public function advancedquery($type, $query)
      {
          return $this -&gt; call($type . '/_search', 'POST', $query);
      }
  
      /**
       * make a search query with result sized set
       * 
       * @param string  $type  what kind of type of index you want to search
       * @param string  $query the query as a string
       * @param integer $size  The size of the results
       * 
       * @return array
       */
  
      public function query_wresultSize($type, $query, $size = 999)
      {
          return $this -&gt; call($type . '/_search?' . http_build_query(array('q' =&gt; $q, 'size' =&gt; $size)));
      }
  
      /**
       * get one index via the id
       * 
       * @param string  $type The index type
       * @param integer $id   the indentifier for a index
       * 
       * @return type
       */
  
      public function get($type, $id)
      {
          return $this -&gt; call($type . '/' . $id, 'GET');
      }
  
      /**
       * Query the whole server
       * 
       * @param type $query
       * 
       * @return type
       */
  
      public function query_all($query)
      {
          return $this -&gt; call('_search?' . http_build_query(array('q' =&gt; $query)));
      }
  
      /**
       * get similar indexes for one index specified by id - send data to add filters or more
       * 
       * @param string  $type
       * @param integer $id
       * @param string  $fields
       * @param string  $data 
       * 
       * @return array 
       */
  
      public function morelikethis($type, $id, $fields = false, $data = false)
      {
          if ($data != false &amp;&amp; !$fields) {
              return $this -&gt; call($type . '/' . $id . '/_mlt', 'GET', $data);
          } else if ($data != false &amp;&amp; $fields != false) {
              return $this -&gt; call($type . '/' . $id . '/_mlt?' . $fields, 'POST', $data);
          } else if (!$fields) {
              return $this -&gt; call($type . '/' . $id . '/_mlt');
          } else {
              return $this -&gt; call($type . '/' . $id . '/_mlt?' . $fields);
          }
      }
  
      /**
       * make a search query with result sized set
       * 
       * @param type $query
       * @param type $size
       * 
       * @return type
       */
      public function query_all_wresultSize($query, $size = 999)
      {
          return $this -&gt; call('_search?' . http_build_query(array('q' =&gt; $query, 'size' =&gt; $size)));
      }
  
      /**
       * make a suggest query based on similar looking terms
       * 
       * @param type $query
       * 
       * @return array
       */
      public function suggest($query)
      {
          return $this -&gt; call('_suggest', 'POST', $query);
      }
  
  }
  </code></pre>
  
  <p>And in my controller :</p>
  
  <pre><code>public function search() 
  {
      $q= '';
      $this-&gt;elasticsearch-&gt;index = 'my_index';
      $data = $this-&gt;elasticsearch-&gt;query_all($q);
      var_dump($data);
  }
  </code></pre>
  
  <p>Unfortunately, I can not find my data in this case. Am I cheating somewhere?</p>
  
  <p>If anyone could help me, it would help me a lot.</p>
  
  <p>Thank you :*</p>