SetGroupBy() public method

set grouping attribute and function
public SetGroupBy ( $attribute, $func, $groupsort = "@group desc" )
Esempio n. 1
0
 protected function applyCriteria(ESphinxCriteria $criteria)
 {
     $this->applyMatchMode($criteria->matchMode);
     $this->applyRankMode($criteria->rankMode);
     $this->applySortMode($criteria->sortMode);
     // apply select
     if (strlen($criteria->select)) {
         $this->sphinxClient->SetSelect($criteria->select);
     }
     // apply limit
     if ($criteria->getIsLimited()) {
         $this->sphinxClient->SetLimits($criteria->offset, $criteria->limit, $criteria->max_matches, $criteria->cutoff);
     }
     // apply group
     if ($criteria->getIsGroupSetted()) {
         $this->sphinxClient->SetGroupBy($criteria->getGroupBy(), $criteria->getGroupFunc());
     }
     // apply id range
     if ($criteria->getIsIdRangeSetted()) {
         $this->sphinxClient->SetIDRange($criteria->getIdMin(), $criteria->getIdMax());
     }
     // apply weights
     $this->applyFieldWeights($criteria->getFieldWeights());
     $this->applyIndexWeights($criteria->getIndexWeights());
     // apply filters
     $this->applyFilters($criteria->getInConditions());
     $this->applyFilters($criteria->getNotInConditions(), true);
     // apply ranges
     $this->applyRanges($criteria->getInRanges());
     $this->applyRanges($criteria->getNotInRanges(), true);
 }
 protected function applyCriteria(ESphinxSearchCriteria $criteria)
 {
     $this->applyMatchMode($criteria->matchMode);
     $this->applyRankMode($criteria);
     if ($criteria->sortMode == ESphinxSort::EXTENDED) {
         $orders = '';
         if ($orderArray = $criteria->getOrders()) {
             $fields = array();
             foreach ($orderArray as $attr => $type) {
                 $fields[] = $attr . ' ' . $type;
             }
             $orders = implode(', ', $fields);
         }
         $this->applySortMode($criteria->sortMode, $orders);
     } else {
         $this->applySortMode($criteria->sortMode, $criteria->getSortBy());
     }
     // apply select
     if (strlen($criteria->select)) {
         $this->sphinxClient->SetSelect($criteria->select);
     }
     // apply limit
     if ($criteria->limit) {
         $this->sphinxClient->SetLimits($criteria->offset, $criteria->limit, $criteria->maxMatches, $criteria->cutOff);
     }
     // apply group
     if ($criteria->groupBy) {
         $this->sphinxClient->SetGroupBy($criteria->groupBy, $criteria->groupByFunc, $criteria->groupBySort);
     }
     if ($criteria->groupDistinct) {
         $this->sphinxClient->SetGroupDistinct($criteria->groupDistinct);
     }
     // apply id range
     if ($criteria->getIsIdRangeSetted()) {
         $this->sphinxClient->SetIDRange($criteria->getMinId(), $criteria->getMaxId());
     }
     // apply weights
     $this->applyFieldWeights($criteria->getFieldWeights());
     $this->applyIndexWeights($criteria->getIndexWeights());
     $this->applyFilters($criteria->getFilters());
     $this->applyRanges($criteria->getRangeFilters());
     $this->sphinxClient->SetMaxQueryTime($criteria->queryTimeout !== null ? $criteria->queryTimeout : $this->_queryTimeout);
     if (VER_COMMAND_SEARCH >= 0x11d) {
         $this->applyOptions($criteria);
     }
 }
Esempio n. 3
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'];
 }
Esempio n. 4
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_ATTR);
$cl->Query('query');
Esempio n. 5
0
function sphinxList($filterArr, $page = 0)
{
    $result = array('data' => array(), 'cate4' => array(), 'property' => array(), 'province' => array(), 'city' => array(), 'brand' => array(), 'total_found' => 0, 'time' => 0);
    $perpage = isset($filterArr['perpage']) && $filterArr['perpage'] ? $filterArr['perpage'] : 20;
    $sphinxConfig = array('host' => '172.17.17.105', 'port' => 9020);
    $sphinx = new \SphinxClient();
    $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
    if (isset($filterArr["has_children"]) && $filterArr["has_children"] == 1) {
        $indexTable = "product_distri_special";
    } elseif (isset($filterArr['cate1']) && $filterArr['cate1']) {
        $indexTable = "product_distri_" . $filterArr["cate1"];
    } else {
        $indexTable = "product_m_distri";
    }
    $gcdweight = "weight()+IF(id>900000000, tradenum*100, 0)+inquirynum*20+star*2+basescore*5+creditscore+IF(is_op=1, all_uv*10+all_pv, 0)+IF(id>900000000, weight()*0.1, 0) as gcpdweight";
    if (isset($filterArr["has_children"]) && $filterArr["has_children"] == 1) {
        $sphinx->SetSelect("id, cid, brand, cate4, feature, province, city, {$gcdweight}");
    } else {
        $sphinx->SetSelect("id, cid, brand, feature, province, city, {$gcdweight}");
    }
    /**************************************** 过滤模块 ******************************************/
    /* 分类过滤 */
    if (isset($filterArr['cate3']) && isset($filterArr['cate2to3'])) {
        $sphinx->SetFilter('cate2', array(intval($filterArr["cate3"])), false);
    } elseif (isset($filterArr['cate3'])) {
        $sphinx->SetFilter('cate3', array(intval($filterArr["cate3"])), false);
    }
    /* 是否通过工厂认证 */
    if (isset($filterArr['filters']['iscertify']) && $filterArr['filters']['iscertify'] > 0) {
        $sphinx->SetFilter('is_gccertify', array($filterArr['filters']['iscertify']), false);
    }
    /* 是否显示价格 */
    if (isset($filterArr['filters']['isprice']) && $filterArr['filters']['isprice'] > 0) {
        $sphinx->SetFilterRange('price', 1, 9999999, false);
    }
    /* 省过滤 */
    if (!empty($filterArr['filters']['province']) && is_numeric($filterArr['filters']['province'])) {
        $sphinx->SetFilter('province', array(intval($filterArr['filters']['province'])), false);
    }
    /* 市过滤 */
    if (!empty($filterArr['filters']['city']) && is_numeric($filterArr['filters']['city'])) {
        $sphinx->SetFilter('city', array(intval($filterArr['filters']['city'])), false);
    }
    /* 品牌过滤 */
    if (isset($filterArr['filters']['brand']) && $filterArr['filters']['brand'] > 0) {
        $sphinx->SetFilter('brand', array(intval($filterArr['filters']['brand'])), false);
    }
    /* 属性过滤 */
    if (isset($filterArr['filters']['feature']) && $filterArr['filters']['feature']) {
        $featureArr = explode('_', $filterArr['filters']['feature']);
        foreach ($featureArr as $value) {
            $sphinx->SetFilter('feature', array(intval($value)), false);
        }
    }
    /***************************************** 过滤结束 ******************************************/
    /***************************************** 排序 *********************************************/
    $sort = isset($filterArr['orders']['sort']) ? $filterArr['orders']['sort'] : '';
    if ($sort && $sort == 1) {
        //销量倒叙排列
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'tradenum DESC');
    } elseif ($sort && $sort == 2) {
        //热度倒叙排列
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'visitnum DESC');
    } elseif ($sort && $sort == 3) {
        //价格正序排列
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'price ASC');
    } elseif ($sort && $sort == 4) {
        //价格倒序排列
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'price DESC');
    } elseif ($sort && $sort == 5) {
        //返积分正序排列
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'integral DESC');
    } else {
        //默认综合排序
        $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'gcpdweight DESC');
    }
    /*************************************** 排序结束 ********************************************/
    /* limit限制 */
    $sphinx->SetLimits(0, $perpage * 10, $perpage * 10);
    $sphinx->AddQuery("", $indexTable);
    /**************************************** 开始并发查询 **************************************/
    /*#############  属性  ###############*/
    $sphinx->ResetGroupBy();
    $sphinx->SetGroupBy('feature', SPH_GROUPBY_ATTR, "@count desc");
    $sphinx->SetLimits(0, 200, 200);
    $sphinx->AddQuery("", $indexTable);
    /*#############  省  ###############*/
    $sphinx->ResetGroupBy();
    $sphinx->SetGroupBy('province', SPH_GROUPBY_ATTR, "@count desc");
    $sphinx->SetLimits(0, 20, 20);
    $sphinx->AddQuery("", $indexTable);
    /*#############  市  ###############*/
    $sphinx->ResetGroupBy();
    $sphinx->SetGroupBy('city', SPH_GROUPBY_ATTR, "@count desc");
    $sphinx->SetLimits(0, 20, 20);
    $sphinx->AddQuery("", $indexTable);
    /*#############  品牌  ###############*/
    $sphinx->ResetGroupBy();
    $sphinx->SetGroupBy('brand', SPH_GROUPBY_ATTR, "@count desc");
    $sphinx->SetLimits(0, 20, 20);
    $sphinx->AddQuery("", $indexTable);
    /*#############  4级分类  ###############*/
    //有子分类使用特权产品索引,可以对cate4排序
    if (isset($filterArr["has_children"]) && $filterArr["has_children"] == 1) {
        $sphinx->ResetGroupBy();
        $sphinx->SetGroupBy('cate4', SPH_GROUPBY_ATTR, "@count desc");
        $sphinx->SetLimits(0, 20, 20);
        $sphinx->AddQuery("", $indexTable);
    }
    /*#############  执行  ###############*/
    $batchResult = $sphinx->RunQueries();
    /**************************************** 完成并发查询 **************************************/
    /**************************************** 错误验证 **************************************/
    $error = $sphinx->getLastError();
    if ($error) {
        return $result;
    }
    /**************************************** 结果抽取处理 **************************************/
    if (is_array($batchResult) && count($batchResult) > 0) {
        $result['data'] = $batchResult[0];
        $result['property'] = isset($batchResult[1]) ? $batchResult[1] : array();
        $result['province'] = isset($batchResult[2]) ? $batchResult[2] : array();
        $result['city'] = isset($batchResult[3]) ? $batchResult[3] : array();
        $result['brand'] = isset($batchResult[4]) ? $batchResult[4] : array();
        $result["cate4"] = isset($batchResult[5]) ? $batchResult[5] : array();
    } else {
        return $result;
    }
    $resData = array();
    if (!isset($result['data']['matches'])) {
        $result['data']['matches'] = array();
    }
    foreach ($result['data']['matches'] as $k => $v) {
        $resData[] = array($k, $v['attrs']['cid']);
    }
    $result['total_found'] = $result['data']['total_found'];
    $result['time'] = $result['data']['time'];
    $result['data'] = $resData;
    $result['data'] = sortData($result['data']);
    if ($page > 0) {
        $result["data"] = array_slice($result["data"], ($page - 1) * $perpage, $perpage);
    }
    $result["property"] = !empty($result['property']) ? fomatSphinxData($result['property']) : array();
    $result["province"] = !empty($result['province']) ? fomatSphinxData($result['province']) : array();
    $result["city"] = !empty($result['city']) ? fomatSphinxData($result['city']) : array();
    $result["brand"] = !empty($result['brand']) ? fomatSphinxData($result['brand']) : array();
    $result["cate4"] = !empty($result['cate4']) ? fomatSphinxData($result['cate4']) : array();
    $result["property"] = json_encode($result["property"], JSON_UNESCAPED_UNICODE);
    $result["province"] = json_encode($result["province"], JSON_UNESCAPED_UNICODE);
    $result["city"] = json_encode($result["city"], JSON_UNESCAPED_UNICODE);
    $result["brand"] = json_encode($result["brand"], JSON_UNESCAPED_UNICODE);
    $result["cate4"] = json_encode($result["cate4"], JSON_UNESCAPED_UNICODE);
    return $result;
}
 public function searchQuery($search_params, $search_words, $excluded_words, &$participants, &$search_results)
 {
     global $user_info, $context, $sourcedir, $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) {
         //!!! Should this not be in here?
         // 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']);
         $mySphinx->SetMatchMode(SPH_MATCH_EXTENDED);
         // Put together a sort string; besides the main column sort (relevance, id_topic, or num_replies), add secondary sorting based on relevance value (if not the main sort method) and age
         $sphinx_sort = ($search_params['sort'] === 'id_msg' ? 'id_topic' : $search_params['sort']) . ' ' . $search_params['sort_dir'] . ($search_params['sort'] === 'relevance' ? '' : ', relevance desc') . ', poster_time desc';
         // 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);
         }
         $mySphinx->SetSortMode(SPH_SORT_EXTENDED, $sphinx_sort);
         // 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.
         $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) ? '-' : '') . smc_clean_word_sphinx($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;
         }
         // Execute the search query.
         $request = $mySphinx->Query($query, 'smf_index');
         // Can a connection to the daemon be made?
         if ($request === false) {
             // Just log the error.
             if ($mySphinx->GetLastError()) {
                 log_error($mySphinx->GetLastError());
             }
             //echo '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']['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'];
 }
Esempio n. 7
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);
}
if ($limit) {
    $cl->SetLimits(0, $limit, $limit > 1000 ? $limit : 1000);
 /**
  * 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;
 }
Esempio n. 9
0
<?php

phpinfo();
die;
$sphinx = new \SphinxClient();
$sphinx->SetServer('10.80.1.114', 9312);
$sphinx->SetConnectTimeout(1);
$sphinx->SetArrayResult(true);
$sphinx->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
$sphinx->setLimits(0, 10, 10000);
$sphinx->setMatchMode(SPH_MATCH_BOOLEAN);
$sphinx->SetSortMode(SPH_SORT_EXTENDED, "sales_volume DESC");
$sphinx->SetFilter('spec_value_id', ['409']);
//$sphinx->setIndexWeights(array(100, 1));
if (isset($param['catArray']) && !empty($param['catArray'])) {
    $sphinx->SetFilter('gc_id', $param['catArray']);
}
if (isset($param['styleArray']) && !empty($param['styleArray'])) {
    $sphinx->SetFilter('style_id', $param['styleArray']);
}
if (isset($param['brandArray']) && !empty($param['brandArray'])) {
    $sphinx->SetFilter('brand_id', $param['brandArray']);
}
$sphinx->SetGroupBy('goods_id', SPH_GROUPBY_ATTR, 'sales_volume DESC');
$res = $sphinx->Query('', 'sku_spec');
echo '<pre>';
var_dump($res);
die;
Esempio n. 10
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;
 }
Esempio n. 11
0
 public function searchQuery($search_params, $searchWords, $excludedIndexWords, &$participants, &$searchArray)
 {
     global $modSettings, $context, $sourcedir, $user_info, $scripturl;
     if (($cached_results = CacheAPI::getCache('search_results_' . md5($user_info['query_see_board'] . '_' . $context['params']))) === null) {
         require_once $sourcedir . '/contrib/sphinxapi.php';
         $mySphinx = new SphinxClient();
         $mySphinx->SetServer($modSettings['sphinx_searchd_server'], (int) $modSettings['sphinx_searchd_port']);
         $mySphinx->SetLimits(0, (int) $modSettings['sphinx_max_results']);
         $mySphinx->SetMatchMode(SPH_MATCH_BOOLEAN);
         if (!$search_params['show_complete']) {
             $mySphinx->SetGroupBy('ID_TOPIC', SPH_GROUPBY_ATTR);
         }
         $mySphinx->SetSortMode($search_params['sort_dir'] === 'asc' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC, $search_params['sort'] === 'ID_MSG' ? 'ID_TOPIC' : $search_params['sort']);
         if (!empty($search_params['topic'])) {
             $mySphinx->SetFilter('ID_TOPIC', array((int) $search_params['topic']));
         }
         if (!empty($search_params['min_msg_id']) || !empty($search_params['max_msg_id'])) {
             $mySphinx->SetIDRange(empty($search_params['min_msg_id']) ? 0 : (int) $search_params['min_msg_id'], empty($search_params['max_msg_id']) ? (int) $modSettings['maxMsgID'] : (int) $search_params['max_msg_id']);
         }
         if (!empty($search_params['brd'])) {
             $mySphinx->SetFilter('ID_BOARD', $search_params['brd']);
         }
         if (!empty($search_params['prefix'])) {
             $mySphinx->SetFilter('ID_PREFIX', $search_params['prefix']);
         }
         if (!empty($search_params['memberlist'])) {
             $mySphinx->SetFilter('ID_MEMBER', $search_params['memberlist']);
         }
         $orResults = array();
         foreach ($searchWords as $orIndex => $words) {
             $andResult = '';
             foreach ($words['indexed_words'] as $sphinxWord) {
                 $andResult .= (in_array($sphinxWord, $excludedIndexWords) ? '-' : '') . $sphinxWord . ' & ';
             }
             $orResults[] = substr($andResult, 0, -3);
         }
         $query = count($orResults) === 1 ? $orResults[0] : '(' . implode(') | (', $orResults) . ')';
         // Execute the search query.
         $request = $mySphinx->Query($query, 'smf_index');
         // Can a connection to the deamon be made?
         if ($request === false) {
             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']['relevance'] / 10000, 1) . '%', 'matches' => array());
                 if (!$search_params['show_complete']) {
                     $cached_results['matches'][$msgID]['num_matches'] = $match['attrs']['@count'];
                 }
             }
         }
         CacheAPI::putCache('search_results_' . md5($user_info['query_see_board']) . '_' . $context['params'], $cached_results, 600);
     }
     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.
     foreach ($searchWords as $orIndex => $words) {
         $searchArray = array_merge($searchArray, $searchWords[$orIndex]['subject_words']);
     }
     // Now that we know how many results to expect we can start calculating the page numbers.
     $context['page_index'] = constructPageIndex($scripturl . '?action=search2;params=' . $context['params'], $_REQUEST['start'], $cached_results['num_results'], $modSettings['search_results_per_page'], false);
     return $cached_results['num_results'];
 }
Esempio n. 12
0
 /**
  * 从sphinx获取数据
  * @author 吕小虎
  * @datetime
  * @return
  */
 public function dataFromSphinx($data)
 {
     $this->data = $data;
     //分词
     $this->data['split'] = \Xz\Func\Common\Tools::curlGetContentMs($this->di['config']->base->split . '/wd/' . urlencode($this->data['wd']), 50);
     if (empty($this->data['split'])) {
         $this->data['split'] = $data['wd'];
     }
     $sphinxConfig = $this->di["config"]["combusinessearchsphinxdist"];
     $sphinx = new \SphinxClient();
     $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
     //        $sphinx->SetServer('172.17.17.103', 9111);
     $indexTable = $sphinxConfig->table;
     $gcdweight = "weight()+IF(hasgccid>0, 1000, 0) as cbweight";
     $fieldStr = "id, comname, legal, areaid, uptime,{$gcdweight}";
     $sphinx->SetSelect($fieldStr);
     //排序 有gccid的靠前
     $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'cbweight DESC');
     //搜某个字段
     $t = isset($this->data['t']) ? trim($this->data['t']) : '';
     //搜索类型 app/common
     $type = isset($this->data['type']) ? $this->data['type'] : 'common';
     //一级分类筛选
     if (!empty($this->data['cate1id']) && intval($this->data['cate1id']) > 0) {
         //            $sphinx->AddQuery('@cate1 ' . $this->data['cate1id'], $indexTable);
         $sphinx->SetFilter('cate1', array(intval($this->data['cate1id'])), false);
     }
     //二级分类筛选
     if (!empty($this->data['cate2id']) && intval($this->data['cate2id']) > 0) {
         //            $sphinx->AddQuery('@cate2 ' . $this->data['cate2id'], $indexTable);
         $sphinx->SetFilter('cate2', array(intval($this->data['cate2id'])), false);
     }
     //地区筛选
     if (!empty($this->data['areaid']) && intval($this->data['areaid']) > 0) {
         if ($this->data['areaid'] % 10000 == 0) {
             $start = intval($this->data['areaid'] / 10000) * 10000;
             $end = $start + 9999;
             $sphinx->SetFilterRange('areaid', $start, $end, false);
         } elseif ($this->data['areaid'] % 100 == 0) {
             $start = intval($this->data['areaid'] / 100) * 100;
             $end = $start + 99;
             $sphinx->SetFilterRange('areaid', $start, $end, false);
         } else {
             $sphinx->SetFilter('areaid', array(intval($this->data['areaid'])));
         }
     }
     $offset = isset($this->data['offset']) ? intval($this->data['offset']) : 0;
     $limit = isset($this->data['limit']) ? intval($this->data['limit']) : 200;
     $max = isset($this->data['max']) ? intval($this->data['max']) : 200;
     $sphinx->SetLimits($offset, $limit, $max);
     $keyStr = '';
     //企业名称和企业法人搜索  企业注册号搜索
     if (!empty($this->data['wd'])) {
         //加快搜索速度
         $replace = array('省', '市', '区', '县', '乡', '镇', '有限公司');
         $this->data['split'] = str_replace($replace, '', $this->data['split']);
         //处理搜索词  提高搜索精度
         $keyArr = explode(' ', $this->data['split']);
         $keyArr = array_filter($keyArr);
         if (is_array($keyArr) && !empty($keyArr)) {
             foreach ($keyArr as $value) {
                 $keyStr .= '"' . $value . '" ';
             }
         }
         if (is_numeric($this->data['wd']) && mb_strlen($this->data['wd'], 'UTF-8') == 15) {
             //注册号全匹配搜索
             $sphinx->AddQuery('@regno ' . $this->data['wd'], $indexTable);
         } elseif ($t == 'legal') {
             //企业名称和法人搜索
             $sphinx->AddQuery('@legal ' . $this->data['wd'], $indexTable);
         } else {
             //企业名称和法人搜索
             $sphinx->AddQuery('@(comname,legal) ' . $keyStr, $indexTable);
         }
     }
     //cate1
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('cate1', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->AddQuery('@(comname,legal)' . $keyStr, $indexTable);
     //cate2
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('cate2', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->AddQuery('@(comname,legal)' . $keyStr, $indexTable);
     //areaid
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('areaid', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->AddQuery('@(comname,legal)' . $keyStr, $indexTable);
     $result = array();
     $batchResult = $sphinx->RunQueries();
     $error = $sphinx->getLastError();
     if ($error) {
         $result['data'] = array();
         $result['cate1'] = '[]';
         $result['cate2'] = '[]';
         $result['areaid'] = '[]';
         $result['total_found'] = 0;
         $result['time'] = 0;
         return $result;
     }
     $result = array();
     if (is_array($batchResult) && count($batchResult) > 0) {
         $result['data'] = $batchResult[0];
         $result['cate1'] = isset($batchResult[1]) ? $batchResult[1] : array();
         $result['cate2'] = isset($batchResult[2]) ? $batchResult[2] : array();
         $result['areaid'] = isset($batchResult[3]) ? $batchResult[3] : array();
         $result['total_found'] = $result['data']['total_found'];
         $result['time'] = $result['data']['time'];
     }
     $result['split'] = $this->data['split'];
     $cidArr = array();
     if (isset($result['data']['matches'])) {
         foreach ($result['data']['matches'] as $k => $v) {
             $cidArr[] = $v['attrs']['id'];
         }
     }
     $result['data'] = $cidArr;
     if (!empty($result['cate1'])) {
         $result['cate1'] = $this->fomatSphinxData($result['cate1']);
     } else {
         $result['cate1'] = array();
     }
     if (!empty($result['cate2'])) {
         $result['cate2'] = $this->fomatSphinxData($result['cate2']);
     } else {
         $result['cate2'] = array();
     }
     if (!empty($result['areaid'])) {
         $result['areaid'] = $this->fomatSphinxData($result['areaid']);
     } else {
         $result['areaid'] = array();
     }
     return $result;
 }
Esempio n. 13
0
 /**
  * Calls SphinxClient::SetGroupBy
  * @param $attribute
  * @param $func
  * @param string $groupsort
  */
 public function setGroupBy($attribute, $func, $groupsort = "@group desc")
 {
     $this->client->SetGroupBy($attribute, $func, $groupsort);
 }
Esempio n. 14
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)
 {
     global $user, $phpbb_log;
     // No keywords? No posts.
     if (!strlen($this->search_query) && !sizeof($author_ary)) {
         return false;
     }
     $id_ary = array();
     // 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));
     }
     /**
      * Allow modifying the Sphinx search options
      *
      * @event core.search_sphinx_keywords_modify_options
      * @var	string	type				Searching type ('posts', 'topics')
      * @var	string	fields				Searching fields ('titleonly', 'msgonly', 'firstpost', 'all')
      * @var	string	terms				Searching terms ('all', 'any')
      * @var	int		sort_days			Time, in days, of the oldest possible post to list
      * @var	string	sort_key			The sort type used from the possible sort types
      * @var	int		topic_id			Limit the search to this topic_id only
      * @var	array	ex_fid_ary			Which forums not to search on
      * @var	string	post_visibility		Post visibility data
      * @var	array	author_ary			Array of user_id containing the users to filter the results to
      * @var	string	author_name			The username to search on
      * @var	object	sphinx				The Sphinx searchd client object
      * @since 3.1.7-RC1
      */
     $sphinx = $this->sphinx;
     $vars = array('type', 'fields', 'terms', 'sort_days', 'sort_key', 'topic_id', 'ex_fid_ary', 'post_visibility', 'author_ary', 'author_name', 'sphinx');
     extract($this->phpbb_dispatcher->trigger_event('core.search_sphinx_keywords_modify_options', compact($vars)));
     $this->sphinx = $sphinx;
     unset($sphinx);
     $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()) {
         $phpbb_log->add('critical', $user->data['user_id'], $user->ip, 'LOG_SPHINX_ERROR', false, array($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;
 }
Esempio n. 15
0
 /**
  * 特殊的搜索,用于供应商查询收录,产品查询收录等
  *
  * @Author   tianyunzi
  * @DateTime 2015-12-23T15:16:46+0800
  * @param    [type]                   $data [description]
  * @return   [type]                         [description]
  */
 public function fromSpecialSphinx($data)
 {
     $this->data = $data;
     $this->data["split"] = "@comname " . $this->data["wd"];
     $sphinxConfig = $this->di["config"]["prosearchd"];
     $sphinx = new \SphinxClient();
     $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
     //$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
     $indexTable = "product_distri";
     //TODO 索引范围
     if (isset($this->data["cate1"]) && $this->data["cate1"] > 0) {
         $indexTable = "product_distri_" . $this->data["cate1"];
     }
     if (!isset($this->data['cateid']) && isset($this->data['cate3'])) {
         $this->data['cateid'] = $this->data['cate3'];
     }
     $sphinx->SetSelect("id, cid, brand, feature, province, city");
     if (isset($this->data["pid"]) && $this->data["pid"] > 0) {
         $this->data["split"] = "";
         $sphinx->SetFilter("id", array($this->data["pid"]), false);
     }
     if (isset($this->data["cid"]) && $this->data["cid"] > 0) {
         $this->data["split"] = "";
         $sphinx->SetFilter("cid", array($this->data["cid"]), false);
     }
     if (!empty($this->data['cateid']) && intval($this->data['cateid']) > 0) {
         $sphinx->SetFilter('cate3', array(intval($this->data['cateid'])), false);
     }
     if (!empty($this->data['brand']) && intval($this->data['brand']) > 0) {
         $sphinx->SetFilter('brand', array(intval($this->data['brand'])), false);
     }
     if (!empty($this->data['province']) && intval($this->data['province']) > 0) {
         $sphinx->SetFilter('province', array(intval($this->data['province'])), false);
     }
     if (!empty($this->data['city']) && intval($this->data['city']) > 0) {
         $sphinx->SetFilter('city', array(intval($this->data['city'])), false);
     }
     if (!empty($this->data['iscertify']) && intval($this->data['iscertify']) > 0) {
         $sphinx->SetFilter('is_gccertify', array(intval($this->data['iscertify'])), false);
     }
     if (!empty($this->data['isprice']) && intval($this->data['isprice']) > 0) {
         $sphinx->SetFilterRange('price', 1, 9999999, false);
     }
     if (!empty($this->data['feature'])) {
         $featureArr = explode('_', $this->data['feature']);
         foreach ($featureArr as $value) {
             $sphinx->SetFilter('feature', array(intval($value)), false);
         }
     }
     if (!empty($this->data['sort'])) {
         switch ($this->data['sort']) {
             case 1:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'tradenum DESC');
                 break;
             case 2:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'visitnum DESC');
                 //访问量/热度
                 break;
             case 3:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'price DESC');
                 break;
             case 4:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'price ASC');
                 break;
             case 6:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'integral DESC');
                 break;
             default:
                 $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'id DESC');
                 break;
         }
     } else {
         $sphinx->SetSortMode(SPH_SORT_EXTENDED, 'id DESC');
     }
     if (isset($this->data['pid']) && $this->data['pid'] > 0) {
         $sphinx->SetLimits(0, 1, 1);
     } else {
         $sphinx->SetLimits(0, 200, 200);
     }
     $sphinx->AddQuery($this->data['split'], $indexTable);
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('feature', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->AddQuery($this->data['split'], $indexTable);
     $sphinx->ResetGroupBy();
     if (isset($this->data["pid"]) && $this->data["pid"] > 0) {
         $sphinx->SetLimits(0, 1, 1);
     } else {
         $sphinx->SetLimits(0, 20, 20);
     }
     $sphinx->SetGroupBy('brand', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->AddQuery($this->data['split'], $indexTable);
     if (!empty($this->data['province'])) {
         //市
         $sphinx->ResetGroupBy();
         $sphinx->SetLimits(0, 20, 20);
         $sphinx->SetGroupBy('city', SPH_GROUPBY_ATTR, "@count desc");
         $sphinx->AddQuery($this->data['split'], $indexTable);
     } else {
         //省
         $sphinx->ResetGroupBy();
         $sphinx->SetLimits(0, 20, 20);
         $sphinx->SetGroupBy('province', SPH_GROUPBY_ATTR, "@count desc");
         $sphinx->AddQuery($this->data['split'], $indexTable);
     }
     $batchResult = $sphinx->RunQueries();
     $error = $sphinx->getLastError();
     if ($error) {
         $result['data'] = array();
         $result['property'] = '[]';
         $result['brand'] = '[]';
         $result['city'] = '[]';
         $result['province'] = '[]';
         $result['total_found'] = 0;
         $result['time'] = 0;
         $result['split'] = $this->data['split'];
         return $result['data'];
     }
     $result = array();
     if (is_array($batchResult) && count($batchResult) > 0) {
         $result['data'] = $batchResult[0];
         $result['property'] = isset($batchResult[1]) ? $batchResult[1] : array();
         $result['brand'] = isset($batchResult[2]) ? $batchResult[2] : array();
         if (!empty($this->data['province'])) {
             $result['city'] = isset($batchResult[3]) ? $batchResult[3] : array();
         } else {
             $result['province'] = isset($batchResult[3]) ? $batchResult[3] : array();
         }
     }
     $resData = array();
     if (isset($result['data']['matches'])) {
         foreach ($result['data']['matches'] as $k => $v) {
             $resData[] = array($k, $v['attrs']['cid']);
         }
     }
     $result['total_found'] = $result['data']['total_found'];
     $result['time'] = $result['data']['time'];
     $result['split'] = $this->data['split'];
     $result['data'] = $resData;
     if (!empty($result['province'])) {
         $province = $this->fomatSphinxData($result['province']);
         $result['province'] = json_encode($province);
     } else {
         $result['province'] = '[]';
     }
     if (!empty($result['city'])) {
         $city = $this->fomatSphinxData($result['city']);
         $result['city'] = json_encode($city);
     } else {
         $result['city'] = '[]';
     }
     if (!empty($result['brand'])) {
         $brand = $this->fomatSphinxData($result['brand']);
         $result['brand'] = json_encode($brand);
     } else {
         $result['brand'] = '[]';
     }
     if (!empty($result['property'])) {
         $property = $this->fomatSphinxData($result['property']);
         $result['property'] = json_encode($property);
     } else {
         $result['property'] = '[]';
     }
     return $result;
 }
Esempio n. 16
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_WEEK);
$cl->Query('query');
Esempio n. 17
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_YEAR);
$cl->Query('query');
Esempio n. 18
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_DAY, 'somesort');
$cl->Query('query');
Esempio n. 19
0
 public function indexAction(Application $application, Template $template)
 {
     $template->setParameter('title', 'Форс-о-метр');
     if (array_key_exists('query', $_GET)) {
         $query = $this['query'] = $_GET['query'];
         $search = new SphinxClient();
         $search->SetServer('localhost', 3312);
         $search->SetGroupBy('created_at', SPH_GROUPBY_MONTH);
         // Полный поиск (запрос):
         $search->ResetFilters();
         $search->SetMatchMode(SPH_MATCH_PHRASE);
         $search->AddQuery($query, 'forceometer');
         // Поиск уникальных в месяц (запрос):
         $search->ResetFilters();
         $search->SetMatchMode(SPH_MATCH_PHRASE);
         $search->SetFilter('uniq_m', array(1));
         $search->AddQuery($query, 'forceometer');
         // Поиск уникальных вообще (запрос):
         $search->ResetFilters();
         $search->SetMatchMode(SPH_MATCH_PHRASE);
         $search->SetFilter('uniq_f', array(1));
         $search->AddQuery($query, 'forceometer');
         $query_result = $search->RunQueries();
         // Поиск уникальных постеров месяца (вообще):
         $search->ResetFilters();
         $search->SetMatchMode(SPH_MATCH_FULLSCAN);
         $search->SetFilter('uniq_m', array(1));
         $bare_query = $search->Query('', 'forceometer');
         /*
         		    $search -> ResetFilters();
                     $search -> SetMatchMode(SPH_MATCH_FULLSCAN);
                     $bare_query2 = $search -> Query('', 'forceometer');
         */
         if (!$query_result || $query_result[0]['total_found'] == 0) {
             $this['not_found'] = 1;
             return true;
         }
         $result = array('posts' => array(), 'posters' => array(), 'uniq_m' => array(), 'uniq_f' => array());
         for ($y = 2009; $y <= date('Y'); $y++) {
             for ($m = 1; $m <= 12; $m++) {
                 // Пропуск несуществующих дат и текущего месяца:
                 if ($y == 2009 && $m < 3) {
                     continue;
                 }
                 if ($y == date('Y') && $m >= date('m')) {
                     break;
                 }
                 $stamp = $y . '-' . ($m < 10 ? '0' . $m : $m) . '-01';
                 $result['posts'][$stamp] = 0;
                 $result['posters'][$stamp] = 0;
                 $result['uniq_m'][$stamp] = 0;
                 $result['uniq_f'][$stamp] = 0;
             }
         }
         // Проходим результаты:
         if ($query_result[0]['matches']) {
             foreach ($query_result[0]['matches'] as $match) {
                 $date = $match['attrs']['@groupby'];
                 $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01';
                 if (array_key_exists($stamp, $result['posts'])) {
                     $result['posts'][$stamp] = $match['attrs']['@count'];
                 }
             }
         }
         if ($bare_query['matches']) {
             foreach ($bare_query['matches'] as $match) {
                 $date = $match['attrs']['@groupby'];
                 $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01';
                 if (array_key_exists($stamp, $result['posters'])) {
                     $result['posters'][$stamp] = $match['attrs']['@count'];
                 }
             }
         }
         if ($query_result[1]['matches']) {
             foreach ($query_result[1]['matches'] as $match) {
                 $date = $match['attrs']['@groupby'];
                 $stamp = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-01';
                 if (array_key_exists($stamp, $result['uniq_m'])) {
                     $result['uniq_m'][$stamp] = $match['attrs']['@count'];
                 }
             }
         }
         /*
                     if ($bare_query2['matches']) {
                         foreach($bare_query2['matches'] as $match) {
                             $date = $match['attrs']['@groupby'];
                             $stamp = substr($date, 0, 4) .'-'. substr($date, 4, 2) .'-01';
         
                             if (array_key_exists($stamp, $result['uniq_f']))
                                 $result['uniq_f'][$stamp] = $match['attrs']['@count'];
                         }
                     }*/
         foreach ($result['posts'] as $date => $count) {
             if ($count == 0) {
                 unset($result['posts'][$date]);
                 unset($result['posters'][$date]);
                 unset($result['uniq_f'][$date]);
                 unset($result['uniq_m'][$date]);
             }
         }
         $this['results'] = json_encode($result);
         unset($result);
         return true;
     }
     return true;
 }
Esempio n. 20
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_MONTH);
$cl->Query('query');
Esempio n. 21
0
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);
$client->ResetGroupBy();
// weights
$client->SetWeights(array(100, 1));
$file = fopen("spec/fixtures/data/weights.bin", "w");
fwrite($file, $client->_reqs[$client->AddQuery("test ")]);
fclose($file);
Esempio n. 22
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;
 }
Esempio n. 23
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;
 }
Esempio n. 24
0
 function group($attribute, $func, $groupsort = "@group desc")
 {
     $this->sphinx->SetGroupBy($attribute, $func, $groupsort);
     return $this;
 }
 /**
  * 功能描述  获取筛选
  * @author 吕小虎
  * @datetime ${DATE} ${TIME}
  * @version
  * @param
  * @return
  */
 public function dataFilterFromSphinx($data)
 {
     $this->data = $data;
     //分词
     $this->data['split'] = \Xz\Func\Common\Tools::curlGetContentMs($this->di['config']->base->split . '/wd/' . urlencode($this->data['wd']), 50);
     if (empty($this->data['split'])) {
         $this->data['split'] = $data['wd'];
     }
     $sphinxConfig = $this->di["config"]["combusinessearchsphinxdist"];
     $sphinx = new \SphinxClient();
     $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
     $indexTable = $sphinxConfig->table;
     $fieldStr = isset($data['field']) ? implode(',', $data['field']) : "id, comname, legal, areaid, uptime";
     $sphinx->SetSelect($fieldStr);
     //一级分类筛选
     if (!empty($this->data['cate1id']) && intval($this->data['cate1id']) > 0) {
         $sphinx->AddQuery('@cate1', $this->data['cate1id'], $indexTable);
     }
     //二级分类筛选
     if (!empty($this->data['cate2id']) && intval($this->data['cate2id']) > 0) {
         $sphinx->AddQuery('@cate2', $this->data['cate2id'], $indexTable);
     }
     //地区筛选
     if (!empty($this->data['areaid']) && intval($this->data['areaid']) > 0) {
         if ($this->data['areaid'] % 10000 == 0) {
             $start = intval($this->data['areaid'] / 10000) * 10000;
             $end = $start + 9999;
             $sphinx->SetFilterRange('areaid', $start, $end, false);
         } elseif ($this->data['areaid'] % 100 == 0) {
             $start = intval($this->data['areaid'] / 100) * 100;
             $end = $start + 99;
             $sphinx->SetFilterRange('areaid', $start, $end, false);
         } else {
             $sphinx->SetFilter('areaid', intval($this->data['areaid']));
         }
     }
     //企业名称和法人搜索
     $sphinx->SetLimits(0, 1, 1);
     $sphinx->AddQuery('@(comname,legal)' . $this->data['split'], $indexTable);
     $sphinx->ResetGroupBy();
     //分类
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('cate1', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->AddQuery('@(comname,legal)' . $this->data['split'], $indexTable);
     $sphinx->SetLimits(0, 20, 20);
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('cate2', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->AddQuery('@(comname,legal)' . $this->data['split'], $indexTable);
     //地区
     $sphinx->SetLimits(0, 35, 20);
     $sphinx->ResetGroupBy();
     $sphinx->SetGroupBy('areaid', SPH_GROUPBY_ATTR, "@count desc");
     $sphinx->AddQuery('@(comname,legal)' . $this->data['split'], $indexTable);
     $result = array();
     $batchResult = $sphinx->RunQueries();
     //        print_r($batchResult);
     $error = $sphinx->getLastError();
     if ($error) {
         $result = array('cate1' => array(), 'cate2' => array(), 'areaid' => array());
     } else {
         //            $result['data'] = $batchResult[0];
         $result['cate1'] = array();
         if (isset($batchResult[1]['matches']) && is_array($batchResult[1]['matches']) && !empty($batchResult[1]['matches'])) {
             foreach ($batchResult[1]['matches'] as $value) {
                 $result['cate1'][$value['attrs']['@groupby']] = $value['attrs']['@count'];
             }
         }
         $result['cate2'] = array();
         if (isset($batchResult[2]['matches']) && is_array($batchResult[2]['matches']) && !empty($batchResult[2]['matches'])) {
             foreach ($batchResult[2]['matches'] as $value) {
                 $result['cate2'][$value['attrs']['@groupby']] = $value['attrs']['@count'];
             }
         }
         $result['areaid'] = array();
         if (isset($batchResult[3]['matches']) && is_array($batchResult[3]['matches']) && !empty($batchResult[3]['matches'])) {
             foreach ($batchResult[3]['matches'] as $value) {
                 $result['areaid'][$value['attrs']['@groupby']] = $value['attrs']['@count'];
             }
         }
     }
     return $result;
 }
Esempio n. 26
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->SetRetries(10, 20);
$cl->AddQuery('test1');
$cl->SetGroupBy('attr', SPH_GROUPBY_DAY);
$cl->AddQuery('test2');
$cl->RunQueries();
Esempio n. 27
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;
 }
Esempio n. 28
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;
    }
}
Esempio n. 29
0
 /**
  * set group by attr
  * 设置分组属性
  * @param $group
  * $group = array(
  *   0=>array("attrname" => "gender","func" => "attr","sort" => "@group desc"),
  * );
  * @param $distinct
  */
 private function setGroups($group, $distinct = '')
 {
     if (!is_array($group) && empty($group)) {
         $this->halt('The argv must be an array and not null.', 1002);
     }
     $func = array('day' => SPH_GROUPBY_DAY, 'week' => SPH_GROUPBY_WEEK, 'month' => SPH_GROUPBY_MONTH, 'year' => SPH_GROUPBY_YEAR, 'attr' => SPH_GROUPBY_ATTR);
     foreach ($group as $k => $v) {
         parent::SetGroupBy($v['attrname'], $func[$v['func']], $v['sort']);
     }
     if ($distinct) {
         parent::SetGroupDistinct($distinct);
     }
 }