public function sphinxTest($keyword, $cateid) { $keyword = $this->getSplitWd($keyword); $sphinxConfig = array("host" => "172.17.17.105", "port" => 9093); $conn = new \Foolz\SphinxQL\Connection(); $conn->setParams(array('host' => $sphinxConfig["host"], 'port' => $sphinxConfig["port"])); $query = \Foolz\SphinxQL\SphinxQL::create($conn); $query->select(array("id", "cate3"))->from("product_distri_29"); $query->where("cate3", "=", intval($cateid)); $query->limit(0, 1000); $query->option("max_matches", 4000); try { $facet = Facet::create($conn)->facet('cate3')->limit(10)->orderby("count(*)", "desc"); $query->facet($facet); echo $query->compileSelect()->getCompiled(), "\n"; $sphinxRs = $query->executeBatch(); var_dump($sphinxRs[1]); echo "\n"; $sphinxql = new SphinxQL($conn); $total = $sphinxql->query('show meta'); $total = $this->formatSphQLArray($total->execute()); var_dump($total); exit; } catch (Exception $e) { return array(); } }
public function getBrand($cate1Arr) { $max_matches = 50; $sphinxConfig = $this->di["config"]["gcdprolistsphinx"]; $host = $sphinxConfig->host; $port = intval($sphinxConfig->port); $table = $sphinxConfig->table; $conn = new Connection(); $conn->setParams(array('host' => $host, 'port' => $port)); $query = SphinxQL::create($conn)->select('id', 'tradenum', 'visitnum', 'cid')->from($table); $query->option('max_matches', $max_matches); $query->limit(0, 1); $query->where('is_op', '=', 1); $query->where('cate1', 'IN', $cate1Arr); $facet = Facet::create($conn); $facet->facet('brand', 'cate3'); $facet->limit(0, 50); $query->facet($facet); $query = $query->enqueue(); $sphinxRes = $query->executeBatch(); $brandArr = $sphinxRes[1]; $tmpArr = $retArr = array(); foreach ($brandArr as $key => $value) { if (!isset($tmpArr[$value['brand']]) || isset($tmpArr[$value['brand']]) && $tmpArr[$value['brand']]['count'] < $value['count(*)']) { $tmpArr[$value['brand']] = array('cate3' => $value['cate3'], 'count' => $value['count(*)'], 'brand' => $value['brand']); $fieldArr[$value['brand']] = $value['count(*)']; } } array_multisort($fieldArr, SORT_DESC, $tmpArr, SORT_DESC); if (empty($tmpArr)) { return array(); } $retArr = array_slice($tmpArr, 0, 15); foreach ($retArr as $key => $value) { $return[$value['brand']] = $value['cate3']; } $rs = $this->getBrandInfo($return); return $rs; }
public function testLimit() { $facet = Facet::create(self::$conn)->facet(array('gid', 'title'))->orderByFunction('COUNT', '*', 'DESC')->limit(5, 5)->getFacet(); $this->assertEquals('FACET gid, title ORDER BY COUNT(*) DESC LIMIT 5, 5', $facet); }
/** * @covers \Foolz\SphinxQL\SphinxQL::facet * @covers \Foolz\SphinxQL\SphinxQL::compileSelect */ public function testFacet() { $this->refill(); // test both setting and not setting the connection foreach (array(self::$conn, null) as $conn) { $result = SphinxQL::create(self::$conn)->select()->from('rt')->facet(Facet::create($conn)->facetFunction('INTERVAL', array('gid', 300, 600))->orderByFunction('FACET', '', 'ASC'))->executeBatch()->getStored(); $this->assertArrayHasKey('id', $result[0][0]); $this->assertArrayHasKey('interval(gid,300,600)', $result[1][0]); $this->assertArrayHasKey('count(*)', $result[1][0]); $this->assertEquals('2', $result[1][0]['count(*)']); $this->assertEquals('5', $result[1][1]['count(*)']); $this->assertEquals('1', $result[1][2]['count(*)']); $result = SphinxQL::create(self::$conn)->select()->from('rt')->facet(Facet::create($conn)->facet(array('gid'))->orderBy('gid', 'ASC'))->executeBatch()->getStored(); $this->assertArrayHasKey('id', $result[0][0]); $this->assertArrayHasKey('gid', $result[1][0]); $this->assertArrayHasKey('count(*)', $result[1][0]); $this->assertEquals('1', $result[1][0]['count(*)']); $this->assertEquals('200', $result[1][0]['gid']); $this->assertEquals('3', $result[1][2]['count(*)']); $this->assertEquals('2', $result[1][3]['count(*)']); } }
/** * 功能描述 通过sphinxQL方式查询 * @author 吕小虎 * @datetime ${DATE} ${TIME} * @version * @param * @return */ public function getDataFromSpinx($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"]["combusinessearchsphinx"]; $conn = new Connection(); $indexTable = $sphinxConfig->table; $conn->setParams(array('host' => $sphinxConfig->host, 'port' => $sphinxConfig->port)); $fieldArr = array('id'); $query = SphinxQL::create($conn)->select($fieldArr)->from($indexTable); //企业名称和企业法人搜索 企业注册号搜索 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) { //注册号全匹配搜索 $query->where('regno', '=', $this->data['wd']); } else { //企业名称和法人搜索 $query->match(array('comname', 'legal'), $keyStr, true); } } //一级分类筛选 if (!empty($this->data['cate1id']) && intval($this->data['cate1id']) > 0) { $query->where('cate1', '=', intval($this->data['cate1id'])); } //二级分类筛选 if (!empty($this->data['cate2id']) && intval($this->data['cate2id']) > 0) { $query->where('cate2', '=', intval($this->data['cate2id'])); } //地区筛选 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; $query->where('areaid', 'BETWEEN', array($start, $end)); } elseif ($this->data['areaid'] % 100 == 0) { $start = intval($this->data['areaid'] / 100) * 100; $end = $start + 99; $query->where('areaid', 'BETWEEN', array($start, $end)); } else { $query->where('areaid', '=', intval($this->data['areaid'])); } } //成立时间筛选 if (isset($this->data['foundstart']) && $this->data['foundstart'] > 0) { $query->where('startdate', '>=', intval($this->data['foundstart'])); if (isset($this->data['foundend']) && $this->data['foundend'] > 0 && $this->data['foundend'] > $this->data['foundstart']) { $query->where('startdate', '<=', intval($this->data['foundend'])); } } //查询条数 if (isset($data['max'])) { $query->option('max_matches', $data['max']); } if (isset($this->data['offset']) && isset($this->data['limit'])) { $query->limit($this->data['offset'], $this->data['limit']); } //分类 $facet = Facet::create($conn)->facet('cate1'); $query->facet($facet); $facet = Facet::create($conn)->facet('cate2'); $query->facet($facet); //地区 $facet = Facet::create($conn)->facet('areaid'); $query->facet($facet); $result = array('data' => array(), 'cate1' => array(), 'cate2' => array(), 'areaid' => array()); $batchResult = $query->executeBatch(); 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(); } if (!empty($result)) { if (!empty($result['cate1'])) { $result['cate1'] = array_combine(array_column($result['cate1'], 'cate1'), array_column($result['cate1'], 'count(*)')); } if (!empty($result['cate2'])) { $result['cate2'] = array_combine(array_column($result['cate2'], 'cate2'), array_column($result['cate2'], 'count(*)')); } if (!empty($result['areaid'])) { $result['areaid'] = array_combine(array_column($result['areaid'], 'areaid'), array_column($result['areaid'], 'count(*)')); } } $cidArr = array(); if (!empty($result['data'])) { foreach ($result['data'] as $key => $value) { $cidArr[] = $value['id']; } } //根据id获取缓存 if (!empty($cidArr)) { $combusObj = new CacheCombusiness(); $fieldArr = array('cid', 'comname', 'regno', 'province', 'address', 'legal', 'gccid', 'RegistCapi', 'businessstart', 'startdate'); $result['data'] = $combusObj->getMore($cidArr, $fieldArr); } else { $result['data'] = array(); } //重新实例化以获取查询到的总数 $sphinxql = new SphinxQL($conn); $total = $sphinxql->query('show meta'); $total = $this->formatSphQLArray($total->execute()); $result['total_found'] = $total['total_found']; $result['time'] = $total['time']; $result['split'] = $this->data['split']; return $result; }
/** * 从sphinx获取数据 * @author 刘建辉 * @datetime 2015-08-19T18:05:19+0800 * @return [type] [description] */ public function getDataFromSpinx($data) { $this->data = $data; if (isset($this->data['redword']) && !empty($this->data['redword'])) { $this->data['split'] = $this->data['redword']; } else { //分词 $this->data['split'] = $this->data['wd']; $splitWd = \Xz\Func\Common\Tools::curlGetContentMs($this->di['config']->base->split . '/wd/' . urlencode($this->data['wd']), 50); if ($splitWd) { $this->data['split'] = $splitWd; } } $sphinxConfig = $this->di["config"]["prosearchsphinx"]; $conn = new Connection(); //$indexTable = "product_distri"; $indexTable = "product_m_distri"; //TODO 索引范围 if (!isset($this->data['cateid'])) { if (isset($this->data['cate3'])) { $this->data['cateid'] = $this->data['cate3']; } } //逻辑判断必须加>0 if (isset($this->data['cateid']) && $this->data['cateid'] > 0) { $cateinfo = \Xz\Lib\Cate::getCateInfo(array($this->data['cateid']), array('nav')); if (!empty($cateinfo)) { $this->data['cate1'] = $cateinfo[$this->data['cateid']]['nav'][0]['cateid']; $indexTable = "product_distri" . '_' . $this->data['cate1']; } //$indexTable = "product_distri"; } $conn->setParams(array('host' => $sphinxConfig->host, 'port' => $sphinxConfig->port)); $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"; //$gcdweight = "weight() + gcdweight as gcpdweight"; $query = SphinxQL::create($conn)->select('id', 'cid', $gcdweight)->from($indexTable); $query->match('*', $this->data['split']); $query->option('max_matches', 200); if (!empty($this->data['cateid']) && intval($this->data['cateid']) > 0) { $query->where('cate3', '=', intval($this->data['cateid'])); } if (!empty($this->data['brand']) && intval($this->data['brand']) > 0) { $query->where('brand', '=', intval($this->data['brand'])); } if (!empty($this->data['province']) && intval($this->data['province']) > 0) { $query->where('province', '=', intval($this->data['province'])); } if (!empty($this->data['city']) && intval($this->data['city']) > 0) { $query->where('city', '=', intval($this->data['city'])); } if (!empty($this->data['iscertify']) && intval($this->data['iscertify']) > 0) { $query->where('is_gccertify', '=', intval($this->data['iscertify'])); } if (!empty($this->data['isprice']) && intval($this->data['isprice']) > 0) { $query->where('price', '>', 0); } if (!empty($this->data['feature'])) { $featureArr = explode('_', $this->data['feature']); foreach ($featureArr as $value) { $query->where('feature', '=', intval($value)); } } if (!empty($this->data['sort'])) { switch ($this->data['sort']) { case 1: $query->orderBy('tradenum', 'DESC'); //销量 break; case 2: $query->orderBy('visitnum', 'DESC'); //访问量/热度 break; case 3: $query->orderBy("price", "DESC"); break; case 4: $query->orderBy("price", "ASC"); break; case 6: $query->orderBy("integral", "ASC"); break; default: $query->orderBy("gcpdweight", "DESC"); break; } } else { $query->orderBy("gcpdweight", "DESC"); } //$facet = Facet::create($conn)->facet('cate3'); //$query->facet($facet); $facet = Facet::create($conn)->facet('feature')->limit(20)->orderby("count(*)", "desc"); $query->facet($facet); //品牌 $brand = Facet::create($conn)->facet('brand')->limit(20)->orderby("count(*)", "desc"); $query->facet($brand); if (!empty($this->data['province'])) { //市 $facet = Facet::create($conn)->facet('city')->limit(20)->orderby("count(*)", "desc"); $query->facet($facet); } else { //省 $facet = Facet::create($conn)->facet('province')->limit(20)->orderby("count(*)", "desc"); $query->facet($facet); } //查询条数 //$query->limit($this->data['offset'], $this->data['limit']); $query->limit(0, 200); //匹配设定 $query->option('field_weights', array('proname' => 300)); //$query->option('ranker', 'sph04'); $batchResult = $query->executeBatch(); $result = array(); if (is_array($batchResult) && count($batchResult) > 0) { $result['data'] = $batchResult[0]; //$result['cate3'] = isset($batchResult[1]) ? $batchResult[1] : array(); $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(); } } //if (!empty($result['data'])) { foreach ($result['data'] as $key => &$value) { //拿到产品ID,企业ID $value = array($value['id'], $value['cid']); } unset($value); //防霸屏 $result['data'] = $this->sortData($result['data']); /*if (!empty($result['cate3'])) { $result['cate3'] = json_encode(array_combine(array_column($result['cate3'], 'cate3'), array_column($result['cate3'], 'count(*)'))); } else { $result['cate3'] = '[]'; }*/ if (!empty($result['province'])) { $result['province'] = json_encode(array_combine(array_column($result['province'], 'province'), array_column($result['province'], 'count(*)'))); } else { $result['province'] = '[]'; } if (!empty($result['city'])) { $result['city'] = json_encode(array_combine(array_column($result['city'], 'city'), array_column($result['city'], 'count(*)'))); } else { $result['city'] = '[]'; } if (!empty($result['brand'])) { $result['brand'] = json_encode(array_combine(array_column($result['brand'], 'brand'), array_column($result['brand'], 'count(*)'))); } else { $result['brand'] = '[]'; } if (!empty($result['property'])) { $result['property'] = json_encode(array_combine(array_column($result['property'], 'feature'), array_column($result['property'], 'count(*)'))); } else { $result['property'] = '[]'; } //} //重新实例化以获取查询到的总数 $sphinxql = new SphinxQL($conn); $total = $sphinxql->query('show meta'); $total = $this->formatSphQLArray($total->execute()); $result['total_found'] = $total['total_found']; $result['time'] = $total['time']; $result['split'] = $this->data['split']; return $result; }
/** * 功能描述 * @author 吕小虎 * @datetime ${DATE} ${TIME} * @version * @param * @return */ public function getDataFilterFromSpinx($data) { return array('cate1' => array(), 'cate2' => array(), 'areaid' => array()); $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"]["combusinessearchsphinx"]; $conn = new Connection(); $indexTable = $sphinxConfig->table; $conn->setParams(array('host' => $sphinxConfig->host, 'port' => $sphinxConfig->port)); $fieldArr = isset($data['field']) && !empty($data['field']) ? $data['field'] : array('id'); $query = SphinxQL::create($conn)->select($fieldArr)->from($indexTable); //企业名称和法人搜索 $query->match(array('comname'), $this->data['split']); //一级分类筛选 if (!empty($this->data['cate1id']) && intval($this->data['cate1id']) > 0) { // $query->match(array('cate1'), $this->data['cate1id']); $query->where('cate1', '=', intval($this->data['cate1id'])); } //二级分类筛选 if (!empty($this->data['cate2id']) && intval($this->data['cate2id']) > 0) { // $query->match(array('cate2'), $this->data['cate2id']); $query->where('cate2', '=', intval($this->data['cate2id'])); } //地区筛选 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; $query->where('areaid', 'BETWEEN', array($start, $end)); } elseif ($this->data['areaid'] % 100 == 0) { $start = intval($this->data['areaid'] / 100) * 100; $end = $start + 99; $query->where('areaid', 'BETWEEN', array($start, $end)); } else { $query->where('areaid', '=', intval($this->data['areaid'])); } } //分类 $facet = Facet::create($conn)->facet('cate1'); $query->facet($facet); $facet = Facet::create($conn)->facet('cate2'); $query->facet($facet); //地区 $facet = Facet::create($conn)->facet('areaid'); $query->facet($facet); $result = array(); // echo $query->compileSelect()->getCompiled();die(); $batchResult = $query->executeBatch(); 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(); } if (!empty($result)) { if (!empty($result['cate1'])) { $result['cate1'] = array_combine(array_column($result['cate1'], 'cate1'), array_column($result['cate1'], 'count(*)')); } if (!empty($result['cate2'])) { $result['cate2'] = array_combine(array_column($result['cate2'], 'cate2'), array_column($result['cate2'], 'count(*)')); } if (!empty($result['areaid'])) { $result['areaid'] = array_combine(array_column($result['areaid'], 'areaid'), array_column($result['areaid'], 'count(*)')); } } return $result; }
public function getList($data = array(), $facetArr = array(), $is_op = 1) { $limit = !isset($data['limit']) ? 20 : $data['limit']; $max_matches = !isset($data['max_matches']) ? 200 : $data['max_matches']; $data['page'] = isset($data['page']) ? $data['page'] : 1; if (isset($data['async']) && $data['async'] == 1) { $offset = ($data['page'] - 1) * 20 + 20; } else { $offset = ($data['page'] - 1) * 20; } $sphinxConfig = $this->di["config"]["gcdprolistsphinx"]; $host = $sphinxConfig->host; $port = intval($sphinxConfig->port); $table = $sphinxConfig->table; $conn = new Connection(); $conn->setParams(array('host' => $host, 'port' => $port)); $query = SphinxQL::create($conn)->select('id', 'cid')->from($table); $query->option('max_matches', $max_matches); if ($is_op == 1) { $query->where('is_op', '=', 1); } if (!empty($data['cateinfo']['cateid'])) { $query->where('cate' . $data['cateinfo']['level'], '=', intval($data['cateinfo']['cateid'])); } if (!empty($data['filter']['brand']) && is_numeric($data['filter']['brand'])) { $query->where('brand', '=', $data['filter']['brand']); } if (!empty($data['filter']['province']) && is_numeric($data['filter']['province'])) { $query->where('province', '=', $data['filter']['province']); } if (!empty($data['filter']['city']) && is_numeric($data['filter']['city'])) { $query->where('city', '=', $data['filter']['city']); } if (!empty($data['cid']) && is_numeric($data['cid'])) { $query->where('cid', '=', $data['cid']); } if (!empty($data['filter']['feature'])) { $featureArr = explode('_', $data['filter']['feature']); foreach ($featureArr as $value) { $query->where('feature', '=', intval($value)); } } // if (!empty($data['filter']['attr']) && is_numeric($data['filter']['attr'])) { // $query->where('attr', '=', $data['filter']['attr']); // } if (!empty($data['order']) && is_numeric($data['order'])) { switch ($data['order']) { case 1: $query->orderBy('gcdweight', 'DESC'); //综合 break; case 2: $query->orderBy('tradenum', 'DESC'); //销量 break; case 3: $query->orderBy('price', 'DESC'); //价格高 break; case 4: $query->orderBy('price', 'ASC'); //价格低 break; case 5: $query->orderBy('visitnum', 'DESC'); //访问量 break; case 6: $query->orderBy('integral', 'DESC'); //返积分 break; } } $query->limit($offset, $limit); if (!empty($facetArr)) { foreach ($facetArr as $key => $value) { $facet = Facet::create($conn); $facet->facet($value)->orderBy('count(*)', 'desc')->limit(20); //$facet->facet($value); $query->facet($facet); } } $query = $query->enqueue(); $query->query('SHOW META'); $sphinxRes = $query->executeBatch(); $result = $this->doSphinx($sphinxRes, $facetArr); return $result; }