Search.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. use fecshop\services\search\MongoSearch;
  11. use Yii;
  12. /**
  13. * Product Search.
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Search extends Service
  18. {
  19. /**
  20. * 在搜索页面侧栏的搜索过滤属性字段.
  21. */
  22. public $filterAttr;
  23. /**
  24. * 在搜索页面, spu相同的sku,是否只显示其中score高的sku,其他的sku隐藏
  25. * 如果设置为true,那么在搜索结果页面,spu相同,sku不同的产品,只会显示score最高的那个产品
  26. * 如果设置为false,那么在搜索结果页面,所有的sku都显示。
  27. * 这里做设置的好处,譬如服装,一个spu的不同颜色尺码可能几十个产品,都显示出来会占用很多的位置,对于这种产品您可以选择设置true
  28. * 这个针对的京东模式的产品
  29. */
  30. public $productSpuShowOnlyOneSku = true;
  31. public function init()
  32. {
  33. //if($this->currentSearchEngine == 'MongoSearch'){
  34. // $this->_searchEngine = new MongoSearch;
  35. //}else if($this->currentSearchEngine == 'XunSearch'){
  36. // $this->_searchEngine = new XunSearch;
  37. //}
  38. parent::init();
  39. }
  40. /**
  41. * init search engine index.
  42. */
  43. protected function actionInitFullSearchIndex()
  44. {
  45. //exit;
  46. $searchEngineList = $this->getAllChildServiceName();
  47. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  48. foreach ($searchEngineList as $sE) {
  49. $service = $this->{$sE};
  50. $service->initFullSearchIndex();
  51. }
  52. }
  53. }
  54. /**
  55. * @param $product_ids | Array 产品id数组
  56. * 批量处理,将所有产品批量同步到搜索工具的库里面。
  57. */
  58. protected function actionSyncProductInfo($product_ids, $numPerPage = 20)
  59. {
  60. $searchEngineList = $this->getAllChildServiceName();
  61. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  62. foreach ($searchEngineList as $sE) {
  63. $service = $this->{$sE};
  64. $service->syncProductInfo($product_ids, $numPerPage);
  65. }
  66. }
  67. }
  68. /**
  69. * @param $nowTimeStamp | int
  70. * 批量更新过程中,被更新的产品都会更新字段sync_updated_at
  71. * 删除xunSearch引擎中sync_updated_at小于$nowTimeStamp的字段.
  72. */
  73. protected function actionDeleteNotActiveProduct($nowTimeStamp)
  74. {
  75. $searchEngineList = $this->getAllChildServiceName();
  76. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  77. foreach ($searchEngineList as $sE) {
  78. $service = $this->{$sE};
  79. $service->deleteNotActiveProduct($nowTimeStamp);
  80. }
  81. }
  82. }
  83. /**
  84. * @param $select | Array
  85. * @param $where | Array
  86. * @param $pageNum | Int
  87. * @param $numPerPage | Array
  88. * @param $product_search_max_count | Int , 搜索结果最大产品数。
  89. * 对于上面的参数和以前的$filter类似,大致和下面的类似
  90. * [
  91. * 'category_id' => 1,
  92. * 'pageNum' => 2,
  93. * 'numPerPage' => 50,
  94. * 'orderBy' => 'name',
  95. * 'where' => [
  96. * ['>','price',11],
  97. * ['<','price',22],
  98. * ],
  99. * 'select' => ['xx','yy'],
  100. * 'group' => '$spu',
  101. * ]
  102. * 得到搜索的产品列表.
  103. */
  104. protected function actionGetSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count, $filterAttr = [])
  105. {
  106. $currentLangCode = Yii::$service->store->currentLangCode;
  107. if (!$currentLangCode) {
  108. Yii::$service->helper->errors->add('current language code is empty');
  109. return;
  110. }
  111. $searchEngineList = $this->getAllChildServiceName();
  112. // 根据当前store的语言,选择相应的搜索引擎
  113. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  114. foreach ($searchEngineList as $sE) {
  115. $service = $this->{$sE};
  116. $searchLang = $service->searchLang;
  117. if (is_array($searchLang) && !empty($searchLang)) {
  118. $searchLangCode = array_keys($searchLang);
  119. // 如果当前store的语言,在当前的搜索引擎中支持,则会使用这个搜索,作为支持。
  120. if (in_array($currentLangCode, $searchLangCode)) {
  121. return $service->getSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count, $filterAttr);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * 得到搜索的sku列表侧栏的过滤.
  129. * @param $filter_attr | Array
  130. * @param $where | Array , like
  131. * [
  132. * ['>','price',11],
  133. * ['<','price',22],
  134. * ],
  135. */
  136. protected function actionGetFrontSearchFilter($filter_attr, $where)
  137. {
  138. $currentLangCode = Yii::$service->store->currentLangCode;
  139. if (!$currentLangCode) {
  140. return;
  141. }
  142. $searchEngineList = $this->getAllChildServiceName();
  143. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  144. foreach ($searchEngineList as $sE) {
  145. $service = $this->{$sE};
  146. $searchLang = $service->searchLang;
  147. if (is_array($searchLang) && !empty($searchLang)) {
  148. $searchLangCode = array_keys($searchLang);
  149. // 如果当前store的语言,在当前的搜索引擎中支持,则会使用这个搜索,作为支持。
  150. if (in_array($currentLangCode, $searchLangCode)) {
  151. return $service->getFrontSearchFilter($filter_attr, $where);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * 通过product_id删除搜索数据.
  159. * @param $product_id | \mongoId
  160. */
  161. protected function actionRemoveByProductId($product_id)
  162. {
  163. $searchEngineList = $this->getAllChildServiceName();
  164. if (is_array($searchEngineList) && !empty($searchEngineList)) {
  165. foreach ($searchEngineList as $sE) {
  166. $service = $this->{$sE};
  167. $service->removeByProductId($product_id);
  168. }
  169. }
  170. }
  171. }