예제 #1
0
 /**
  * Search the the backend service for a given term
  * @param string $term The search string to be fed in
  * @return array[WPSearch_Document]
  */
 public function search($term, $page = 0, $per_page = 10)
 {
     $timer = "Function " . __CLASS__ . "::search()";
     WPSearch_Benchmark::start($timer);
     if (!is_array($term)) {
         $term = array('q' => $term);
     }
     $category_id = WPSearch_Utility::arrayGet($term, 'category_id');
     $title_boost = WPSearch_Utility::getOption(WPSearch_Core::KEY_TITLE_BOOST, WPSearch_Core::DEFAULT_TITLE_BOOST);
     $content_boost = WPSearch_Utility::getOption(WPSearch_Core::KEY_CONTENT_BOOST, WPSearch_Core::DEFAULT_CONTENT_BOOST);
     $title_boost = WPSearch_Utility::getScaledBoostValue($title_boost, self::$_boostRangeLow, self::$_boostRangeHigh);
     $content_boost = WPSearch_Utility::getScaledBoostValue($content_boost, self::$_boostRangeLow, self::$_boostRangeHigh);
     $search_types = unserialize(WPSearch_Utility::getOption(WPSearch_Core::KEY_SEARCH_TYPES, FALSE));
     $search_types = $search_types !== FALSE ? $search_types : WPSearch_Core::$_defaultSearchTypes;
     if (!$search_types) {
         $search_types = "NOTYPES";
     } else {
         $search_types = implode(' ', $search_types);
     }
     $title_query = "post_title:({$term['q']})^{$title_boost}";
     $content_query = "post_content:({$term['q']})^{$content_boost}";
     $category_query = $category_id ? "+post_category_ids:({$category_id})" : '';
     $type_query = "+post_type:({$search_types})";
     $comments_query = "";
     $categories_query = "";
     if ($this->_doesIndexCategories()) {
         $categories_query = "post_category_names:({$term['q']})";
     }
     if ($this->_doesIndexComments()) {
         $comments_query = "post_comments:({$term['q']})";
     }
     $index = $this->_getIndex();
     if (!$index) {
         throw new Exception("Can't get index object for search..");
     }
     $index->setResultSetLimit(100);
     $query = "+({$title_query} {$content_query}) {$type_query} {$category_query} {$categories_query} {$comments_query}";
     WPSearch_Log::add('debug', "Searching index for '{$query}'");
     $query = Zend_Search_Lucene_Search_QueryParser::parse($query, 'UTF-8');
     $hits = $index->find($query);
     $start = $page * $per_page;
     $documents = array();
     $stop = min(array($start + $per_page, count($hits)));
     for ($i = $start; $i < $stop; $i++) {
         $doc = $this->_buildResultFromHit($hits[$i]);
         /* We're gonna hold off on this one until the next release */
         # $doc->post_content = $this->_highlightMatches($query, $doc->post_content);
         # $doc->post_title   = $this->_highlightMatches($query, $doc->post_title);
         $documents[] = $doc;
     }
     WPSearch_Benchmark::stop($timer);
     return (object) array('hits' => $documents, 'total' => count($hits));
 }
예제 #2
0
 /**
  * An ajax method that will search the index (you can specify the driver)
  * and return results in JSON format.
  */
 public static function search()
 {
     $driver = WPSearch_Utility::arrayGet($_POST, 'driver');
     $term = WPSearch_Utility::arrayGet($_POST, 'q');
     $page = WPSearch_Utility::arrayGet($_POST, 'p', 0);
     $per_page = WPSearch_Utility::arrayGet($_POST, 'n', 5);
     if (!$term) {
         die(json_encode(array('success' => false)));
     }
     try {
         $search = new WPSearch_Search($driver);
         $start = microtime(true);
         $results = $search->search($term, $page, $per_page);
         $search_time = round(microtime(true) - $start, 3);
         # Return seconds without the leading 0
         if (substr($search_time, 0, 1) == '0') {
             $search_time = substr($search_time, 1);
         }
     } catch (WPSearch_Exception $ex) {
         WPSearch_Log::add('error', "Exception during AJAX search call: " . $ex->__toString());
         die(json_encode(array('success' => false)));
     }
     die(json_encode(array('success' => true, 'search_time' => $search_time, 'result' => $results)));
 }
예제 #3
0
 /**
  * 
  * @global object $wp_query The Wordpress query object (available from the Wordpress env.)
  * @global object $wp The Wordpress super-oobject, also available from the Wordpress env.
  * @param array $posts The array of posts Wordpress found originall. This should be empty
  *   from the LIMIT clause manipulation hook
  * @return array[WPSearch_Document] An array of objects that maintain all of the
  *   properties expected of the Wordpress search results
  */
 public function queryCallback($posts)
 {
     if (!is_search() || is_admin()) {
         return $posts;
     }
     WPSearch_Log::add('debug', "Search callback executed");
     global $wp_query;
     # Think I like doing this? You're off your rocker
     global $wp;
     # Wp super object
     $term = array();
     $term['q'] = $wp->query_vars["s"];
     # The search term
     $term['page'] = self::$_page;
     $term['per_page'] = self::$_perPage;
     $category_id = WPSearch_Utility::arrayGet($_GET, 'c_id', FALSE);
     if ($category_id !== FALSE) {
         $term['category_id'] = $category_id;
     }
     if (!WPSearch_Search::instance()->isReady()) {
         WPSearch_Log::add('debug', "Search is in execution for term '{$term['q']}', but the driver is either not installed or indexing." . " Falling back to Wordpress search..");
         return $posts;
     }
     $count_per_page = self::$_perPage;
     $posts = array();
     # Empty what should be an empty array
     WPSearch_Log::add('debug', "Grabbing term from Wordpress query variabled ({$term['q']})");
     try {
         $result = WPSearch_Search::instance()->search($term, self::$_page, self::$_perPage);
         $wp_query->found_posts = count($result->hits);
         $wp_query->max_num_pages = ceil($result->total / $count_per_page);
         WPSearch_Log::add('debug', "Total number of posts found: {$result->total}");
         WPSearch_Log::add('debug', "Set Wordpress vars - # Retrieved posts for page, {$wp_query->found_posts}; # Pages, {$wp_query->max_num_pages}");
         return $result->hits;
     } catch (Exception $ex) {
         $wp_query->found_posts = 0;
         $wp_query->max_num_pages = 0;
         WPSearch_Log::add('error', "There was an error searching the index!" . $ex->__toString());
         return array();
     }
 }