/** * A convenient method that aggregates the results of all the other methods in the class. Example of output: * * <code> * array( * // the keys are names of fields and/or taxonomies * 'taxonomy' => array( * 'available' => array( * 'taxonomy1' => array( * 'count' => 10, * 'slug' => 'taxonomy1', * 'name' => 'Taxonomy One', * 'font' => 24 * ) * ), * 'selected' => array( * 'taxonomy2' => array( * 'slug' => 'taxonomy2', * 'name' => 'Taxonomy Two' * ) * ), * 'total' => 10 * ), * 'rating' => array( * 'available' => array( * '10-20' => array( * 'count' => 4, * 'slug' => '10-20', * 'to' => 20, * 'from' => 10 * ) * ), * 'total' => 4 * ) * ) * </code> * * @param string $minFont The minimum font size to use for display in a tag cloud (defaults to : 12) * @param string $maxFont The maximum font size to use for display in a tag cloud (defaults to : 24) * * @return array An associative array where the keys represent the data point with a list of selected and/or available options. **/ static function all($minFont = 12, $maxFont = 24) { $options = array(); foreach (Config::taxonomies() as $tax) { $options[$tax] = self::taxonomy($tax); } $numeric = Config::option('numeric'); $fields = array_merge(Config::fields(), Config::meta_fields()); foreach ($fields as $field) { if (isset($numeric[$field])) { $options[$field] = self::range($field); } if ($field == 'post_type') { $options['post_type'] = self::types(Config::types()); } } foreach (Config::customFacets() as $field) { $options[$field] = self::custom($field); } foreach ($options as $name => &$field) { if (isset($field['available'])) { foreach ($field['available'] as &$available) { $available['font'] = self::cloud($field['available'], $available, $minFont, $maxFont); } } } return $options; }
/** * @internal **/ public static function _buildQuery($search, $facets = array()) { global $blog_id; $search = str_ireplace(array(' and ', ' or '), array(' AND ', ' OR '), $search); $fields = array(); $musts = array(); $filters = array(); $scored = array(); foreach (Config::taxonomies() as $tax) { if ($search) { $score = Config::score('tax', $tax); if ($score > 0) { $scored[] = "{$tax}_name^{$score}"; } } self::_filterBySelectedFacets($tax, $facets, 'term', $musts, $filters); } $args = array(); $numeric = Config::option('numeric'); $exclude = Config::apply_filters('searcher_query_exclude_fields', array('post_date')); $fields = Config::fields(); self::_searchField($fields, 'field', $exclude, $search, $facets, $musts, $filters, $scored, $numeric); self::_searchField(Config::meta_fields(), 'meta', $exclude, $search, $facets, $musts, $filters, $scored, $numeric); if (count($scored) > 0 && $search) { $qs = array('fields' => $scored, 'query' => $search); $fuzzy = Config::option('fuzzy'); if ($fuzzy && strpos($search, "~") > -1) { $qs['fuzzy_min_sim'] = $fuzzy; } $qs = Config::apply_filters('searcher_query_string', $qs); $musts[] = array('query_string' => $qs); } if (in_array('post_type', $fields)) { self::_filterBySelectedFacets('post_type', $facets, 'term', $musts, $filters); } self::_searchField(Config::customFacets(), 'custom', $exclude, $search, $facets, $musts, $filters, $scored, $numeric); if (count($filters) > 0) { $args['filter']['bool'] = self::_filtersToBoolean($filters); } if (count($musts) > 0) { $args['query']['bool']['must'] = $musts; } $blogfilter = array('term' => array('blog_id' => $blog_id)); $args['filter']['bool']['must'][] = $blogfilter; $args = Config::apply_filters('searcher_query_pre_facet_filter', $args); if (in_array('post_type', $fields)) { $args['aggs']['post_type']['terms'] = array('field' => 'post_type', 'size' => Config::apply_filters('searcher_query_facet_size', 100)); } // return facets foreach (Config::facets() as $facet) { $args['aggs'][$facet] = array('aggs' => array("facet" => array('terms' => array('field' => $facet, 'size' => Config::apply_filters('searcher_query_facet_size', 100))))); if (count($filters) > 0) { $applicable = array(); foreach ($filters as $filter) { foreach ($filter as $type) { $terms = array_keys($type); if (!in_array($facet, $terms)) { // do not filter on itself when using OR $applicable[] = $filter; } } } if (count($applicable) > 0) { $args['aggs'][$facet]['filter']['bool'] = self::_filtersToBoolean($applicable); } } } if (is_array($numeric)) { foreach (array_keys($numeric) as $facet) { $ranges = Config::ranges($facet); if (count($ranges) > 0) { $args['aggs'][$facet]['aggs'] = array("range" => array('range' => array('field' => $facet, 'ranges' => array()))); foreach ($ranges as $key => $range) { $params = array(); if (isset($range['to'])) { $params['to'] = $range['to']; } if (isset($range['from'])) { $params['from'] = $range['from']; } $args['aggs'][$facet]['aggs']['range']['range']['ranges'][] = $params; } } } } if (isset($args['aggs'])) { foreach ($args['aggs'] as $facet => &$config) { if (!isset($config['filter'])) { $config['filter'] = array('bool' => array('must' => array())); } $config['filter']['bool']['must'][] = $blogfilter; } } return Config::apply_filters('searcher_query_post_facet_filter', $args); }