search.model.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /**
  3. * 搜索
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class searchModel{
  11. //是否开启分面搜索
  12. private $_open_face = false;
  13. //全文搜索对象
  14. private $_xs_search;
  15. //全文搜索对象
  16. private $_xs_index;
  17. //全文搜索到的商品ID数组
  18. private $_indexer_ids = array();
  19. //全文搜索到的品牌数组
  20. private $_indexer_brands = array();
  21. //全文检索到的商品分类数组
  22. private $_indexer_cates = array();
  23. //全文搜索结果总数
  24. private $_indexer_count;
  25. //搜索结果中品牌分面信息
  26. private $_face_brand = array();
  27. //搜索结果中品牌分面信息
  28. private $_face_attr = array();
  29. /**
  30. * 从全文索引库搜索关键词
  31. * @param unknown $condition 条件
  32. * @param unknown $order 排序
  33. * @param number $pagesize 每页显示商品数
  34. * @return
  35. */
  36. public function getIndexerList($condition = array(), $order = array(), $pagesize = 24) {
  37. try {
  38. //全文搜索初始化
  39. $this->_createXS($pagesize,C('fullindexer.appname'));
  40. //设置搜索内容
  41. $this->_setQueryXS($condition,$order);
  42. //执行搜索
  43. $this->_searchXS();
  44. return array($this->_indexer_ids, $this->_indexer_count);
  45. } catch (XSException $e) {
  46. // if (C('debug')) {
  47. // showMessage($e->getMessage(),'','html','error');
  48. // } else {
  49. Shopnc\Log::record('search\index'.$e->getMessage()."\r\n".$sql,Shopnc\Log::RUN);
  50. return false;
  51. // }
  52. }
  53. }
  54. /**
  55. * 设置全文检索查询条件
  56. * @param unknown $condition
  57. * @param array $order
  58. */
  59. private function _setQueryXS($condition,$order) {
  60. $condition['keyword'] = preg_replace("/([a-zA-Z0-9]{2,})/",' ${1} ',$condition['keyword']);
  61. $this->_xs_search->setQuery(is_null($condition['keyword']) ? '':$condition['keyword']);
  62. if (isset($condition['cate'])) {
  63. $this->_xs_search->addQueryString($condition['cate']['key'].':'.$condition['cate']['value']);
  64. } else {
  65. $this->_open_face = false;
  66. }
  67. if (isset($condition['brand_id'])) {
  68. $this->_xs_search->addQueryString('brand_id'.':'.$condition['brand_id']);
  69. }
  70. if (isset($condition['store_id'])) {
  71. $this->_xs_search->addQueryString('store_id'.':'.$condition['store_id']);
  72. }
  73. if (isset($condition['area_id'])) {
  74. $this->_xs_search->addQueryString('area_id'.':'.$condition['area_id']);
  75. }
  76. if (isset($condition['have_gift'])) {
  77. $this->_xs_search->addQueryString('have_gift'.':'.$condition['have_gift']);
  78. }
  79. if (is_array($condition['attr_id'])) {
  80. foreach ($condition['attr_id'] as $attr_id) {
  81. $this->_xs_search->addQueryString('attr_id'.':'.$attr_id);
  82. }
  83. }
  84. if (count($order) > 1) {
  85. $this->_xs_search->setMultiSort($order);
  86. } else {
  87. $this->_xs_search->setSort($order);
  88. }
  89. }
  90. /**
  91. * 创建全文搜索对象,并初始化基本参数
  92. * @param number $pagesize 每页显示商品数
  93. * @param string $appname 全文搜索INI配置文件名
  94. */
  95. private function _createXS($pagesize,$appname) {
  96. if (is_numeric($_GET['curpage']) && $_GET['curpage'] > 0) {
  97. $curpage = intval($_GET['curpage']);
  98. $start = ($curpage-1) * $pagesize;
  99. } else {
  100. $start = null;
  101. }
  102. require_once(BASE_DATA_PATH.'/api/xs/lib/XS.php');
  103. $obj_doc = new XSDocument();
  104. $obj_xs = new XS(C('fullindexer.appname'));
  105. $this->_xs_search = $obj_xs->search;
  106. $this->_xs_index = $obj_xs->index;
  107. $this->_xs_search->setCharset(CHARSET);
  108. $this->_xs_search->setLimit($pagesize,$start);
  109. //设置分面
  110. if ($this->_open_face) {
  111. $this->_xs_search->setFacets(array('brand_id','attr_id'));
  112. }
  113. }
  114. /**
  115. * 执行全文搜索
  116. */
  117. private function _searchXS(){
  118. // $goods_class = Model('goods_class')->getGoodsClassIndexedListAll();
  119. $docs = $this->_xs_search->search();
  120. $count = $this->_xs_search->getLastCount();
  121. $goods_ids = array();
  122. $brands = array();
  123. $cates = array();
  124. foreach ($docs as $k => $doc) {
  125. $goods_ids[] = $doc->goods_id;
  126. // if ($doc->brand_id > 0) {
  127. // $brands[$doc->brand_id]['brand_id'] = $doc->brand_id;
  128. // $brands[$doc->brand_id]['brand_name'] = $doc->brand_name;
  129. // }
  130. // if ($doc->gc_id > 0) {
  131. // $cates[$doc->gc_id]['gc_id'] = $doc->gc_id;
  132. // $cates[$doc->gc_id]['gc_name'] = $goods_class[$doc->gc_id]['gc_name'];
  133. // }
  134. }
  135. $this->_indexer_ids = $goods_ids;
  136. $this->_indexer_count = $count;
  137. $this->_indexer_brands = $brands;
  138. $this->_indexer_cates = $cates;
  139. //读取分面结果
  140. if ($this->_open_face) {
  141. $this->_face_brand = $this->_xs_search->getFacets('brand_id');
  142. $this->_face_attr = $this->_xs_search->getFacets('attr_id');
  143. $this->_parseFaceAttr($this->_face_attr);
  144. }
  145. return true;
  146. }
  147. /**
  148. * 处理属性多面信息
  149. */
  150. private function _parseFaceAttr($face_attr = array()) {
  151. if (!is_array($face_attr)) return;
  152. $new_attr = array();
  153. foreach ($face_attr as $k => $v) {
  154. $new_attr = array_merge($new_attr,explode('_',$k));
  155. }
  156. $this->_face_attr = $new_attr;
  157. }
  158. /**
  159. * 删除没有商品的品牌(不显示)
  160. * @param unknown $brand_array
  161. * @return unknown|multitype:
  162. */
  163. public function delInvalidBrand($brand_array = array()) {
  164. if (!$this->_open_face) return $brand_array;
  165. if (is_array($brand_array) && is_array($this->_face_brand)) {
  166. foreach ($brand_array as $k => $v) {
  167. if (!isset($this->_face_brand[$k])) {
  168. unset($brand_array[$k]);
  169. }
  170. }
  171. }
  172. return $brand_array;
  173. }
  174. /**
  175. * 删除没有商品的属性(不显示)
  176. * @param unknown $brand_array
  177. * @return unknown|multitype:
  178. */
  179. public function delInvalidAttr($attr_array = array()) {
  180. if (!$this->_open_face) return $attr_array;
  181. if (is_array($attr_array) && is_array($this->_face_attr)) {
  182. foreach ($attr_array as $key => $value) {
  183. if (is_array($value['value'])) {
  184. foreach ($value['value'] as $k => $v) {
  185. if (!in_array($k,$this->_face_attr)) {
  186. unset($attr_array[$key]['value'][$k]);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. return $attr_array;
  193. }
  194. public function __get($key) {
  195. return $this->$key;
  196. }
  197. /**
  198. * 删除搜索索引中的无效商品
  199. * @param unknown $brand_array
  200. * @return unknown|multitype:
  201. */
  202. public function delInvalidGoods($goods_list, $indexer_ids = array()) {
  203. $goods_ids = array();
  204. foreach ($goods_list as $k => $v) {
  205. $goods_ids[] = $v['goods_id'];
  206. }
  207. $_diff_ids = array_diff($indexer_ids,$goods_ids);
  208. if (!empty($_diff_ids)) {
  209. file_put_contents(BASE_DATA_PATH.'/log/search.log',date('Y-m-d H:i:s',TIMESTAMP)."\r\n",FILE_APPEND);
  210. file_put_contents(BASE_DATA_PATH.'/log/search.log',implode(',',$indexer_ids)."\r\n",FILE_APPEND);
  211. file_put_contents(BASE_DATA_PATH.'/log/search.log',implode(',',$goods_ids)."\r\n",FILE_APPEND);
  212. file_put_contents(BASE_DATA_PATH.'/log/search.log',implode(',',$_diff_ids)."\r\n\r\n",FILE_APPEND);
  213. // $this->_xs_index->del($_diff_ids);
  214. // QueueClient::push('flushIndexer', '');
  215. }
  216. }
  217. /**
  218. * 取得商品分类详细信息
  219. *
  220. * @param array $search_param 需要的参数内容
  221. * @return array 数组类型的返回结果
  222. */
  223. public function getAttr($get, $default_classid){
  224. if(!empty($get['a_id'])){
  225. $attr_ids = explode('_', $get['a_id']);
  226. }
  227. $data = array();
  228. $model_type = Model('type');
  229. // 获取当前的分类内容
  230. $class_array = Model('goods_class')->getGoodsClassForCacheModel();
  231. $data['class'] = $class_array[$get['cate_id']];
  232. if (empty($data['class']['child']) && empty($data['class']['childchild'])) {
  233. // 根据属性查找商品
  234. if (is_array($attr_ids)) {
  235. // 商品id数组
  236. $goodsid_array = array();
  237. $data['sign'] = false;
  238. foreach ($attr_ids as $val) {
  239. $where = array();
  240. $where['attr_value_id'] = $val;
  241. if ($data['sign']) {
  242. $where['goods_id'] = array('in', $goodsid_array);
  243. }
  244. $goodsattrindex_list = Model('goods_attr_index')->getGoodsAttrIndexList($where, 'goods_id');
  245. if (!empty($goodsattrindex_list)) {
  246. $data['sign'] = true;
  247. $tpl_goodsid_array = array();
  248. foreach ($goodsattrindex_list as $val) {
  249. $tpl_goodsid_array[] = $val['goods_id'];
  250. }
  251. $goodsid_array = $tpl_goodsid_array;
  252. } else {
  253. $data['goodsid_array'] = $goodsid_array = array();
  254. $data['sign'] = false;
  255. break;
  256. }
  257. }
  258. if ($data['sign']) {
  259. $data['goodsid_array'] = $goodsid_array;
  260. }
  261. }
  262. }
  263. $class = $class_array[$default_classid];
  264. if (empty($class['child']) && empty($class['childchild'])) {
  265. //获得分类对应的类型ID
  266. $type_id = $class['type_id'];
  267. //品牌列表
  268. $typebrand_list = Model('type')->getTypeBrandList(array('type_id' => $type_id), 'brand_id');
  269. if (!empty($typebrand_list)) {
  270. $brandid_array = array();
  271. foreach ($typebrand_list as $val) {
  272. $brandid_array[] = $val['brand_id'];
  273. }
  274. $brand_array = Model('brand')->getBrandPassedList(array('brand_id' => array('in', $brandid_array)), 'brand_id,brand_name');
  275. $brand_array = array_under_reset($brand_array, 'brand_id');
  276. }
  277. // 被选中的品牌
  278. $brand_id = intval($get['b_id']);
  279. if ($brand_id > 0 && !empty($brand_array)){
  280. $checked_brand = array();
  281. if(isset($brand_array[$brand_id])){
  282. $checked_brand[$brand_id]['brand_name'] = $brand_array[$brand_id]['brand_name'];
  283. }
  284. }
  285. //属性列表
  286. $model_attribute = Model('attribute');
  287. $attribute_list = $model_attribute->getAttributeShowList(array('type_id' => $type_id), 'attr_id,attr_name');
  288. $attributevalue_list = $model_attribute->getAttributeValueList(array('type_id' => $type_id), 'attr_value_id,attr_value_name,attr_id');
  289. $attributevalue_list = array_under_reset($attributevalue_list, 'attr_id', 2);
  290. $attr_array = array();
  291. if (!empty($attribute_list)) {
  292. foreach ($attribute_list as $val) {
  293. $attr_array[$val['attr_id']]['name'] = $val['attr_name'];
  294. $tpl_array = array_under_reset($attributevalue_list[$val['attr_id']], 'attr_value_id');
  295. $attr_array[$val['attr_id']]['value'] = $tpl_array;
  296. }
  297. }
  298. // 被选中的属性
  299. if(is_array($attr_ids) && !empty($attr_array)){
  300. $checked_attr = array();
  301. foreach ($attr_ids as $s){
  302. foreach ($attr_array as $k=>$d){
  303. if(isset($d['value'][$s])){
  304. $checked_attr[$k]['attr_name'] = $d['name'];
  305. $checked_attr[$k]['attr_value_id'] = $s;
  306. $checked_attr[$k]['attr_value_name']= $d['value'][$s]['attr_value_name'];
  307. }
  308. }
  309. }
  310. }
  311. if (C('fullindexer.open')) {
  312. $brand_array = $this->delInvalidBrand($brand_array);
  313. $attr_array = $this->delInvalidAttr($attr_array);
  314. }
  315. }
  316. return array($data, $brand_array, $attr_array, $checked_brand, $checked_attr);
  317. }
  318. /**
  319. * 从TAG中查找分类
  320. */
  321. public function getTagCategory($keyword = '') {
  322. if ($keyword != '') {
  323. // 跟据class_tag缓存搜索出与keyword相关的分类
  324. $tag_list = rkcache('class_tag', true);
  325. if (!empty($tag_list) && is_array($tag_list)) {
  326. foreach($tag_list as $key => $val) {
  327. $tag_value = str_replace(',', '==ShopNC==', $val['gc_tag_value']);
  328. if (strpos($tag_value, $keyword)) {
  329. $data[] = $val['gc_id'];
  330. }
  331. }
  332. }
  333. }
  334. return $data;
  335. }
  336. /**
  337. * 获取父级分类,递归调用
  338. */
  339. private function _getParentCategory($gc_id, $goods_class, $data) {
  340. array_unshift($data, $gc_id);
  341. if ($goods_class[$gc_id]['gc_parent_id'] != 0) {
  342. return $this->_getParentCategory($goods_class[$gc_id]['gc_parent_id'], $goods_class, $data);
  343. } else {
  344. return $data;
  345. }
  346. }
  347. /**
  348. * 显示左侧商品分类
  349. * @param array $param 分类id
  350. * @sign int $sign 0为取得最后一级的同级分类,1为不取得
  351. */
  352. public function getLeftCategory($param, $sign = 0) {
  353. $data = array();
  354. if (!empty($param)) {
  355. $goods_class = Model('goods_class')->getGoodsClassForCacheModel();
  356. foreach ($param as $val) {
  357. $data[] = $this->_getParentCategory($val, $goods_class, array());
  358. }
  359. }
  360. $tpl_data = array();
  361. $gc_list = Model('goods_class')->get_all_category();
  362. foreach ($data as $value) {
  363. //$tpl_data[$val[0]][$val[1]][$val[2]] = $val[2];
  364. if (!empty($gc_list[$value[0]])){ // 一级
  365. $tpl_data[$value[0]]['gc_id'] = $gc_list[$value[0]]['gc_id'];
  366. $tpl_data[$value[0]]['gc_name'] = $gc_list[$value[0]]['gc_name'];
  367. if (!empty($gc_list[$value[0]]['class2'][$value[1]])) { // 二级
  368. $tpl_data[$value[0]]['class2'][$value[1]]['gc_id'] = $gc_list[$value[0]]['class2'][$value[1]]['gc_id'];
  369. $tpl_data[$value[0]]['class2'][$value[1]]['gc_name'] = $gc_list[$value[0]]['class2'][$value[1]]['gc_name'];
  370. if (!empty($gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]])) { // 三级
  371. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_id'] = $gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_id'];
  372. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_name'] = $gc_list[$value[0]]['class2'][$value[1]]['class3'][$value[2]]['gc_name'];
  373. if (!$sign) { // 取得全部三级分类
  374. foreach ($gc_list[$value[0]]['class2'][$value[1]]['class3'] as $val) {
  375. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['gc_id'] = $val['gc_id'];
  376. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['gc_name'] = $val['gc_name'];
  377. if ($value[2] == $val['gc_id']) {
  378. $tpl_data[$value[0]]['class2'][$value[1]]['class3'][$val['gc_id']]['default'] = 1;
  379. }
  380. }
  381. }
  382. } else { // 取得全部二级分类
  383. if (!$sign) { // 取得同级分类
  384. if (!empty($gc_list[$value[0]]['class2'])) {
  385. foreach ($gc_list[$value[0]]['class2'] as $gc2) {
  386. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['gc_id'] = $gc2['gc_id'];
  387. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['gc_name'] = $gc2['gc_name'];
  388. if (!empty($gc2['class3'])) {
  389. foreach ($gc2['class3'] as $gc3) {
  390. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_id'] = $gc3['gc_id'];
  391. $tpl_data[$value[0]]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_name'] = $gc3['gc_name'];
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. } else { // 取得全部一级分类
  399. if (!$sign) { // 取得同级分类
  400. if (!empty($gc_list)) {
  401. foreach ($gc_list as $gc1) {
  402. $tpl_data[$gc1['gc_id']]['gc_id'] = $gc1['gc_id'];
  403. $tpl_data[$gc1['gc_id']]['gc_name'] = $gc1['gc_name'];
  404. if (!empty($gc1['class2'])) {
  405. foreach ($gc1['class2'] as $gc2) {
  406. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['gc_id'] = $gc2['gc_id'];
  407. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['gc_name'] = $gc2['gc_name'];
  408. if (!empty($gc2['class3'])) {
  409. foreach ($gc2['class3'] as $gc3) {
  410. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_id'] = $gc3['gc_id'];
  411. $tpl_data[$gc1['gc_id']]['class2'][$gc2['gc_id']]['class3'][$gc3['gc_id']]['gc_name'] = $gc3['gc_name'];
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. }
  422. return $tpl_data;
  423. }
  424. /**
  425. * 全文搜索
  426. * @return array 商品主键,搜索结果总数
  427. */
  428. public function indexerSearch($get = array(),$pagesize) {
  429. if (!C('fullindexer.open')) return array(null,0);
  430. $condition = array();
  431. //拼接条件
  432. if (intval($get['cate_id']) > 0) {
  433. $cate_id = intval($get['cate_id']);
  434. } elseif (intval($get['gc_id']) > 0) {
  435. $cate_id = intval($get['gc_id']);
  436. }
  437. if ($cate_id) {
  438. $goods_class = Model('goods_class')->getGoodsClassForCacheModel();
  439. $depth = $goods_class[$cate_id]['depth'];
  440. $cate_field = 'cate_'.$depth;
  441. $condition['cate']['key'] = $cate_field;
  442. $condition['cate']['value'] = $cate_id;
  443. }
  444. if ($get['keyword'] != '') {
  445. $condition['keyword'] = $get['keyword'];
  446. }
  447. if (intval($get['b_id']) > 0) {
  448. $condition['brand_id'] = intval($get['b_id']);
  449. }
  450. if (preg_match('/^[\d_]+$/',$get['a_id'])) {
  451. $attr_ids = explode('_',$get['a_id']);
  452. if (is_array($attr_ids)){
  453. foreach ($attr_ids as $v) {
  454. if (intval($v) > 0) {
  455. $condition['attr_id'][] = intval($v);
  456. }
  457. }
  458. }
  459. }
  460. if ($get['type'] == 1) {
  461. $condition['store_id'] = 1;
  462. }
  463. if (intval($get['area_id']) > 0) {
  464. $condition['area_id'] = intval($get['area_id']);
  465. }
  466. if ($get['gift'] == 1) {
  467. $condition['have_gift'] = 1;
  468. }
  469. //拼接排序(销量,浏览量,价格)
  470. $order = array();
  471. $order = array('store_id' => false,'goods_id' => false);
  472. if (in_array($get['key'],array('1','2','3'))) {
  473. $order = array(str_replace(array('1','2','3'), array('goods_salenum','goods_click','goods_price'), $get['key'])
  474. => $get['order'] == '1' ? true : false
  475. );
  476. }
  477. //取得商品主键等信息
  478. $result = $this->getIndexerList($condition,$order,$pagesize);
  479. if ($result !== false) {
  480. list($indexer_ids,$indexer_count) = $result;
  481. //如果全文搜索发生错误,后面会再执行数据库搜索
  482. } else {
  483. $indexer_ids = null;
  484. $indexer_count = 0;
  485. }
  486. return array($indexer_ids,$indexer_count);
  487. }
  488. }