/**
  * Checks if index exists by index name, returns true or false
  *
  * @param null $index_name
  *
  * @return bool
  */
 public function index_exists($index_name = null)
 {
     $index_url = ep_get_index_url($index_name);
     $request_args = array('headers' => $this->format_request_headers());
     $request = wp_remote_head($index_url, apply_filters('ep_index_exists_request_args', $request_args, $index_name));
     // 200 means the index exists
     // 404 means the index was non-existent
     if (!is_wp_error($request) && (200 === wp_remote_retrieve_response_code($request) || 404 === wp_remote_retrieve_response_code($request))) {
         if (404 === wp_remote_retrieve_response_code($request)) {
             return false;
         }
         if (200 === wp_remote_retrieve_response_code($request)) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * Search for posts under a specific site index or the global index ($site_id = 0).
  *
  * @param intval $post_id
  * @since 1.1.1
  * @return array
  */
 public function more_like_this($post_id, $scope = 'current')
 {
     $index = $this->_get_index($scope);
     $index_url = ep_get_index_url($index);
     $url = $index_url . "/post/{$post_id}/_mlt";
     $request = wp_remote_request($url, array('method' => 'GET'));
     if (!is_wp_error($request)) {
         // Allow for direct response retrieval
         do_action('ep_retrieve_raw_response', $request, array(), $scope);
         $response_body = wp_remote_retrieve_body($request);
         $response = json_decode($response_body, true);
         if ($this->is_empty_search($response)) {
             return array('found_posts' => 0, 'posts' => array());
         }
         $hits = $response['hits']['hits'];
         // Check for and store aggregations
         if (!empty($response['aggregations'])) {
             do_action('ep_retrieve_aggregations', $response['aggregations'], $args, $scope);
         }
         $posts = array();
         foreach ($hits as $hit) {
             $post = $hit['_source'];
             $post['site_id'] = $this->parse_site_id($hit['_index']);
             $posts[] = apply_filters('ep_retrieve_the_post', $post, $hit);
         }
         return array('found_posts' => $response['hits']['total'], 'posts' => $posts);
     }
     return array('found_posts' => 0, 'posts' => array());
 }