} if ($_G['adminid'] != '1' && $_G['setting']['search']['forum']['maxspm']) { if (DB::result_first("SELECT COUNT(*) FROM " . DB::table('common_searchindex') . " WHERE srchmod='{$srchmod}' AND dateline>'{$_G['timestamp']}'-60") >= $_G['setting']['search']['forum']['maxspm']) { showmessage('search_toomany', 'search.php?mod=forum', array('maxspm' => $_G['setting']['search']['forum']['maxspm'])); } } if ($srchtype == 'fulltext' && $_G['setting']['sphinxon']) { require_once libfile('class/sphinx'); $s = new SphinxClient(); $s->setServer($_G['setting']['sphinxhost'], intval($_G['setting']['sphinxport'])); $s->setMaxQueryTime(intval($_G['setting']['sphinxmaxquerytime'])); $s->SetRankingMode($_G['setting']['sphinxrank']); $s->setLimits(0, intval($_G['setting']['sphinxlimit']), intval($_G['setting']['sphinxlimit'])); $s->setGroupBy('tid', SPH_GROUPBY_ATTR); if ($srchfilter == 'digest') { $s->setFilterRange('digest', 1, 3, false); } if ($srchfilter == 'top') { $s->setFilterRange('displayorder', 1, 2, false); } else { $s->setFilterRange('displayorder', 0, 2, false); } if (!empty($srchfrom) && empty($srchtxt) && empty($srchuid) && empty($srchuname)) { $expiration = TIMESTAMP + $cachelife_time; $keywords = ''; if ($before) { $spx_timemix = 0; $spx_timemax = TIMESTAMP - $srchfrom; } else { $spx_timemix = TIMESTAMP - $srchfrom; $spx_timemax = TIMESTAMP;
/** * Does an index search via Sphinx and returns the results * * @param string $type Search type. Valid types are: author, title, series, subject, keyword (default) * @param string $term Search term/phrase * @param int $limit Number of results to return * @param int $offset Where to begin result set -- for pagination purposes * @param array $sort_array Numerically keyed array of sort parameters. Valid options are: newest, oldest * @param array $location_array Numerically keyed array of location params. NOT IMPLEMENTED YET * @param array $facet_args String-keyed array of facet parameters. See code below for array structure * @return array String-keyed result set */ public function search($type, $term, $limit, $offset, $sort_opt = NULL, $format_array = array(), $location_array = array(), $facet_args = array(), $override_search_filter = FALSE, $limit_available = FALSE, $show_inactive = FALSE) { if (is_callable(array(__CLASS__ . '_hook', __FUNCTION__))) { eval('$hook = new ' . __CLASS__ . '_hook;'); return $hook->{__FUNCTION__}($type, $term, $limit, $offset, $sort_opt, $format_array, $location_array, $facet_args, $override_search_filter, $limit_available); } require_once $this->locum_config['sphinx_config']['api_path'] . '/sphinxapi.php'; $db =& MDB2::connect($this->dsn); $term_arr = explode('?', trim(preg_replace('/\\//', ' ', $term))); $term = trim($term_arr[0]); if ($term == '*' || $term == '**') { $term = ''; } else { $term_prestrip = $term; //$term = preg_replace('/[^A-Za-z0-9*\- ]/iD', '', $term); $term = preg_replace('/\\*\\*/', '*', $term); //fix for how iii used to do wildcards } $final_result_set['term'] = $term; $final_result_set['type'] = trim($type); $cl = new SphinxClient(); $cl->SetServer($this->locum_config['sphinx_config']['server_addr'], (int) $this->locum_config['sphinx_config']['server_port']); // Defaults to 'keyword', non-boolean $bool = FALSE; $cl->SetMatchMode(SPH_MATCH_ALL); if (!$term) { // Searches for everything (usually for browsing purposes--Hot/New Items, etc..) $cl->SetMatchMode(SPH_MATCH_EXTENDED2); } else { $picturebook = array('picturebook', 'picture book'); $picbk_search = '(@callnum ^E)'; $term = str_ireplace($picturebook, $picbk_search, $term); if ($type == 'keyword') { // Custom fiction and non-fiction search $nonfic_search = ' (@callnum "0*" | @callnum "1*" | @callnum "2*" | @callnum "3*" | @callnum "4*" | @callnum "5*" | @callnum "6*" | @callnum "7*" | @callnum "8*" | @callnum "9*")'; $fiction_search = ' (@title fiction | @subjects fiction | @callnum mystery | @callnum fantasy | @callnum fiction | @callnum western | @callnum romance)'; if (stripos($term, 'nonfiction') !== FALSE) { $term = '@@relaxed ' . str_ireplace('nonfiction', '', $term) . $nonfic_search; } else { if (strpos($term, 'non-fiction') !== FALSE) { $term = '@@relaxed ' . str_ireplace('non-fiction', '', $term) . $nonfic_search; } else { if (strpos($term, 'fiction') !== FALSE) { $term = '@@relaxed ' . str_ireplace('fiction', '', $term) . $fiction_search; } } } } // Is it a boolean search? if (preg_match("/ \\| /i", $term) || preg_match("/ \\-/i", $term) || preg_match("/ \\!/i", $term)) { $cl->SetMatchMode(SPH_MATCH_BOOLEAN); $bool = TRUE; } if (preg_match("/ OR /i", $term)) { $cl->SetMatchMode(SPH_MATCH_BOOLEAN); $term = preg_replace('/ OR /i', ' | ', $term); $bool = TRUE; } // Is it a phrase search? if (preg_match("/\"/i", $term) || preg_match("/\\@/i", $term)) { $cl->SetMatchMode(SPH_MATCH_EXTENDED2); $bool = TRUE; } } // Set up for the various search types switch ($type) { case 'author': $cl->SetFieldWeights(array('author' => 50, 'addl_author' => 30)); $idx = 'bib_items_author'; break; case 'title': $cl->SetFieldWeights(array('title' => 50, 'title_medium' => 50, 'series' => 30)); $idx = 'bib_items_title'; break; case 'series': $cl->SetFieldWeights(array('title' => 5, 'series' => 80)); $idx = 'bib_items_title'; break; case 'subject': $idx = 'bib_items_subject'; break; case 'callnum': $cl->SetFieldWeights(array('callnum' => 100)); $idx = 'bib_items_callnum'; //$cl->SetMatchMode(SPH_MATCH_ANY); break; case 'tags': $cl->SetFieldWeights(array('tag_idx' => 100)); $idx = 'bib_items_tags'; //$cl->SetMatchMode(SPH_MATCH_PHRASE); break; case 'reviews': $cl->SetFieldWeights(array('review_idx' => 100)); $idx = 'bib_items_reviews'; break; case 'keyword': default: $cl->SetFieldWeights(array('title' => 400, 'title_medium' => 30, 'author' => 70, 'addl_author' => 40, 'tag_idx' => 25, 'series' => 25, 'review_idx' => 10, 'notes' => 10, 'subjects' => 5)); $idx = 'bib_items_keyword'; break; } // Filter out the records we don't want shown, per locum.ini if (!$override_search_filter) { if (trim($this->locum_config['location_limits']['no_search'])) { $cfg_filter_arr = $this->csv_parser($this->locum_config['location_limits']['no_search']); foreach ($cfg_filter_arr as $cfg_filter) { $cfg_filter_vals[] = $this->string_poly($cfg_filter); } $cl->SetFilter('loc_code', $cfg_filter_vals, TRUE); } } // Valid sort types are 'newest' and 'oldest'. Default is relevance. switch ($sort_opt) { case 'newest': $cl->SetSortMode(SPH_SORT_EXTENDED, 'pub_year DESC, @relevance DESC'); break; case 'oldest': $cl->SetSortMode(SPH_SORT_EXTENDED, 'pub_year ASC, @relevance DESC'); break; case 'catalog_newest': $cl->SetSortMode(SPH_SORT_EXTENDED, 'bib_created DESC, @relevance DESC'); break; case 'catalog_oldest': $cl->SetSortMode(SPH_SORT_EXTENDED, 'bib_created ASC, @relevance DESC'); break; case 'title': $cl->SetSortMode(SPH_SORT_ATTR_ASC, 'title_ord'); break; case 'author': $cl->SetSortMode(SPH_SORT_EXTENDED, 'author_null ASC, author_ord ASC'); break; case 'top_rated': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'rating_idx'); break; case 'popular_week': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'hold_count_week'); break; case 'popular_month': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'hold_count_month'); break; case 'popular_year': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'hold_count_year'); break; case 'popular_total': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'hold_count_total'); break; case 'atoz': $cl->SetSortMode(SPH_SORT_ATTR_ASC, 'title_ord'); break; case 'ztoa': $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'title_ord'); break; default: $cl->SetSortMode(SPH_SORT_EXPR, "@weight + (hold_count_total)*0.02"); break; } // Filter by material types if (is_array($format_array)) { foreach ($format_array as $format) { if (strtolower($format) != 'all') { $filter_arr_mat[] = $this->string_poly(trim($format)); } } if (count($filter_arr_mat)) { $cl->SetFilter('mat_code', $filter_arr_mat); } } // Filter by location if (count($location_array)) { foreach ($location_array as $location) { if (strtolower($location) != 'all') { $filter_arr_loc[] = $this->string_poly(trim($location)); } } if (count($filter_arr_loc)) { $cl->SetFilter('loc_code', $filter_arr_loc); } } // Filter by pub_year if ($facet_args['facet_year']) { if (strpos($facet_args['facet_year'][0], '-') !== FALSE) { $min_year = 1; $max_year = 9999; $args = explode('-', $facet_args['facet_year'][0]); $min_arg = (int) $args[0]; $max_arg = (int) $args[1]; if ($min_arg && $min_arg > $min_year) { $min_year = $min_arg; } if ($max_arg && $max_arg < $max_year) { $max_year = $max_arg; } $cl->setFilterRange('pub_year', $min_year, $max_year); } else { $cl->SetFilter('pub_year', $facet_args['facet_year']); } } // Filter by pub_decade if ($facet_args['facet_decade']) { $cl->SetFilter('pub_decade', $facet_args['facet_decade']); } // Filter by lexile if ($facet_args['facet_lexile']) { $cl->SetFilter('lexile', $facet_args['facet_lexile']); } // Filter by Series if (count($facet_args['facet_series'])) { foreach ($facet_args['facet_series'] as &$facet_series) { $facet_series = $this->string_poly($facet_series); } $cl->SetFilter('series_attr', $facet_args['facet_series']); } // Filter by Language if (count($facet_args['facet_lang'])) { foreach ($facet_args['facet_lang'] as &$facet_lang) { $facet_lang = $this->string_poly($facet_lang); } $cl->SetFilter('lang', $facet_args['facet_lang']); } // Filter inactive records if (!$show_inactive) { $cl->SetFilter('active', array('0'), TRUE); } // Filter by age if (count($facet_args['age'])) { foreach ($facet_args['age'] as $age_facet) { $cl->SetFilter('ages', array($this->string_poly($age_facet))); } } // Filter by availability if ($limit_available) { $cl->SetFilter('branches', array($this->string_poly($limit_available))); } $cl->SetRankingMode(SPH_RANK_SPH04); $proximity_check = $cl->Query($term, $idx); // Quick check on number of results // If original match didn't return any results, try a proximity search if (empty($proximity_check['matches']) && $bool == FALSE && $term != "*" && $type != "tags") { $term = '"' . $term . '"/1'; $cl->SetMatchMode(SPH_MATCH_EXTENDED); $forcedchange = 'yes'; } // Paging/browsing through the result set. $sort_limit = 2000; if ($offset + $limit > $sort_limit) { $sort_limit = $offset + $limit; } $cl->SetLimits((int) $offset, (int) $limit, (int) $sort_limit); // And finally.... we search. $cl->AddQuery($term, $idx); // CREATE FACETS $cl->SetLimits(0, 1000); // Up to 1000 facets $cl->SetArrayResult(TRUE); // Allow duplicate documents in result, for facet grouping $cl->SetGroupBy('pub_year', SPH_GROUPBY_ATTR); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('pub_decade', SPH_GROUPBY_ATTR); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('mat_code', SPH_GROUPBY_ATTR, '@count desc'); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('branches', SPH_GROUPBY_ATTR, '@count desc'); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('ages', SPH_GROUPBY_ATTR, '@count desc'); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('lang', SPH_GROUPBY_ATTR, '@count desc'); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('series_attr', SPH_GROUPBY_ATTR, '@count desc'); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $cl->SetGroupBy('lexile', SPH_GROUPBY_ATTR); $cl->AddQuery($term, $idx); $cl->ResetGroupBy(); $results = $cl->RunQueries(); // Include descriptors $final_result_set['num_hits'] = $results[0]['total_found']; if ($results[0]['total'] <= $this->locum_config['api_config']['suggestion_threshold'] || $forcedchange == 'yes') { if ($this->locum_config['api_config']['use_yahoo_suggest'] == TRUE) { $final_result_set['suggestion'] = $this->yahoo_suggest($term_prestrip); } } // Pull full records out of Couch if ($final_result_set['num_hits']) { $skip_avail = $this->csv_parser($this->locum_config['format_special']['skip_avail']); $bib_hits = array(); foreach ($results[0]['matches'] as $match) { $bib_hits[] = (string) $match['id']; } $final_result_set['results'] = $this->get_bib_items_arr($bib_hits); foreach ($final_result_set['results'] as &$result) { $result = $result['value']; if ($result['bnum']) { // Get availability (Only cached) $result['status'] = $this->get_item_status($result['bnum'], FALSE, TRUE); } } } $final_result_set['facets'] = $this->sphinx_facetizer($results); if ($forcedchange == 'yes') { $final_result_set['changed'] = 'yes'; } return $final_result_set; }
function sphinx_search($keyword) { $fid = 0; $daterange = 0; $orderby = 'match'; $page = 1; $pagesize = 60; global $conf, $time; $cl = new SphinxClient(); $cl->SetServer($conf['sphinx_host'], $conf['sphinx_port']); $cl->SetConnectTimeout(3); $cl->SetArrayResult(TRUE); $cl->SetWeights(array(100, 1, 5)); // 标题权重100,内容权重1,作者权重10 $fid && $cl->SetFilter('fid', array($fid)); $daterange && $cl->setFilterRange('dateline', $time - $daterange * 86400, $time); $cl->SetMatchMode(SPH_MATCH_ALL); if ($orderby == 'match') { $cl->SetSortMode(SPH_SORT_RELEVANCE); // 如果不设置,默认按照权重排序!但是TMD是正序! } elseif ($orderby == 'timeasc') { $cl->SetSortMode(SPH_SORT_ATTR_ASC, 'tid'); } elseif ($orderby == 'timedesc') { $cl->SetSortMode(SPH_SORT_ATTR_DESC, 'tid'); } //$cl->SetSortMode (SPH_SORT_ATTR_DESC, 'tid'); // 如果不设置,默认按照权重排序!但是TMD是正序! /* $cl->SetMatchMode ( SPH_MATCH_EXTENDED ); //设置模式 $cl->SetRankingMode ( SPH_RANK_PROXIMITY ); //设置评分模式 $cl->SetFieldWeights (array('subject'=>100,'message'=>10,'username'=>1));//设置字段的权重,如果area命中,那么权重算2 $cl->SetSortMode ('SPH_SORT_EXPR','@weight'); //按照权重排序 */ // --------------> 优先搜索增量索引 $newlist = array(); $forums = array(); if ($page == 1) { $cl->SetLimits(0, $pagesize, 1000); // 最大结果集 $res = $cl->Query($keyword, $conf['sphinx_deltasrc']); // * 为所有的索引 if (!empty($cl->_error)) { return xn_error(-1, 'Sphinx 错误:' . $cl->_error); } if (!empty($res) && !empty($res['total'])) { $deltamatch = $res['matches']; } $res['matches'] && arrlist_change_key($res['matches'], 'id'); $newlist = array(); $forums = array(); foreach ((array) $res['matches'] as $v) { if (empty($v['attrs'])) { continue; } if (empty($v['attrs']['fid'])) { continue; } $fid = $v['attrs']['fid']; $thread = thread_read($v['attrs']['tid']); if (empty($thread)) { continue; } if (stripos($thread['subject'], $keyword) === FALSE) { continue; } $thread['subject'] = str_replace($keyword, '<span class="red">' . $keyword . '</span>', $thread['subject']); $newlist[] = $thread; } } // --------------> 再搜索主索引 $start = ($page - 1) * $pagesize; $cl->SetLimits($start, $pagesize, 1000); // 最大结果集 $res = $cl->Query($keyword, $conf['sphinx_datasrc']); if (!empty($cl->_error)) { return xn_error(-1, 'Sphinx 错误:' . $cl->_error); } if (empty($res) || empty($res['total'])) { $res['matches'] = $deltamatch; } else { arrlist_change_key($res['matches'], 'id'); } $threadlist = array(); foreach ((array) $res['matches'] as $v) { if (empty($v['attrs'])) { continue; } if (empty($v['attrs']['fid'])) { continue; } $fid = $v['attrs']['fid']; $thread = thread_read($v['attrs']['tid']); if (empty($thread)) { continue; } $thread['subject'] = str_replace($keyword, '<span class="red">' . $keyword . '</span>', $thread['subject']); $threadlist[] = $thread; } $arrlist = $newlist + $threadlist; return $arrlist; }
public function fetch() { if (!class_exists('SphinxClient')) { return false; } $s = new SphinxClient(); $s->setServer($this->_sphinxHost, $this->_sphinxPort); if (count($this->_arrSearchOutRangeColumnMinMax) > 0) { foreach ($this->_arrSearchOutRangeColumnMinMax as $value) { $d = explode(',', $value); $s->setFilterRange($d[0], $d[1], $d[2], true); } } if (count($this->_arrSearchInRangeColumnMinMax) > 0) { foreach ($this->_arrSearchInRangeColumnMinMax as $value) { $d = explode(',', $value); $s->setFilterRange($d[0], $d[1], $d[2], false); } } $s->setConnectTimeout($this->_connectTimeout); $s->setMaxQueryTime($this->{$_maxquerytime}); // $s->setRetries ( int $this->retriesCount , int $this->retriesDelay ); // $s->setMatchMode($this->searchMode); $s->setFieldWeights($this->_fieldweights); // $s->setFilter ( string $attribute , array $values [, bool $exclude = false ] ); // $s->setFilterFloatRange ( string $attribute , float $min , float $max [, bool $exclude = false ] ); // $s->setFilterRange ( string $attribute , int $min , int $max [, bool $exclude = false ] ); // $s->setGeoAnchor ( string $attrlat , string $attrlong , float $latitude , float $longitude ); // $s->setGroupBy ( string $attribute , int $func [, string $groupsort = "@group desc" ] ); // $s->setGroupDistinct ( string $attribute ); // $s->setIDRange ( int $min , int $max ); $s->setIndexWeights($this->_arrIndexweights); // $s->setLimits ( int $offset , int $limit [, int $max_matches = 0 [, int $cutoff = 0 ]] ); $s->setMatchMode($this->searchMode); // $s->setOverride ( string $attribute , int $type , array $values ); $s->setRankingMode($this->rankMode); // $s->setSelect ( string $clause ); // $s->setSortMode ( int $mode [, string $sortby ] ); return $s->query($this->_query); }
/** * Binds the filter to a SphinxClient instance * * @param \SphinxClient $sphinx * * @return FilterRange */ public function bindToSphinx(\SphinxClient $sphinx) { $sphinx->setFilterRange($this->name, $this->min, $this->max, $this->exclude); return $this; }