/** * Extract query from Ajax parameters. * @return WPSOLR_Query */ public static function CreateQuery(WPSOLR_Query $wpsolr_query = null) { $wpsolr_query = isset($wpsolr_query) ? $wpsolr_query : WPSOLR_Query::Create(); if (isset($_POST[WPSOLR_Query_Parameters::SEARCH_PARAMETER_AJAX_URL_PARAMETERS])) { // It is an Ajax call // Parameters are in the url $url_parameters = ltrim($_POST[WPSOLR_Query_Parameters::SEARCH_PARAMETER_AJAX_URL_PARAMETERS], '?'); // Extract url parameters parse_str($url_parameters, $url_parameters); } else { // It is a GET url // Array of parameters $url_parameters = array(); // Extract all url parameters in an array if (isset($_SERVER['QUERY_STRING'])) { parse_str($_SERVER['QUERY_STRING'], $url_parameters); } } // Compatibility: copy old WPSOLR query and standard WP query in current WPSOLR query foreach (array(WPSOLR_Query_Parameters::SEARCH_PARAMETER_SEARCH, WPSOLR_Query_Parameters::SEARCH_PARAMETER_S) as $query_parameter) { if (isset($url_parameters[$query_parameter])) { // Copy old parameter value to new parameter $url_parameters[WPSOLR_Query_Parameters::SEARCH_PARAMETER_Q] = $url_parameters[$query_parameter]; unset($url_parameters[$query_parameter]); } } // Copy url parameters to query self::copy_parameters_to_query($wpsolr_query, $url_parameters); return $wpsolr_query; }
/** * @param WP_Query $wp_query * * @return WPSOLR_Query */ public static function Create() { $wpsolr_query = new WPSOLR_Query(); $wpsolr_query->set_defaults(); return $wpsolr_query; }
/** * Convert a $wpsolr_query in a Solarium select query * * @param WPSOLR_Query $wpsolr_query * * @return Query */ public function set_solarium_query(WPSOLR_Query $wpsolr_query) { // Create the solarium query $solarium_query = $this->solarium_client->createSelect(); // Set the query keywords. $this->set_keywords($solarium_query, $wpsolr_query->get_wpsolr_query()); // Set default operator $solarium_query->setQueryDefaultOperator('AND'); // Limit nb of results $solarium_query->setStart($wpsolr_query->get_start())->setRows(WPSOLR_Global::getOption()->get_search_max_nb_results_by_page()); /* * Add sort field(s) */ $this->add_sort_field($solarium_query, $wpsolr_query->get_wpsolr_sort()); /* * Add facet fields */ $this->add_filter_query_fields($solarium_query, $wpsolr_query->get_filter_query_fields()); /* * Add highlighting fields */ $this->add_highlighting_fields($solarium_query, array(self::PARAMETER_HIGHLIGHTING_FIELD_NAMES => array(WpSolrSchema::_FIELD_NAME_TITLE, WpSolrSchema::_FIELD_NAME_CONTENT, WpSolrSchema::_FIELD_NAME_COMMENTS), self::PARAMETER_HIGHLIGHTING_FRAGMENT_SIZE => WPSOLR_Global::getOption()->get_search_max_length_highlighting(), self::PARAMETER_HIGHLIGHTING_PREFIX => self::DEFAULT_HIGHLIGHTING_PREFIX, self::PARAMETER_HIGHLIGHTING_POSTFIX => self::DEFAULT_HIGHLIGHTING_POSFIX)); /* * Add facet fields */ $this->add_facet_fields($solarium_query, array(self::PARAMETER_FACET_FIELD_NAMES => WPSOLR_Global::getOption()->get_facets_to_display(), self::PARAMETER_FACET_LIMIT => WPSOLR_Global::getOption()->get_search_max_nb_items_by_facet(), self::PARAMETER_FACET_MIN_COUNT => self::DEFAULT_MIN_COUNT_BY_FACET)); /* * Add fields */ $this->add_fields($solarium_query); // Filter to change the solarium query do_action(WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY, array(WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_QUERY => $solarium_query, WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SEARCH_TERMS => $wpsolr_query->get_wpsolr_query(), WpSolrFilters::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SEARCH_USER => wp_get_current_user())); // Done return $this->solarium_query = $solarium_query; }