/**
  * Search for the specified query string.
  *
  * @param string $query The query string that we are searching for.
  * @param array $indexes The indexes to perform the search on.
  * @param array $options The options for the query.
  * @param bool $escapeQuery Should the query string be escaped?
  *
  * @return array The results of the search.
  */
 public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
 {
     if ($escapeQuery) {
         $query = $this->sphinx->escapeString($query);
     }
     /**
      * Build the list of indexes to be queried.
      */
     $indexNames = '';
     foreach ($indexes as &$label) {
         if (isset($this->indexes[$label])) {
             $indexNames .= $this->indexes[$label] . ' ';
         }
     }
     /**
      * If no valid indexes were specified, return an empty result set.
      *
      * FIXME: This should probably throw an exception.
      */
     if (empty($indexNames)) {
         return array();
     }
     /**
      * Set the offset and limit for the returned results.
      */
     if (isset($options['result_offset']) && isset($options['result_limit'])) {
         $this->sphinx->setLimits($options['result_offset'], $options['result_limit']);
     }
     /**
      * Weight the individual fields.
      */
     if (isset($options['field_weights'])) {
         $this->sphinx->setFieldWeights($options['field_weights']);
     }
     /**
      * Perform the query.
      */
     $start = round(microtime(true) * 1000);
     $results = $this->sphinx->query($query, $indexNames);
     if ($results['status'] === SEARCHD_ERROR) {
         throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
     }
     if ($results['status'] === SEARCHD_RETRY) {
         throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with retry "%s".', $label, $query, $this->sphinx->getLastError()));
     }
     $end = round(microtime(true) * 1000) - $start;
     $event = new SearchEvent($query, $indexes, $end);
     $this->dispatcher->dispatch("sphinx.event.search", $event);
     return $results;
 }
 /**
  * Search for the specified query string.
  *
  * @param string $query The query string that we are searching for.
  * @param array $indexes The indexes to perform the search on.
  *
  * @return array The results of the search.
  *
  * $indexes should look like:
  *
  * $indexes = array(
  *   'IndexLabel' => array(
  *     'result_offset' => (int), // optional unless result_limit is set
  *     'result_limit'  => (int), // optional unless result_offset is set
  *     'field_weights' => array( // optional
  *       'FieldName'   => (int),
  *       ...,
  *     ),
  *   ),
  *   ...,
  * );
  */
 public function search($query, array $indexes, $escapeQuery = true)
 {
     if ($escapeQuery) {
         $query = $this->sphinx->escapeString($query);
     }
     $results = array();
     foreach ($indexes as $label => $options) {
         /**
          * Ensure that the label corresponds to a defined index.
          */
         if (!isset($this->indexes[$label])) {
             continue;
         }
         /**
          * Set the offset and limit for the returned results.
          */
         if (isset($options['result_offset']) && isset($options['result_limit'])) {
             $this->sphinx->setLimits($options['result_offset'], $options['result_limit']);
         }
         /**
          * Weight the individual fields.
          */
         if (isset($options['field_weights'])) {
             $this->sphinx->setFieldWeights($options['field_weights']);
         }
         /**
          * Perform the query.
          */
         $results[$label] = $this->sphinx->query($query, implode(' ', $this->indexes[$label]["index"]));
         if ($results[$label]['status'] !== SEARCHD_OK) {
             throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
         }
     }
     /**
      * If only one index was searched, return that index's results directly.
      */
     if (count($indexes) === 1 && count($results) === 1) {
         $results = reset($results);
     }
     /**
      * FIXME: Throw an exception if $results is empty?
      */
     return $results;
 }
 /**
  * Search for the specified query string.
  *
  * @param string $query The query string that we are searching for.
  * @param array $indexes The indexes to perform the search on.
  *
  * @return ResultCollection The results of the search.
  *
  * $indexes should have the format:
  *
  *	$indexes = array(
  *		'IndexLabel' => array(
  *			'result_offset'	=> (int),
  *			'result_limit'	=> (int)
  *		),
  *		...,
  *	);
  */
 public function search($query, array $indexes)
 {
     // $query = $this->sphinx->escapeString($query);
     $results = array();
     foreach ($indexes as $label => $options) {
         /**
          * Ensure that the label corresponds to a defined index.
          */
         if (!isset($this->indexes[$label])) {
             continue;
         }
         /**
          * Set the offset and limit for the returned results.
          */
         if (isset($options['result_offset']) && isset($options['result_limit']) && is_numeric($options['result_offset']) && is_numeric($options['result_limit'])) {
             $this->sphinx->setLimits($options['result_offset'], $options['result_limit']);
         }
         /**
          * Weight the individual fields.
          */
         if (!empty($this->indexes[$label]['field_weights'])) {
             $this->sphinx->setFieldWeights($this->indexes[$label]['field_weights']);
         }
         /**
          * Perform the query.
          */
         $results[$label] = $this->sphinx->query($query, implode(' ', $this->indexes[$label]["index"]));
         if ($this->sphinx->IsConnectError()) {
             throw new ConnectionException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
         } elseif ($results[$label]['status'] !== SEARCHD_OK) {
             throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
         }
     }
     /**
      * FIXME: Throw an exception if $results is empty?
      */
     return new ResultCollection($results, $this->mapping, $this->em);
 }
 /**
  * 关键词获取普通产品获取相关分类
  * @param array $splitKeywords
  * @return array
  */
 public function getRelaCateCommon($splitKeywords, $cateid = 0, $limit = 20)
 {
     $sphinxConfig = $this->di["config"]["prosearchd"];
     $sphinx = new \SphinxClient();
     $sphinx->SetServer($sphinxConfig['host'], intval($sphinxConfig['port']));
     $sphinx->SetSelect("id, cate3");
     $sphinx->SetFilter("cate3", array(1270), true);
     if ($cateid) {
         $sphinx->SetFilter("cate3", array($cateid), false);
     }
     $sphinx->SetFilterRange("id", 1, 900000000, false);
     $sphinx->SetLimits(0, $limit, $limit);
     $sphinx->SetGroupBy('cate3', SPH_GROUPBY_ATTR, "@count desc");
     $batchResult = $sphinx->query($splitKeywords, "product_distri");
     $error = $sphinx->getLastError();
     if ($error) {
         return array();
     }
     $result = $this->fomatSphinxData($batchResult);
     if (empty($result) || !is_array($result)) {
         $result = array();
     }
     return $result;
 }
Example #5
0
 /**
  * @brief returns errors if any
  */
 public function getLastError()
 {
     return $this->client->getLastError();
 }
Example #6
0
 /**
  * 从sphinx获取数据 不带分组筛选
  * @author 吕小虎
  * @datetime
  * @return
  */
 public function sphinx($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, 100, 0) as cbweight";
     $fieldStr = "id, comname, legal, areaid, uptime,{$gcdweight}";
     $sphinx->SetSelect($fieldStr);
     //排序 有gccid的靠前
     if (isset($data['order']) && !empty($data['order'])) {
         $sphinx->SetSortMode(SPH_SORT_EXTENDED, $data['order']);
     } else {
         $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->SetFilter('cate1', array(intval($this->data['cate1id'])), false);
     }
     //二级分类筛选
     if (!empty($this->data['cate2id']) && intval($this->data['cate2id']) > 0) {
         $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'])));
         }
     }
     //成立时间筛选
     if (isset($this->data['foundstart']) && $this->data['foundstart'] > 0) {
         $start = intval($this->data['foundstart']);
         $end = isset($this->data['foundend']) && $this->data['foundend'] > $this->data['foundstart'] ? intval($this->data['foundend']) : time();
         $sphinx->SetFilterRange('startdate', $start, $end, false);
     }
     //成立时间筛选
     if (isset($this->data['busstart']) && $this->data['busstart'] > 0) {
         $start = intval($this->data['busstart']);
         $end = isset($this->data['busend']) && $this->data['busend'] > $this->data['busstart'] ? intval($this->data['busend']) : time();
         $sphinx->SetFilterRange('businessstart', $start, $end, false);
     }
     //注册资本
     if (isset($this->data['regcapstart']) && $this->data['regcapstart'] > 0) {
         $start = intval($this->data['regcapstart']);
         $end = isset($this->data['regcapend']) && $this->data['regcapend'] > $this->data['regcapstart'] ? intval($this->data['regcapend']) : 100000000;
         $sphinx->SetFilterRange('regcapital', $start, $end, false);
     }
     //企业状态
     if (isset($this->data['state']) && $this->data['state'] > 0) {
         if ($this->data['state'] == 10) {
             //有效企业
             $sphinx->SetFilterRange('intstate', 0, 10, false);
         } elseif ($this->data['state'] == 20) {
             //无效企业
             $sphinx->SetFilterRange('intstate', 11, 20, false);
         } else {
             $sphinx->SetFilter('intstate', array(intval($this->data['state'])), false);
         }
     }
     $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);
     //企业名称和企业法人搜索  企业注册号搜索
     if (!empty($this->data['wd'])) {
         //处理搜索词
         $keyArr = explode(' ', $this->data['split']);
         $keyArr = array_filter($keyArr);
         $keyStr = '';
         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);
         }
     } else {
         $sphinx->AddQuery('', $indexTable);
     }
     $batchResult = $sphinx->RunQueries();
     $error = $sphinx->getLastError();
     if ($error) {
         $result['data'] = array();
         $result['total_found'] = 0;
         $result['time'] = 0;
         return $result;
     }
     $result = array();
     if (is_array($batchResult) && count($batchResult) > 0) {
         $result['data'] = $batchResult[0];
         $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;
     return $this->outputData($result);
 }
 /**
  * Runs a search against sphinx
  *
  * @param array $args
  * @return array Sphinx result set
  */
 public function search_posts($args)
 {
     $options = $this->get_options();
     $defaults = array('search_using' => 'any', 'sort' => 'match', 'paged' => 1, 'posts_per_page' => 0, 'showposts' => 0);
     $args = wp_parse_args($args, $defaults);
     $sphinx = new SphinxClient();
     $sphinx->setServer($options['server'], $options['port']);
     $search = $args['s'];
     switch ($args['search_using']) {
         case 'all':
             $sphinx->setMatchMode(SPH_MATCH_ALL);
             break;
         case 'exact':
             $sphinx->setMatchMode(SPH_MATCH_PHRASE);
             break;
         default:
             $sphinx->setMatchMode(SPH_MATCH_ANY);
     }
     switch ($args['sort']) {
         case 'date':
             $sphinx->setSortMode(SPH_SORT_ATTR_DESC, 'date_added');
             break;
         case 'title':
             $sphinx->setSortMode(SPH_SORT_ATTR_ASC, 'title');
             break;
         default:
             $sphinx->setSortMode(SPH_SORT_RELEVANCE);
     }
     $page = isset($args['paged']) && intval($args['paged']) > 0 ? intval($args['paged']) : 1;
     $per_page = max(array($args['posts_per_page'], $args['showposts']));
     if ($per_page < 1) {
         $per_page = get_option('posts_per_page');
     }
     $sphinx->setLimits(($page - 1) * $per_page, $per_page);
     $sphinx->setMaxQueryTime(intval($options['timeout']));
     $result = $sphinx->query($search, $options['index']);
     $this->last_error = $sphinx->getLastError();
     $this->last_warning = $sphinx->getLastWarning();
     return $result;
 }
Example #8
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;
 }
Example #9
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;
}
 /**
  * 功能描述  获取筛选
  * @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;
 }
Example #11
0
 /**
  * RunQueries() + validate.
  *
  * - Single Query: Resultados da Query
  *
  * - Multi Query:  Array de Resultados das Querys
  *
  * Formato de cada Resultado:
  *
  * <code>
  * //Results
  * array(
  *     array(
  *         'id'     => 12345,
  *         'weight' => 30,
  *         'attrs'  => array(...)
  *     ),
  *     array(
  *         'id'     => 23456,
  *         'weight' => 20,
  *         'attrs'  => array(...)
  *     ),
  *     ...
  * );
  * </code>
  *
  * @param \SphinxClient $sphinxClient
  *
  * @throws \Exception
  *
  * @return array
  */
 protected function getResult(\SphinxClient $sphinxClient)
 {
     $result = $sphinxClient->RunQueries();
     if (false === $result) {
         throw new \Exception($sphinxClient->getLastError());
     }
     if ($sphinxClient->GetLastWarning()) {
         throw new \Exception($sphinxClient->GetLastWarning());
     }
     if (false === $result) {
         throw new \Exception($sphinxClient->getLastError());
     }
     if ($sphinxClient->GetLastWarning()) {
         throw new \Exception($sphinxClient->GetLastWarning());
     }
     //Suporte ao formato inicial de unica query
     if (count($result) === 1) {
         return current($result);
     }
     return $result;
 }