function get_elastic_crp_posts($args = array()) { global $post; // Are we matching only the title or the post content as well? $match_fields = array('post_title'); $match_fields_content = array($post->post_title); if ($args['match_content']) { $match_fields[] = 'post_content'; $match_fields_content[] = crp_excerpt($post->ID, $args['match_content_words'], false); } // Limit the related posts by time $current_time = current_time('timestamp', 0); $from_date = $current_time - $args['daily_range'] * DAY_IN_SECONDS; $from_date = gmdate('Y-m-d H:i:s', $from_date); $limit = $args['strict_limit'] ? $args['limit'] : $args['limit'] * 3; $client = \Grupoadslzone\Singletons\ElasticSearchClient::i(); /*$clientBuilder = Elasticsearch\ClientBuilder::create(); $clientBuilder->setHosts(array('http://127.0.0.1:9200')); $client = $clientBuilder->build();*/ require_once plugin_dir_path(__FILE__) . 'includes/SearchQuery.php'; $search_query = new SearchQuery(); $search_query->setParamsByWPPost($post); $params = ['index' => ep_get_index_name(), 'type' => 'post', 'body' => ['_source' => 'post_id', 'query' => ['more_like_this' => ["fields" => $search_query->getFields(), "like_text" => $search_query->getSearchQuery(), "min_term_freq" => $search_query->getMinTermFreq(), "max_query_terms" => $search_query->getMaxQueryTerms(), "min_word_length" => 2, "minimum_should_match" => $search_query->getMinimumShouldMatch(), "boost" => 9, "boost_terms" => 4]], 'filter' => ["bool" => ["should" => ['range' => ['post_date' => ['from' => $from_date]]], 'must_not' => ['term' => ["post.post_id" => $post->ID]]]], 'from' => 0, 'size' => $limit]]; //$ret = json_encode($params['body']); $response = $client->search($params); $results = $response['hits']['hits']; return $results; }