SetFilter() public method

only match records where $attribute value is in given set
public SetFilter ( $attribute, $values, $exclude = false )
Example #1
0
 function hook_search($search)
 {
     $offset = 0;
     $limit = 500;
     $sphinxClient = new SphinxClient();
     $sphinxpair = explode(":", SPHINX_SERVER, 2);
     $sphinxClient->SetServer($sphinxpair[0], (int) $sphinxpair[1]);
     $sphinxClient->SetConnectTimeout(1);
     $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, 'feed_title' => 20));
     $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
     $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
     $sphinxClient->SetLimits($offset, $limit, 1000);
     $sphinxClient->SetArrayResult(false);
     $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
     $result = $sphinxClient->Query($search, SPHINX_INDEX);
     $ids = array();
     if (is_array($result['matches'])) {
         foreach (array_keys($result['matches']) as $int_id) {
             $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
             array_push($ids, $ref_id);
         }
     }
     $ids = join(",", $ids);
     if ($ids) {
         return array("ref_id IN ({$ids})", array());
     } else {
         return array("ref_id = -1", array());
     }
 }
 protected function applyFilters(array $conditions, $exclude = false)
 {
     $exclude = (bool) $exclude;
     foreach ($conditions as $field => $values) {
         $this->sphinxClient->SetFilter($field, $values, $exclude);
     }
 }
Example #3
0
 /**
  *
  * @param string $query
  * @return array of integers - taskIds
  */
 public static function searchTasks($query)
 {
     $fieldWeights = array('description' => 10, 'note' => 6);
     $indexName = 'plancake_tasks';
     $client = new SphinxClient();
     // $client->SetServer (sfConfig::get('app_sphinx_host'), sfConfig::get('app_sphinx_port'));
     $client->SetFilter("author_id", array(PcUserPeer::getLoggedInUser()->getId()));
     $client->SetConnectTimeout(1);
     $client->SetMatchMode(SPH_MATCH_ANY);
     $client->SetSortMode(SPH_SORT_RELEVANCE);
     $client->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
     $client->SetArrayResult(true);
     $client->SetFieldWeights($fieldWeights);
     $client->setLimits(0, 100);
     $results = $client->query($client->EscapeString($query), $indexName);
     if ($results === false) {
         $error = "Sphinx Error - " . $client->GetLastError();
         sfErrorNotifier::alert($error);
     }
     $ids = array();
     if (isset($results['matches']) && count($results['matches'])) {
         foreach ($results['matches'] as $match) {
             $ids[] = $match['id'];
         }
     }
     return PcTaskPeer::retrieveByPKs($ids);
 }
Example #4
0
 /**
  * 搜索faq
  * @param string $keywords 搜索的字符串
  * @param int $gameId	游戏ID
  * @param int $kindId	分类ID
  */
 public function search($keywords, $gameId = null, $kindId = null)
 {
     $this->_sphinx->SetFilter('lang_id', array(C('LANG_ID')), false);
     //设置语言为简体
     if (is_numeric($gameId)) {
         $this->_sphinx->SetFilter('game_type_id', array($gameId), false);
     }
     if (is_numeric($kindId)) {
         $this->_sphinx->SetFilter('kind_id', array($kindId), false);
     }
     if (is_numeric($this->_faqStatus)) {
         $this->_sphinx->SetFilter('status', array($this->_faqStatus), true);
     }
     $result = $this->_sphinx->Query($keywords);
     $retResult = array('data' => $this->_getResult($result['matches']), 'info' => array('total' => $result['total'], 'total_found' => $result['total_found'], 'time' => $result['time'], 'words' => $result['words']));
     return $retResult;
 }
 /**
  * Adds new integer values set filter to the existing list of filters.
  *
  * @param $attribute An attribute name.
  * @param $values Plain array of integer values.
  * @param bool $exclude If set to TRUE, matching items are excluded from the result set.
  * @return SphinxSearch_Abstract_List
  * @throws Exception on failure
  */
 public function setFilter($attribute, $values, $exclude = false)
 {
     $result = $this->SphinxClient->SetFilter($attribute, $values, $exclude);
     if ($result === false) {
         throw new Exception("Error on setting filter \"" . $attribute . "\":\n" . $this->SphinxClient->GetLastError());
     }
     return $this;
 }
Example #6
0
 /**
  * 设置属性过滤
  * $attribute, $values, $exclude = FALSE
  * 此调用在已有的过滤器列表中添加新的过滤器。$attribute是属性名。$values是整数数组。$exclude是布尔值,它控制是接受匹配的文档(默认模式,即$exclude为false时)还是拒绝它们。
  * 只有当索引中$attribute列的值与$values中的任一值匹配时文档才会被匹配(或者拒绝,如果$exclude值为true)
  */
 function filter($filter)
 {
     if (is_array($filter) && count($filter) > 0) {
         foreach ($filter as $attribute => $v) {
             list($values, $exclude) = explode(',', $v);
             $sphinx_int_array = explode("_", $values);
             $this->sphinx->SetFilter($attribute, $sphinx_int_array, constant($exclude));
         }
     }
     return $this;
 }
Example #7
0
 /**
  * Непосредственно сам поиск
  *
  * @param string $sTerms	Поисковый запрос
  * @param string $sObjType	Тип поиска
  * @param int $iOffset	Сдвиг элементов
  * @param int $iLimit	Количество элементов
  * @param array $aExtraFilters	Список фильтров
  * @return array
  */
 public function FindContent($sTerms, $sObjType, $iOffset, $iLimit, $aExtraFilters)
 {
     /**
      * используем кеширование при поиске
      */
     $sExtraFilters = serialize($aExtraFilters);
     $cacheKey = Config::Get('module.search.entity_prefix') . "searchResult_{$sObjType}_{$sTerms}_{$iOffset}_{$iLimit}_{$sExtraFilters}";
     if (false === ($data = $this->Cache_Get($cacheKey))) {
         /**
          * Параметры поиска
          */
         $this->oSphinx->SetMatchMode(SPH_MATCH_ALL);
         $this->oSphinx->SetLimits($iOffset, $iLimit);
         /**
          * Устанавливаем атрибуты поиска
          */
         $this->oSphinx->ResetFilters();
         if (!is_null($aExtraFilters)) {
             foreach ($aExtraFilters as $sAttribName => $sAttribValue) {
                 $this->oSphinx->SetFilter($sAttribName, is_array($sAttribValue) ? $sAttribValue : array($sAttribValue));
             }
         }
         /**
          * Ищем
          */
         if (!is_array($data = $this->oSphinx->Query($sTerms, Config::Get('module.search.entity_prefix') . $sObjType . 'Index'))) {
             return FALSE;
             // Скорее всего недоступен демон searchd
         }
         /**
          * Если результатов нет, то и в кеш писать не стоит...
          * хотя тут момент спорный
          */
         if ($data['total'] > 0) {
             $this->Cache_Set($data, $cacheKey, array(), 60 * 15);
         }
     }
     return $data;
 }
Example #8
0
 /**
  * Непосредственно сам поиск
  *
  * @param string $sQuery           Поисковый запрос
  * @param string $sObjType         Тип поиска
  * @param int    $iOffset          Сдвиг элементов
  * @param int    $iLimit           Количество элементов
  * @param array  $aExtraFilters    Список фильтров
  *
  * @return array
  */
 public function FindContent($sQuery, $sObjType, $iOffset, $iLimit, $aExtraFilters)
 {
     // * используем кеширование при поиске
     $sExtraFilters = serialize($aExtraFilters);
     $cacheKey = Config::Get('plugin.sphinx.prefix') . "searchResult_{$sObjType}_{$sQuery}_{$iOffset}_{$iLimit}_{$sExtraFilters}";
     if (false === ($data = E::ModuleCache()->Get($cacheKey))) {
         // * Параметры поиска
         $this->oSphinx->SetMatchMode(SPH_MATCH_ALL);
         $this->oSphinx->SetLimits($iOffset, $iLimit, 1000);
         // * Устанавливаем атрибуты поиска
         $this->oSphinx->ResetFilters();
         if (!is_null($aExtraFilters)) {
             foreach ($aExtraFilters as $sAttribName => $sAttribValue) {
                 $this->oSphinx->SetFilter($sAttribName, is_array($sAttribValue) ? $sAttribValue : array($sAttribValue));
             }
         }
         // * Ищем
         $sIndex = Config::Get('plugin.sphinx.prefix') . $sObjType . 'Index';
         $data = $this->oSphinx->Query($sQuery, $sIndex);
         if (!is_array($data)) {
             // Если false, то, скорее всего, ошибка и ее пишем в лог
             $sError = $this->GetLastError();
             if ($sError) {
                 $sError .= "\nquery:{$sQuery}\nindex:{$sIndex}";
                 if ($aExtraFilters) {
                     $sError .= "\nfilters:";
                     foreach ($aExtraFilters as $sAttribName => $sAttribValue) {
                         $sError .= $sAttribName . '=(' . (is_array($sAttribValue) ? join(',', $sAttribValue) : $sAttribValue) . ')';
                     }
                 }
                 $this->LogError($sError);
             }
             return false;
         }
         /**
          * Если результатов нет, то и в кеш писать не стоит...
          * хотя тут момент спорный
          */
         if ($data['total'] > 0) {
             E::ModuleCache()->Set($data, $cacheKey, array(), 60 * 15);
         }
     }
     return $data;
 }
 public function index()
 {
     C('TOKEN_ON', false);
     $seo = seo();
     $this->assign("seo", $seo);
     if (isset($_GET['q'])) {
         G('search');
         //关键字
         $q = Input::forSearch(safe_replace($this->_get("q")));
         $q = htmlspecialchars(strip_tags($q));
         //时间范围
         $time = $this->_get("time");
         //模型
         $mid = (int) $this->_get("modelid");
         //栏目
         $catid = (int) $this->_get("catid");
         //排序
         $order = array("adddate" => "DESC", "searchid" => "DESC");
         //搜索历史记录
         $shistory = cookie("shistory");
         if (!$shistory) {
             $shistory = array();
         }
         $model = F("Model");
         $category = F("Category");
         if (trim($_GET['q']) == '') {
             header('Location: ' . U("Search/Index/index"));
             exit;
         }
         array_unshift($shistory, $q);
         $shistory = array_slice(array_unique($shistory), 0, 10);
         //加入搜索历史
         cookie("shistory", $shistory);
         $where = array();
         //每页显示条数
         $pagesize = $this->config['pagesize'] ? $this->config['pagesize'] : 10;
         //缓存时间
         $cachetime = (int) $this->config['cachetime'];
         //按时间搜索
         if ($time == 'day') {
             //一天
             $search_time = time() - 86400;
             $where['adddate'] = array("GT", $search_time);
         } elseif ($time == 'week') {
             //一周
             $search_time = time() - 604800;
             $where['adddate'] = array("GT", $search_time);
         } elseif ($time == 'month') {
             //一月
             $search_time = time() - 2592000;
             $where['adddate'] = array("GT", $search_time);
         } elseif ($time == 'year') {
             //一年
             $search_time = time() - 31536000;
             $where['adddate'] = array("GT", $search_time);
         } else {
             $search_time = 0;
         }
         //可用数据源
         $this->config['modelid'] = $this->config['modelid'] ? $this->config['modelid'] : array();
         //按模型搜索
         if ($mid && in_array($mid, $this->config['modelid'])) {
             $where['modelid'] = array("EQ", (int) $mid);
         }
         //按栏目搜索
         if ($catid) {
             //不支持多栏目搜索,和父栏目搜索。
             $where['catid'] = array("EQ", (int) $catid);
         }
         //分页模板
         $TP = '共有{recordcount}条信息 {pageindex}/{pagecount} {first}{prev}{liststart}{list}{listend}{next}{last}';
         //如果开启sphinx
         if ($this->config['sphinxenable']) {
             import("Sphinxapi", APP_PATH . C("APP_GROUP_PATH") . '/Search/Class/');
             $sphinxhost = $this->config['sphinxhost'];
             $sphinxport = $this->config['sphinxport'];
             $cl = new SphinxClient();
             //设置searchd的主机名和TCP端口
             $cl->SetServer($sphinxhost, $sphinxport);
             //设置连接超时
             $cl->SetConnectTimeout(1);
             //控制搜索结果集的返回格式
             $cl->SetArrayResult(true);
             //设置全文查询的匹配模式 api http://docs.php.net/manual/zh/sphinxclient.setmatchmode.php
             $cl->SetMatchMode(SPH_MATCH_EXTENDED2);
             //设置排名模式 api http://docs.php.net/manual/zh/sphinxclient.setrankingmode.php
             $cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
             //按一种类似SQL的方式将列组合起来,升序或降序排列。用weight是权重排序
             $cl->SetSortMode(SPH_SORT_EXTENDED, "@weight desc");
             //设置返回结果集偏移量和数目
             $page = (int) $this->_get(C("VAR_PAGE"));
             $page = $page < 1 ? 1 : $page;
             $offset = $pagesize * ($page - 1);
             $cl->SetLimits($offset, $pagesize, $pagesize > 1000 ? $pagesize : 1000);
             if (in_array($time, array("day", "week", "month", "year"))) {
                 //过滤时间
                 $cl->SetFilterRange('adddate', $search_time, time(), false);
             }
             if ($mid && in_array($mid, $this->config['modelid'])) {
                 //过滤模型
                 $cl->SetFilter('modelid', (int) $mid);
             }
             if ($catid) {
                 //过滤栏目
                 $cl->SetFilter('catid', (int) $catid);
             }
             //执行搜索 api http://docs.php.net/manual/zh/sphinxclient.query.php
             $res = $cl->Query($q, "*");
             //信息总数
             $count = $res['total'];
             //如果结果不为空
             if (!empty($res['matches'])) {
                 $result_sphinx = $res['matches'];
             }
             $result = array();
             //数组重新组合
             foreach ($result_sphinx as $k => $v) {
                 $result[$k] = array("searchid" => $v['id'], "adddate" => $v['attrs']['adddate'], "catid" => $v['attrs']['catid'], "id" => $v['attrs']['id'], "modelid" => $v['attrs']['modelid']);
             }
             $words = array();
             //搜索关键字
             foreach ($res['words'] as $k => $v) {
                 $words[] = $k;
             }
             $page = page($count, $pagesize);
             $page->SetPager('default', $TP, array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
             $this->assign("Page", $page->show('default'));
         } else {
             import("Segment", APP_PATH . C("APP_GROUP_PATH") . '/Search/Class/');
             $Segment = new Segment();
             //分词结果
             $segment_q = $Segment->get_keyword($Segment->split_result($q));
             $words = explode(" ", $segment_q);
             if (!empty($segment_q)) {
                 $where['_string'] = " MATCH (`data`) AGAINST ('{$segment_q}' IN BOOLEAN MODE) ";
             } else {
                 //这种搜索最不行
                 $where['data'] = array('like', "%{$q}%");
             }
             //查询结果缓存
             if ($cachetime) {
                 //统计
                 $count = M("Search")->cache(true, $cachetime)->where($where)->count();
                 $page = page($count, $pagesize);
                 $result = M("Search")->cache(true, $cachetime)->where($where)->limit($page->firstRow . ',' . $page->listRows)->order($order)->select();
             } else {
                 $count = M("Search")->where($where)->count();
                 $page = $this->page($count, $pagesize);
                 $result = M("Search")->where($where)->limit($page->firstRow . ',' . $page->listRows)->order($order)->select();
             }
             $page->SetPager('default', $TP, array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
             $this->assign("Page", $page->show('default'));
         }
         //搜索结果处理
         if ($result && is_array($result)) {
             foreach ($result as $k => $r) {
                 $modelid = $r['modelid'];
                 $id = $r['id'];
                 $tablename = ucwords($model[$modelid]['tablename']);
                 if ($tablename) {
                     $result[$k] = M($tablename)->where(array("id" => $id))->find();
                 }
             }
         }
         //搜索记录
         if (strlen($q) < 17 && strlen($q) > 1 && $result) {
             $res = M("SearchKeyword")->where(array('keyword' => $q))->find();
             if ($res) {
                 //关键词搜索数+1
                 M("SearchKeyword")->where(array('keyword' => $q))->setInc("searchnums");
             } else {
                 //关键词转换为拼音
                 load("@.iconvfunc");
                 $pinyin = gbk_to_pinyin(iconv('utf-8', 'gbk//IGNORE', $q));
                 if (is_array($pinyin)) {
                     $pinyin = implode('', $pinyin);
                 }
                 M("SearchKeyword")->add(array('keyword' => $q, 'searchnums' => 1, 'data' => $segment_q, 'pinyin' => $pinyin));
             }
         }
         //相关搜索功能
         if ($this->config['relationenble']) {
             $map = array();
             //相关搜索
             if (!empty($segment_q)) {
                 $relation_q = str_replace(' ', '%', $segment_q);
             } else {
                 $relation_q = $q;
             }
             $map['_string'] = " MATCH (`data`) AGAINST ('%{$relation_q}%' IN BOOLEAN MODE) ";
             $relation = M("SearchKeyword")->where($map)->select();
             $this->assign("relation", $relation);
         }
         foreach ($this->config['modelid'] as $modelid) {
             $source[$modelid] = array("name" => $model[$modelid]['name'], "modelid" => $modelid);
         }
         //搜索结果
         $this->assign("result", $result);
         //运行时间
         $search_time = G('search', 'end', 6);
         $this->assign("count", $count ? $count : 0);
         $this->assign("search_time", $search_time);
         $this->assign("keyword", $q);
         $this->assign("category", $category);
         $this->assign("source", $source);
         $this->assign("time", $time);
         $this->assign("modelid", $mid);
         $this->assign("shistory", $shistory);
         //分词后的搜索关键字
         $this->assign("words", $words);
         $this->display("search");
     } else {
         $this->display();
     }
 }
Example #10
0
function do_query($search_str)
{
    //$tmp_var = array(array('itemName' => "test1"), array('itemName' => "test2"), array('itemName' => "test3"));
    //echo implode(",",tmp_var);
    //echo json_encode($tmp_var);
    //return tmp_var;
    $q = "";
    $sql = "";
    $mode = SPH_MATCH_ALL;
    $host = "localhost";
    $port = 9312;
    $index = "*";
    $groupby = "";
    $groupsort = "@group desc";
    $filter = "group_id";
    $filtervals = array();
    $distinct = "";
    $sortby = "";
    $sortexpr = "";
    $limit = 20;
    $ranker = SPH_RANK_PROXIMITY_BM25;
    $select = "*";
    $cl = new SphinxClient();
    $cl->SetServer($host, $port);
    $cl->SetConnectTimeout(1);
    $cl->SetArrayResult(true);
    $cl->SetWeights(array(100, 1));
    $cl->SetMatchMode($mode);
    if (count($filtervals)) {
        $cl->SetFilter($filter, $filtervals);
    }
    if ($groupby) {
        $cl->SetGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort);
    }
    if ($sortby) {
        $cl->SetSortMode(SPH_SORT_EXTENDED, $sortby);
    }
    if ($sortexpr) {
        $cl->SetSortMode(SPH_SORT_EXPR, $sortexpr);
    }
    if ($distinct) {
        $cl->SetGroupDistinct($distinct);
    }
    if ($select) {
        $cl->SetSelect($select);
    }
    if ($limit) {
        $cl->SetLimits(0, $limit, $limit > 1000 ? $limit : 1000);
    }
    $cl->SetRankingMode($ranker);
    $res = $cl->Query($search_str, $index);
    //return $res;
    if (is_array($res["matches"])) {
        $results = array();
        $n = 1;
        //print "Matches:\n";
        foreach ($res["matches"] as $docinfo) {
            //print "$n. doc_id=$docinfo[id], weight=$docinfo[weight]";
            $attr_array = array();
            $results[$docinfo[id]];
            foreach ($res["attrs"] as $attrname => $attrtype) {
                $value = $docinfo["attrs"][$attrname];
                if ($attrtype == SPH_ATTR_MULTI || $attrtype == SPH_ATTR_MULTI64) {
                    $value = "(" . join(",", $value) . ")";
                } else {
                    if ($attrtype == SPH_ATTR_TIMESTAMP) {
                        $value = date("Y-m-d H:i:s", $value);
                    }
                }
                $attr_array[$attrname] = $value;
                //print $value;
            }
            $results[$docinfo[id]] = $attr_array;
            $n++;
            //print implode("",$results)."\n";
        }
        return $results;
    }
}
Example #11
0
 public function __construct($rowsPerPage, $currentPage, $siteID, $wildCardString, $sortBy, $sortDirection)
 {
     $this->_db = DatabaseConnection::getInstance();
     $this->_siteID = $siteID;
     $this->_sortByFields = array('firstName', 'lastName', 'city', 'state', 'dateModifiedSort', 'dateCreatedSort', 'ownerSort');
     if (ENABLE_SPHINX) {
         /* Sphinx API likes to throw PHP errors *AND* use it's own error
          * handling.
          */
         assert_options(ASSERT_WARNING, 0);
         $sphinx = new SphinxClient();
         $sphinx->SetServer(SPHINX_HOST, SPHINX_PORT);
         $sphinx->SetWeights(array(0, 100, 0, 0, 50));
         $sphinx->SetMatchMode(SPH_MATCH_EXTENDED);
         $sphinx->SetLimits(0, 1000);
         $sphinx->SetSortMode(SPH_SORT_TIME_SEGMENTS, 'date_added');
         // FIXME: This can be sped up a bit by actually grouping ranges of
         //        site IDs into their own index's. Maybe every 500 or so at
         //        least on the Hosted system.
         $sphinx->SetFilter('site_id', array($this->_siteID));
         /* Create the Sphinx query string. */
         $wildCardString = DatabaseSearch::humanToSphinxBoolean($wildCardString);
         /* Execute the Sphinx query. Sphinx can ask us to retry if its
          * maxed out. Retry up to 5 times.
          */
         $tries = 0;
         do {
             /* Wait for one second if this isn't out first attempt. */
             if (++$tries > 1) {
                 sleep(1);
             }
             $results = $sphinx->Query($wildCardString, SPHINX_INDEX);
             $errorMessage = $sphinx->GetLastError();
         } while ($results === false && strpos($errorMessage, 'server maxed out, retry') !== false && $tries <= 5);
         /* Throw a fatal error if Sphinx errors occurred. */
         if ($results === false) {
             $this->fatal('Sphinx Error: ' . ucfirst($errorMessage) . '.');
         }
         /* Throw a fatal error (for now) if Sphinx warnings occurred. */
         $lastWarning = $sphinx->GetLastWarning();
         if (!empty($lastWarning)) {
             // FIXME: Just display a warning, and notify dev team.
             $this->fatal('Sphinx Warning: ' . ucfirst($lastWarning) . '.');
         }
         /* Show warnings for assert()s again. */
         assert_options(ASSERT_WARNING, 1);
         if (empty($results['matches'])) {
             $this->_WHERE = '0';
         } else {
             $attachmentIDs = implode(',', array_keys($results['matches']));
             $this->_WHERE = 'attachment.attachment_id IN(' . $attachmentIDs . ')';
         }
     } else {
         $this->_WHERE = DatabaseSearch::makeBooleanSQLWhere(DatabaseSearch::fulltextEncode($wildCardString), $this->_db, 'attachment.text');
     }
     /* How many companies do we have? */
     $sql = sprintf("SELECT\n                COUNT(*) AS count\n            FROM\n                attachment\n            LEFT JOIN candidate\n                ON attachment.data_item_id = candidate.candidate_id\n                AND attachment.data_item_type = %s\n                AND attachment.site_id = candidate.site_id\n            LEFT JOIN user AS owner_user\n                ON candidate.owner = owner_user.user_id\n            WHERE\n                resume = 1\n            AND\n                %s\n            AND\n                (ISNULL(candidate.is_admin_hidden) OR (candidate.is_admin_hidden = 0))\n            AND\n                (ISNULL(candidate.is_active) OR (candidate.is_active = 1))\n            AND\n                attachment.site_id = %s", DATA_ITEM_CANDIDATE, $this->_WHERE, $this->_siteID);
     $rs = $this->_db->getAssoc($sql);
     /* Pass "Search By Resume"-specific parameters to Pager constructor. */
     parent::__construct($rs['count'], $rowsPerPage, $currentPage);
 }
 /**
  * Отправляет подготовленный запрос на сфинкс, и преобразует ответ в нужный вид.
  *
  * @param string $query      поисковый запрос (в оригинальном виде)
  * @param int    $storeId    ИД текущего магазина
  * @param string $indexCode  Код индекса  по которому нужно провести поиск (mage_catalog_product ...)
  * @param string $primaryKey Primary Key индекса (entity_id, category_id, post_id ...)
  * @param array  $attributes Масив атрибутов с весами
  * @param int    $offset     Страница
  *
  * @return array масив ИД елементов, где ИД - ключ, релевантность значение
  */
 protected function _query($query, $storeId, $index, $offset = 1)
 {
     $uid = Mage::helper('mstcore/debug')->start();
     $indexCode = $index->getCode();
     $primaryKey = $index->getPrimaryKey();
     $attributes = $index->getAttributes();
     $client = new SphinxClient();
     $client->setMaxQueryTime(5000);
     //5 seconds
     $client->setLimits(($offset - 1) * self::PAGE_SIZE, self::PAGE_SIZE, $this->_config->getResultLimit());
     $client->setSortMode(SPH_SORT_RELEVANCE);
     $client->setMatchMode(SPH_MATCH_EXTENDED);
     $client->setServer($this->_spxHost, $this->_spxPort);
     $client->SetFieldWeights($attributes);
     if ($storeId) {
         $client->SetFilter('store_id', $storeId);
     }
     $sphinxQuery = $this->_buildQuery($query, $storeId);
     if (!$sphinxQuery) {
         return array();
     }
     $sphinxQuery = '@(' . implode(',', $index->getSearchableAttributes()) . ')' . $sphinxQuery;
     $sphinxResult = $client->query($sphinxQuery, $indexCode);
     if ($sphinxResult === false) {
         Mage::throwException($client->GetLastError() . "\nQuery: " . $query);
     } elseif ($sphinxResult['total'] > 0) {
         $entityIds = array();
         foreach ($sphinxResult['matches'] as $data) {
             $entityIds[$data['attrs'][strtolower($primaryKey)]] = $data['weight'];
         }
         if ($sphinxResult['total'] > $offset * self::PAGE_SIZE && $offset * self::PAGE_SIZE < $this->_config->getResultLimit()) {
             $newIds = $this->_query($query, $storeId, $index, $offset + 1);
             foreach ($newIds as $key => $value) {
                 $entityIds[$key] = $value;
             }
         }
     } else {
         $entityIds = array();
     }
     $entityIds = $this->_normalize($entityIds);
     Mage::helper('mstcore/debug')->end($uid, $entityIds);
     return $entityIds;
 }
Example #13
0
function sphinx_search($query, $offset = 0, $limit = 30)
{
    require_once 'lib/sphinxapi.php';
    $sphinxClient = new SphinxClient();
    $sphinxClient->SetServer('localhost', 9312);
    $sphinxClient->SetConnectTimeout(1);
    $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, 'feed_title' => 20));
    $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
    $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
    $sphinxClient->SetLimits($offset, $limit, 1000);
    $sphinxClient->SetArrayResult(false);
    $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
    $result = $sphinxClient->Query($query, SPHINX_INDEX);
    $ids = array();
    if (is_array($result['matches'])) {
        foreach (array_keys($result['matches']) as $int_id) {
            $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
            array_push($ids, $ref_id);
        }
    }
    return $ids;
}
Example #14
0
 /**
  * 关键词获取普通产品获取相关分类
  * @param array $splitKeywords
  * @return array
  */
 public function getRelaCateCommon($splitKeywords, $cateid = 0, $limit = 20)
 {
     $sphinxConfig = $this->di["config"]["prosearchd"];
     $sphinx = new \SphinxClient();
     $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
     $sphinx->SetSelect("id, cate3");
     $sphinx->SetFilter("cate3", array(1270), true);
     if ($cateid) {
         $sphinx->SetFilter("cate3", array($cateid), false);
     }
     $sphinx->SetFilterRange("id", 1, 900000000, false);
     $sphinx->SetLimits(0, $limit, $limit);
     $sphinx->SetGroupBy('cate3', SPH_GROUPBY_ATTR, "@count desc");
     $batchResult = $sphinx->query($splitKeywords, "product_distri");
     $error = $sphinx->getLastError();
     if ($error) {
         return array();
     }
     $result = $this->fomatSphinxData($batchResult);
     if (empty($result) || !is_array($result)) {
         $result = array();
     }
     return $result;
 }
Example #15
0
<?php

/**
 * sphinx链接测试
 */
$offset = 0;
$limit = 30;
include_once "sphinxapi.php";
$sphinx = new SphinxClient();
$sphinx->setServer('192.168.2.188', 9319);
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
$sphinx->SetSortMode(SPH_SORT_EXTENDED, "newstime DESC");
$sphinx->SetFilter('checked', array(1));
$sphinx->SetLimits($offset * 30, $limit, 1000);
$res = $sphinx->query("", 'd_hangyenews,m_hangyenews');
echo "<pre>";
var_dump($sphinx);
echo "\n";
var_dump($res);
exit;
Example #16
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetFilter('attr', array(10, 20, 30));
$cl->Query('query');
Example #17
0
 /**
  * Performs a search on keywords depending on display specific params. You have to run split_keywords() first
  *
  * @param	string		$type				contains either posts or topics depending on what should be searched for
  * @param	string		$fields				contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
  * @param	string		$terms				is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
  * @param	array		$sort_by_sql		contains SQL code for the ORDER BY part of a query
  * @param	string		$sort_key			is the key of $sort_by_sql for the selected sorting
  * @param	string		$sort_dir			is either a or d representing ASC and DESC
  * @param	string		$sort_days			specifies the maximum amount of days a post may be old
  * @param	array		$ex_fid_ary			specifies an array of forum ids which should not be searched
  * @param	string		$post_visibility	specifies which types of posts the user can view in which forums
  * @param	int			$topic_id			is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
  * @param	array		$author_ary			an array of author ids if the author should be ignored during the search the array is empty
  * @param	string		$author_name		specifies the author match, when ANONYMOUS is also a search-match
  * @param	array		&$id_ary			passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
  * @param	int			$start				indicates the first index of the page
  * @param	int			$per_page			number of ids each page is supposed to contain
  * @return	boolean|int						total number of results
  */
 public function keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
 {
     // No keywords? No posts.
     if (!strlen($this->search_query) && !sizeof($author_ary)) {
         return false;
     }
     $id_ary = array();
     $join_topic = $type != 'posts';
     // Sorting
     if ($type == 'topics') {
         switch ($sort_key) {
             case 'a':
                 $this->sphinx->SetGroupBy('topic_id', SPH_GROUPBY_ATTR, 'poster_id ' . ($sort_dir == 'a' ? 'ASC' : 'DESC'));
                 break;
             case 'f':
                 $this->sphinx->SetGroupBy('topic_id', SPH_GROUPBY_ATTR, 'forum_id ' . ($sort_dir == 'a' ? 'ASC' : 'DESC'));
                 break;
             case 'i':
             case 's':
                 $this->sphinx->SetGroupBy('topic_id', SPH_GROUPBY_ATTR, 'post_subject ' . ($sort_dir == 'a' ? 'ASC' : 'DESC'));
                 break;
             case 't':
             default:
                 $this->sphinx->SetGroupBy('topic_id', SPH_GROUPBY_ATTR, 'topic_last_post_time ' . ($sort_dir == 'a' ? 'ASC' : 'DESC'));
                 break;
         }
     } else {
         switch ($sort_key) {
             case 'a':
                 $this->sphinx->SetSortMode($sort_dir == 'a' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, 'poster_id');
                 break;
             case 'f':
                 $this->sphinx->SetSortMode($sort_dir == 'a' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, 'forum_id');
                 break;
             case 'i':
             case 's':
                 $this->sphinx->SetSortMode($sort_dir == 'a' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, 'post_subject');
                 break;
             case 't':
             default:
                 $this->sphinx->SetSortMode($sort_dir == 'a' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, 'post_time');
                 break;
         }
     }
     // Most narrow filters first
     if ($topic_id) {
         $this->sphinx->SetFilter('topic_id', array($topic_id));
     }
     $search_query_prefix = '';
     switch ($fields) {
         case 'titleonly':
             // Only search the title
             if ($terms == 'all') {
                 $search_query_prefix = '@title ';
             }
             // Weight for the title
             $this->sphinx->SetFieldWeights(array("title" => 5, "data" => 1));
             // 1 is first_post, 0 is not first post
             $this->sphinx->SetFilter('topic_first_post', array(1));
             break;
         case 'msgonly':
             // Only search the body
             if ($terms == 'all') {
                 $search_query_prefix = '@data ';
             }
             // Weight for the body
             $this->sphinx->SetFieldWeights(array("title" => 1, "data" => 5));
             break;
         case 'firstpost':
             // More relative weight for the title, also search the body
             $this->sphinx->SetFieldWeights(array("title" => 5, "data" => 1));
             // 1 is first_post, 0 is not first post
             $this->sphinx->SetFilter('topic_first_post', array(1));
             break;
         default:
             // More relative weight for the title, also search the body
             $this->sphinx->SetFieldWeights(array("title" => 5, "data" => 1));
             break;
     }
     if (sizeof($author_ary)) {
         $this->sphinx->SetFilter('poster_id', $author_ary);
     }
     // As this is not simply possible at the moment, we limit the result to approved posts.
     // This will make it impossible for moderators to search unapproved and softdeleted posts,
     // but at least it will also cause the same for normal users.
     $this->sphinx->SetFilter('post_visibility', array(ITEM_APPROVED));
     if (sizeof($ex_fid_ary)) {
         // All forums that a user is allowed to access
         $fid_ary = array_unique(array_intersect(array_keys($this->auth->acl_getf('f_read', true)), array_keys($this->auth->acl_getf('f_search', true))));
         // All forums that the user wants to and can search in
         $search_forums = array_diff($fid_ary, $ex_fid_ary);
         if (sizeof($search_forums)) {
             $this->sphinx->SetFilter('forum_id', $search_forums);
         }
     }
     $this->sphinx->SetFilter('deleted', array(0));
     $this->sphinx->SetLimits($start, (int) $per_page, SPHINX_MAX_MATCHES);
     $result = $this->sphinx->Query($search_query_prefix . str_replace('&quot;', '"', $this->search_query), $this->indexes);
     // Could be connection to localhost:9312 failed (errno=111,
     // msg=Connection refused) during rotate, retry if so
     $retries = SPHINX_CONNECT_RETRIES;
     while (!$result && strpos($this->sphinx->GetLastError(), "errno=111,") !== false && $retries--) {
         usleep(SPHINX_CONNECT_WAIT_TIME);
         $result = $this->sphinx->Query($search_query_prefix . str_replace('&quot;', '"', $this->search_query), $this->indexes);
     }
     if ($this->sphinx->GetLastError()) {
         add_log('critical', 'LOG_SPHINX_ERROR', $this->sphinx->GetLastError());
         if ($this->auth->acl_get('a_')) {
             trigger_error($this->user->lang('SPHINX_SEARCH_FAILED', $this->sphinx->GetLastError()));
         } else {
             trigger_error($this->user->lang('SPHINX_SEARCH_FAILED_LOG'));
         }
     }
     $result_count = $result['total_found'];
     if ($result_count && $start >= $result_count) {
         $start = floor(($result_count - 1) / $per_page) * $per_page;
         $this->sphinx->SetLimits((int) $start, (int) $per_page, SPHINX_MAX_MATCHES);
         $result = $this->sphinx->Query($search_query_prefix . str_replace('&quot;', '"', $this->search_query), $this->indexes);
         // Could be connection to localhost:9312 failed (errno=111,
         // msg=Connection refused) during rotate, retry if so
         $retries = SPHINX_CONNECT_RETRIES;
         while (!$result && strpos($this->sphinx->GetLastError(), "errno=111,") !== false && $retries--) {
             usleep(SPHINX_CONNECT_WAIT_TIME);
             $result = $this->sphinx->Query($search_query_prefix . str_replace('&quot;', '"', $this->search_query), $this->indexes);
         }
     }
     $id_ary = array();
     if (isset($result['matches'])) {
         if ($type == 'posts') {
             $id_ary = array_keys($result['matches']);
         } else {
             foreach ($result['matches'] as $key => $value) {
                 $id_ary[] = $value['attrs']['topic_id'];
             }
         }
     } else {
         return false;
     }
     $id_ary = array_slice($id_ary, 0, (int) $per_page);
     return $result_count;
 }
Example #18
0
 /**
  * This has it's own custom search.
  *
  * @param mixed[] $search_params
  * @param mixed[] $search_words
  * @param string[] $excluded_words
  * @param int[] $participants
  * @param string[] $search_results
  */
 public function searchQuery($search_params, $search_words, $excluded_words, &$participants, &$search_results)
 {
     global $user_info, $context, $modSettings;
     // Only request the results if they haven't been cached yet.
     if (($cached_results = cache_get_data('search_results_' . md5($user_info['query_see_board'] . '_' . $context['params']))) === null) {
         // The API communicating with the search daemon.
         require_once SOURCEDIR . '/sphinxapi.php';
         // Create an instance of the sphinx client and set a few options.
         $mySphinx = new SphinxClient();
         $mySphinx->SetServer($modSettings['sphinx_searchd_server'], (int) $modSettings['sphinx_searchd_port']);
         $mySphinx->SetLimits(0, (int) $modSettings['sphinx_max_results'], (int) $modSettings['sphinx_max_results']);
         // Put together a sort string; besides the main column sort (relevance, id_topic, or num_replies),
         $search_params['sort_dir'] = strtoupper($search_params['sort_dir']);
         $sphinx_sort = $search_params['sort'] === 'id_msg' ? 'id_topic' : $search_params['sort'];
         // Add secondary sorting based on relevance value (if not the main sort method) and age
         $sphinx_sort .= ' ' . $search_params['sort_dir'] . ($search_params['sort'] === 'relevance' ? '' : ', relevance DESC') . ', poster_time DESC';
         // Include the engines weight values in the group sort
         $sphinx_sort = str_replace('relevance ', '@weight ' . $search_params['sort_dir'] . ', relevance ', $sphinx_sort);
         // Grouping by topic id makes it return only one result per topic, so don't set that for in-topic searches
         if (empty($search_params['topic'])) {
             $mySphinx->SetGroupBy('id_topic', SPH_GROUPBY_ATTR, $sphinx_sort);
         }
         // Set up the sort expresssion
         $mySphinx->SetSortMode(SPH_SORT_EXPR, '(@weight + (relevance / 1000))');
         // Update the field weights for subject vs body
         $mySphinx->SetFieldWeights(array('subject' => !empty($modSettings['search_weight_subject']) ? $modSettings['search_weight_subject'] * 200 : 1000, 'body' => 1000));
         // Set the limits based on the search parameters.
         if (!empty($search_params['min_msg_id']) || !empty($search_params['max_msg_id'])) {
             $mySphinx->SetIDRange($search_params['min_msg_id'], empty($search_params['max_msg_id']) ? (int) $modSettings['maxMsgID'] : $search_params['max_msg_id']);
         }
         if (!empty($search_params['topic'])) {
             $mySphinx->SetFilter('id_topic', array((int) $search_params['topic']));
         }
         if (!empty($search_params['brd'])) {
             $mySphinx->SetFilter('id_board', $search_params['brd']);
         }
         if (!empty($search_params['memberlist'])) {
             $mySphinx->SetFilter('id_member', $search_params['memberlist']);
         }
         // Construct the (binary mode & |) query while accounting for excluded words
         $orResults = array();
         $inc_words = array();
         foreach ($search_words as $orIndex => $words) {
             $inc_words = array_merge($inc_words, $words['indexed_words']);
             $andResult = '';
             foreach ($words['indexed_words'] as $sphinxWord) {
                 $andResult .= (in_array($sphinxWord, $excluded_words) ? '-' : '') . $this->_cleanWordSphinx($sphinxWord, $mySphinx) . ' & ';
             }
             $orResults[] = substr($andResult, 0, -3);
         }
         // If no search terms are left after comparing against excluded words (i.e. "test -test" or "test last -test -last"),
         // sending that to Sphinx would result in a fatal error
         if (!count(array_diff($inc_words, $excluded_words))) {
             // Instead, fail gracefully (return "no results")
             return 0;
         }
         $query = count($orResults) === 1 ? $orResults[0] : '(' . implode(') | (', $orResults) . ')';
         // Subject only searches need to be specified.
         if ($search_params['subject_only']) {
             $query = '@(subject) ' . $query;
         }
         // Choose an appropriate matching mode
         $mode = SPH_MATCH_ALL;
         // Over two words and searching for any (since we build a binary string, this will never get set)
         if (substr_count($query, ' ') > 1 && (!empty($search_params['searchtype']) && $search_params['searchtype'] == 2)) {
             $mode = SPH_MATCH_ANY;
         }
         // Binary search?
         if (preg_match('~[\\|\\(\\)\\^\\$\\?"\\/=-]~', $query)) {
             $mode = SPH_MATCH_EXTENDED;
         }
         // Set the matching mode
         $mySphinx->SetMatchMode($mode);
         // Execute the search query.
         $request = $mySphinx->Query($query, 'elkarte_index');
         // Can a connection to the daemon be made?
         if ($request === false) {
             // Just log the error.
             if ($mySphinx->GetLastError()) {
                 log_error($mySphinx->GetLastError());
             }
             fatal_lang_error('error_no_search_daemon');
         }
         // Get the relevant information from the search results.
         $cached_results = array('matches' => array(), 'num_results' => $request['total']);
         if (isset($request['matches'])) {
             foreach ($request['matches'] as $msgID => $match) {
                 $cached_results['matches'][$msgID] = array('id' => $match['attrs']['id_topic'], 'relevance' => round($match['attrs']['@count'] + $match['attrs']['relevance'] / 10000, 1) . '%', 'num_matches' => empty($search_params['topic']) ? $match['attrs']['@count'] : 0, 'matches' => array());
             }
         }
         // Store the search results in the cache.
         cache_put_data('search_results_' . md5($user_info['query_see_board'] . '_' . $context['params']), $cached_results, 600);
     }
     $participants = array();
     foreach (array_slice(array_keys($cached_results['matches']), $_REQUEST['start'], $modSettings['search_results_per_page']) as $msgID) {
         $context['topics'][$msgID] = $cached_results['matches'][$msgID];
         $participants[$cached_results['matches'][$msgID]['id']] = false;
     }
     // Sentences need to be broken up in words for proper highlighting.
     $search_results = array();
     foreach ($search_words as $orIndex => $words) {
         $search_results = array_merge($search_results, $search_words[$orIndex]['subject_words']);
     }
     return $cached_results['num_results'];
 }
fclose($file);
$client->SetSortMode(SPH_SORT_RELEVANCE, "");
// boolean
$client->SetMatchMode(SPH_MATCH_BOOLEAN);
$file = fopen("spec/fixtures/data/boolean.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->SetMatchMode(SPH_MATCH_ALL);
// phrase
$client->SetMatchMode(SPH_MATCH_PHRASE);
$file = fopen("spec/fixtures/data/phrase.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("testing this ")]);
fclose($file);
$client->SetMatchMode(SPH_MATCH_ALL);
// filter
$client->SetFilter("id", array(10, 100, 1000));
$file = fopen("spec/fixtures/data/filter.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->ResetFilters();
// group
$client->SetGroupBy("id", SPH_GROUPBY_ATTR, "id");
$file = fopen("spec/fixtures/data/group.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->ResetGroupBy();
// distinct
$client->SetGroupDistinct("id");
$file = fopen("spec/fixtures/data/distinct.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
Example #20
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetFilter('attr2', array(40, 50));
$cl->SetFilter('attr1', array(10, 20, 30));
$cl->Query('query');
 /**
  * Reset sphinx client and apply the options
  *
  * Only apply filters and group by
  *
  * @param  SearchEngineOptions $options
  * @return SphinxSearch
  */
 protected function applyOptions(SearchEngineOptions $options)
 {
     $this->resetSphinx();
     $filters = [];
     foreach ($options->getCollections() as $collection) {
         $filters[] = sprintf("%u", crc32($collection->get_databox()->get_sbas_id() . '_' . $collection->get_coll_id()));
     }
     $this->sphinx->SetFilter('crc_sbas_coll', $filters);
     $this->sphinx->SetFilter('deleted', [0]);
     $this->sphinx->SetFilter('parent_record_id', [$options->getSearchType()]);
     if ($options->getDateFields() && ($options->getMaxDate() || $options->getMinDate())) {
         foreach (array_unique(array_map(function (\databox_field $field) {
             return $field->get_name();
         }, $options->getDateFields())) as $field) {
             $min = $options->getMinDate() ? $options->getMinDate()->format('U') : 0;
             $max = $options->getMaxDate() ? $options->getMaxDate()->format('U') : pow(2, 32);
             $this->sphinx->SetFilterRange(ConfigurationPanel::DATE_FIELD_PREFIX . $field, $min, $max);
         }
     }
     if ($options->getFields()) {
         $filters = [];
         foreach ($options->getFields() as $field) {
             $filters[] = sprintf("%u", crc32($field->get_databox()->get_sbas_id() . '_' . $field->get_id()));
         }
         $this->sphinx->SetFilter('crc_struct_id', $filters);
     }
     if ($options->getBusinessFieldsOn()) {
         $crc_coll_business = [];
         foreach ($options->getBusinessFieldsOn() as $collection) {
             $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_1'));
             $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_0'));
         }
         $non_business = [];
         foreach ($options->getCollections() as $collection) {
             foreach ($options->getBusinessFieldsOn() as $BFcollection) {
                 if ($collection->get_base_id() == $BFcollection->get_base_id()) {
                     continue 2;
                 }
             }
             $non_business[] = $collection;
         }
         foreach ($non_business as $collection) {
             $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_0'));
         }
         $this->sphinx->SetFilter('crc_coll_business', $crc_coll_business);
     } elseif ($options->getFields()) {
         $this->sphinx->SetFilter('business', [0]);
     }
     /**
      * @todo : enhance : check status in a better way
      */
     $status_opts = $options->getStatus();
     foreach ($options->getDataboxes() as $databox) {
         foreach ($databox->get_statusbits() as $n => $status) {
             if (!array_key_exists($n, $status_opts)) {
                 continue;
             }
             if (!array_key_exists($databox->get_sbas_id(), $status_opts[$n])) {
                 continue;
             }
             $crc = sprintf("%u", crc32($databox->get_sbas_id() . '_' . $n));
             $this->sphinx->SetFilter('status', [$crc], $status_opts[$n][$databox->get_sbas_id()] == '0');
         }
     }
     if ($options->getRecordType()) {
         $this->sphinx->SetFilter('crc_type', [sprintf("%u", crc32($options->getRecordType()))]);
     }
     $order = '';
     switch ($options->getSortOrder()) {
         case SearchEngineOptions::SORT_MODE_ASC:
             $order = 'ASC';
             break;
         case SearchEngineOptions::SORT_MODE_DESC:
         default:
             $order = 'DESC';
             break;
     }
     switch ($options->getSortBy()) {
         case SearchEngineOptions::SORT_RANDOM:
             $sort = '@random';
             break;
         case SearchEngineOptions::SORT_RELEVANCE:
         default:
             $sort = '@relevance ' . $order . ', created_on ' . $order;
             break;
         case SearchEngineOptions::SORT_CREATED_ON:
             $sort = 'created_on ' . $order;
             break;
     }
     $this->sphinx->SetGroupBy('crc_sbas_record', SPH_GROUPBY_ATTR, $sort);
     return $this;
 }
Example #22
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetFilterRange('attr1', 10, 20, true);
$cl->SetFilter('attr3', array(30, 40, 50));
$cl->SetFilterRange('attr1', 60, 70);
$cl->SetFilter('attr2', array(80, 90, 100), true);
$cl->Query('query');
Example #23
0
fclose($file);
$client->SetSortMode(SPH_SORT_RELEVANCE, "");
// boolean
$client->SetMatchMode(SPH_MATCH_BOOLEAN);
$file = fopen("spec/fixtures/data/boolean.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->SetMatchMode(SPH_MATCH_ALL);
// phrase
$client->SetMatchMode(SPH_MATCH_PHRASE);
$file = fopen("spec/fixtures/data/phrase.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("testing this ")]);
fclose($file);
$client->SetMatchMode(SPH_MATCH_ALL);
// filter
$client->SetFilter("id", array(10, 100, 1000));
$file = fopen("spec/fixtures/data/filter.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->ResetFilters();
// group
$client->SetGroupBy("id", SPH_GROUPBY_ATTR, "id");
$file = fopen("spec/fixtures/data/group.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
$client->ResetGroupBy();
// distinct
$client->SetGroupDistinct("id");
$file = fopen("spec/fixtures/data/distinct.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
Example #24
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->SetFilter('attr', array(10, 20, 30), true);
$cl->Query('query');
Example #25
0
 function getCategory($keywords)
 {
     $q = $keywords;
     $mode = SPH_MATCH_EXTENDED;
     $item = array();
     $comma_separated = "";
     //Init config
     $host = SPHINX_SERVER;
     $port = SPHINX_PORT;
     $index = '*';
     $ranker = SPH_RANK_PROXIMITY_BM25;
     $cl = new SphinxClient();
     $cl->SetServer($host, $port);
     $cl->SetConnectTimeout(1);
     $cl->SetWeights(array(100, 1));
     $cl->SetMatchMode($mode);
     $cl->SetRankingMode($ranker);
     $cl->SetArrayResult(true);
     $cl->SetFilter('status', array('1'));
     $cl->SetGroupBy("level_1_category_id", SPH_GROUPBY_ATTR, "@count DESC");
     $res = $cl->Query($q, $index);
     $arr = array();
     if ($res && isset($res["matches"])) {
         if (is_array($res["matches"])) {
             foreach ($res["matches"] as $results) {
                 $arr[] = $results["attrs"];
             }
         }
     }
     return $arr;
 }
Example #26
0
 function draw()
 {
     //$tbl_source     = "category_bk";
     $tbl_source = "category";
     global $display;
     $keywords = AZLib::getParam('searchKeyword');
     $src_catid = (int) Url::get('sourceCategories');
     $src_l1_catid = 0;
     $src_l2_catid = 0;
     $src_l3_catid = 0;
     if ($src_catid) {
         //Kiểm tra danh mục nguồn
         $src_cat = DB::select("{$tbl_source}", "id={$src_catid}");
         if ($src_cat) {
             if ($src_cat && $src_cat['parent_id']) {
                 //DM cấp 2
                 /*$src_l1_catid = $src_cat['parent_id'];
                 		$src_l2_catid = $src_catid;
                 		*/
                 $src_cat_parent = DB::select("{$tbl_source}", "id={$src_cat['id']}");
                 if (!$src_cat_parent || $src_cat_parent && $src_cat_parent['parent_id']) {
                     //DM cấp 3
                     $src_l1_catid = $src_cat_parent['parent_id'];
                     $src_l2_catid = $src_cat['parent_id'];
                     $src_l3_catid = $src_catid;
                 } else {
                     $src_l1_catid = $src_cat['parent_id'];
                     $src_l2_catid = $src_catid;
                 }
             } else {
                 $src_l1_catid = $src_catid;
             }
         }
     }
     $des_catid = (int) AZLib::getParam('desCategories');
     $search_result = false;
     $items = array();
     $total = 0;
     if ($keywords) {
         //Nếu tìm theo từ khóa
         $q = $keywords;
         $mode = SPH_MATCH_ALL;
         //Init config
         $host = SPHINX_SERVER;
         $port = SPHINX_PORT;
         $index = SPHINX_INDEX;
         $ranker = SPH_RANK_PROXIMITY_BM25;
         $cl = new SphinxClient();
         $cl->SetServer($host, $port);
         $cl->SetConnectTimeout(1);
         $cl->_limit = 50000;
         $cl->_maxmatches = 50000;
         $cl->SetWeights(array(100, 1));
         $cl->SetMatchMode($mode);
         if ($src_l2_catid) {
             $cl->SetFilter('category_id', array($src_catid));
         } elseif ($src_l1_catid) {
             $cl->SetFilter('level_1_catid', array($src_catid));
         }
         //$cl->SetLimits( $offset , $limit, 10000 );
         $cl->SetRankingMode($ranker);
         $cl->SetArrayResult(true);
         $res = $cl->Query($q, $index);
         if ($res && isset($res["matches"])) {
             if (is_array($res["matches"])) {
                 $itemIDs = '';
                 $count = 0;
                 foreach ($res["matches"] as $results) {
                     $itemIDs .= ($itemIDs != '' ? ',' : '') . $results['id'];
                 }
                 if ($itemIDs != '') {
                     //Đếm lại số bản ghi chính xác
                     $sql = 'SELECT count(*) AS totalItem FROM item WHERE id IN(' . $itemIDs . ')';
                     if ($src_catid) {
                         if ($src_l3_catid) {
                             // Nếu tìm kiếm theo từ khóa trong danh mục cấp 3
                             $sql .= ' AND category_id = ' . $src_l3_catid;
                         } elseif ($src_l2_catid) {
                             // Nếu tìm kiếm theo từ khóa trong danh mục nào đó
                             $sql .= ' AND level_2_catid = ' . $src_l2_catid;
                         } elseif ($src_l1_catid) {
                             $sql .= ' AND level_1_catid = ' . $src_l1_catid;
                         }
                     }
                     if ($des_catid) {
                         $sql .= ' AND category_id != ' . $des_catid;
                     }
                     $re = DB::Query($sql);
                     if ($re) {
                         $row = mysql_fetch_assoc($re);
                         $total += (int) $row['totalItem'];
                     }
                     $display->add('itemids', $itemIDs);
                 }
             }
         }
     } elseif ($src_catid) {
         // Nếu giới hạn theo danh mục
         $sql = "SELECT count(*) AS itemTotal FROM item";
         if ($src_l3_catid) {
             $sql .= ' WHERE category_id = ' . $src_l3_catid;
         } elseif ($src_l2_catid) {
             $sql .= ' WHERE level_3_category_id = ' . $src_l2_catid;
         } elseif ($src_l1_catid) {
             $sql .= ' WHERE level_1_catid = ' . $src_l1_catid;
         }
         $re = DB::query($sql);
         if ($re) {
             $row = mysql_fetch_assoc($re);
             $total = $row['itemTotal'];
         }
     }
     $this->beginForm();
     //Build source categories list
     $cat_search_name = '';
     $re = DB::query("SELECT id,name,parent_id ,position,status FROM {$tbl_source} ORDER BY parent_id,position");
     $all_cats = array();
     $all_subcats = array();
     if ($re) {
         while ($cat = mysql_fetch_assoc($re)) {
             if ($cat['parent_id']) {
                 //Là danh mục cấp 2
                 if (isset($all_cats[$cat['parent_id']]) && $all_cats[$cat['parent_id']]['parent_id'] == 0) {
                     //Là danh mục cấp 2
                     $all_subcats[$cat['parent_id']][$cat['id']] = $cat;
                 }
             } else {
                 if (!isset($all_subcats[$cat['id']])) {
                     $all_subcats[$cat['id']] = array();
                 }
             }
             $all_cats[$cat['id']] = $cat;
         }
     }
     $all_top_cat = array();
     $all_top_cat[0] = 'Tất cả các danh mục';
     foreach ($all_subcats as $topid => $subcats) {
         if ($src_catid && $src_catid == $topid) {
             $cat_search_name = $all_cats[$topid]['name'];
         }
         if ($all_cats[$topid]['status'] == 'HIDE') {
             $all_cats[$topid]['name'] .= ' (ẨN)';
         }
         $all_top_cat[$topid] = $all_cats[$topid]['name'];
         foreach ($subcats as $subcat) {
             if ($src_catid && $src_catid == $subcat['id']) {
                 $cat_search_name = $subcat['name'];
             }
             if ($subcat['status'] == 'HIDE') {
                 $subcat['name'] .= ' (ẨN)';
             }
             $all_top_cat[$subcat['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $subcat['name'];
         }
     }
     //print_r($all_top_cat);
     $display->add('msg', $this->showFormSuccesMessages(1));
     $display->add('sourceCategories', $all_top_cat);
     //Build destination categories list
     $re = DB::query("SELECT id,name,parent_id,status,position FROM category ORDER BY parent_id,position");
     $all_cats = array();
     $all_subcats = array();
     $level1_cats = array();
     $level2_cats = array();
     $level3_cats = array();
     if ($re) {
         while ($cat = mysql_fetch_assoc($re)) {
             if ($cat['parent_id']) {
                 //Là danh mục cấp 2 hoặc 3
                 if (isset($all_cats[$cat['parent_id']]) && $all_cats[$cat['parent_id']]['parent_id'] == 0) {
                     //Là danh mục cấp 2
                     $all_subcats[$cat['parent_id']][$cat['id']] = $cat;
                     $cat['max'] = 0;
                     if ($cat['position'] > $level1_cats[$cat['parent_id']]['max']) {
                         $level1_cats[$cat['parent_id']]['max'] = $cat['position'];
                     }
                     $level2_cats[$cat['id']] = $cat;
                 } else {
                     //là danh mục cấp 3
                     if ($cat['position'] > $level2_cats[$cat['parent_id']]['max']) {
                         $level2_cats[$cat['parent_id']]['max'] = $cat['position'];
                     }
                     $level3_cats[$all_cats[$cat['parent_id']]['parent_id']][$cat['parent_id']][$cat['id']] = $cat;
                 }
             } else {
                 $cat['max'] = 0;
                 $level1_cats[$cat['id']] = $cat;
                 if (!isset($all_subcats[$cat['id']])) {
                     $all_subcats[$cat['id']] = array();
                 }
             }
             $all_cats[$cat['id']] = $cat;
         }
     }
     $all_top_cat = array();
     $categories = array();
     foreach ($all_subcats as $topid => $subcats) {
         if ($all_cats[$topid]['status'] == 'HIDE') {
             $all_cats[$topid]['name'] .= ' (ẨN)';
         }
         $categories[$topid] = $all_cats[$topid];
         $all_top_cat[$topid] = $all_cats[$topid]['name'];
         foreach ($subcats as $subcat) {
             if ($subcat['status'] == 'HIDE') {
                 $subcat['name'] .= ' (ẨN)';
             }
             $all_top_cat[$subcat['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $subcat['name'];
             $categories[$subcat['id']] = $subcat;
             if (isset($level2_cats[$subcat['id']]) && $level2_cats[$subcat['id']]['max']) {
                 $subcatsl3 = $level3_cats[$subcat['parent_id']][$subcat['id']];
                 foreach ($subcatsl3 as $subcatl3) {
                     if ($subcatl3['status'] == 'HIDE') {
                         $subcatl3['name'] .= ' (ẨN)';
                     }
                     $all_top_cat[$subcatl3['id']] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;" . $subcatl3['name'];
                     $subcatl3['parent_id'] .= ',' . $subcat['parent_id'];
                     $categories[$subcatl3['id']] = $subcatl3;
                 }
             }
         }
     }
     $display->add('desCategories', $all_top_cat);
     $display->add('desJSONCategories', json_encode($categories));
     $display->add("allrecord", $total);
     $display->add('keywords', $keywords);
     $display->add('cat_search_id', $src_catid);
     $display->add('cat_search_name', $cat_search_name);
     $display->add('category_id', $des_catid);
     $display->output("ManageContentCategory");
     $this->endForm();
 }
Example #27
0
require_once '../tools/main.php';
include_once "../tools/sphinxapi.php";
if (isset($_REQUEST["number"]) && intval($_REQUEST["number"]) > 9) {
    $number = $_REQUEST["number"];
} else {
    $number = 9;
}
$pageNum = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 0;
$mdb = new MeekroDB(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_TABLE_NAME, DB_PORT, DB_CHARSET);
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_HOST, SPHINX_PORT);
$sphinx->SetArrayResult(true);
$sphinx->SetLimits($pageNum * $number, $number, 5000);
$sphinx->SetMaxQueryTime(10);
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
$sphinx->SetFilter('enabled', array(1));
if (isset($_REQUEST["type"])) {
    $r1 = $mdb->queryFirstRow("SELECT id, name FROM media_type WHERE name=%s", $_REQUEST["type"]);
    $type = $r1["id"];
    //set type
    if ($type == "") {
        echo json_encode(array("result" => null, "status" => false, "error" => "请求的类型不存在"));
        exit;
    }
    $sphinx->SetFilter('type', array($type));
}
if (isset($_REQUEST['language'])) {
    $r2 = $mdb->queryFirstRow("SELECT id, name FROM media_language WHERE name=%s", $_REQUEST["language"]);
    $language = $r2["id"];
    //set language
    if ($language == "") {
Example #28
0
 /**
  * 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;
 }
Example #29
0
    fwrite($stderr, "Config Error: SPHINX_API could not be found.\n");
    exit(1);
}
include $SphinxAPI;
/* Sphinx API likes to throw PHP errors *AND* use it's own error
 * handling.
 */
assert_options(ASSERT_WARNING, 0);
/* Execute the Sphinx query. */
$sphinx = new SphinxClient();
$sphinx->SetServer(SPHINX_HOST, SPHINX_PORT);
$sphinx->SetWeights(array(0, 100, 0, 0, 50));
$sphinx->SetMatchMode(SPH_MATCH_BOOLEAN);
$sphinx->SetLimits(0, 10);
$sphinx->SetSortMode(SPH_SORT_TIME_SEGMENTS, 'date_added');
$sphinx->SetFilter('site_id', TEST_SITE_ID);
/* Execute the Sphinx query. Sphinx can ask us to retry if its
 * maxed out. Retry up to 5 times.
 */
$tries = 0;
do {
    /* Wait for one second if this isn't out first attempt. */
    if (++$tries > 1) {
        sleep(1);
    }
    $results = $sphinx->Query(TEST_QUERY, SPHINX_INDEX);
    $errorMessage = $sphinx->GetLastError();
} while ($results === false && strpos($errorMessage, 'server maxed out, retry') !== false && $tries <= 5);
/* Throw a fatal error if Sphinx errors occurred. */
if ($results === false) {
    fwrite($stderr, 'Sphinx Error: ' . ucfirst($errorMessage) . ".\n");
Example #30
0
                    }
                }
            }
        }
    }
}
////////////
// do query
////////////
$cl->SetServer($host, $port);
$cl->SetConnectTimeout(1);
$cl->SetArrayResult(true);
$cl->SetWeights(array(100, 1));
$cl->SetMatchMode($mode);
if (count($filtervals)) {
    $cl->SetFilter($filter, $filtervals);
}
if ($groupby) {
    $cl->SetGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort);
}
if ($sortby) {
    $cl->SetSortMode(SPH_SORT_EXTENDED, $sortby);
}
if ($sortexpr) {
    $cl->SetSortMode(SPH_SORT_EXPR, $sortexpr);
}
if ($distinct) {
    $cl->SetGroupDistinct($distinct);
}
if ($select) {
    $cl->SetSelect($select);