| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- /*
- * FecShop file.
- *
- * @link http://www.fecshop.com/
- * @copyright Copyright (c) 2016 FecShop Software LLC
- * @license http://www.fecshop.com/license/
- */
- namespace fecshop\services;
- use fecshop\services\search\MongoSearch;
- use Yii;
- /**
- * Product Search.
- * @author Terry Zhao <2358269014@qq.com>
- * @since 1.0
- */
- class Search extends Service
- {
- /**
- * 在搜索页面侧栏的搜索过滤属性字段.
- */
- public $filterAttr;
- /**
- * 在搜索页面, spu相同的sku,是否只显示其中score高的sku,其他的sku隐藏
- * 如果设置为true,那么在搜索结果页面,spu相同,sku不同的产品,只会显示score最高的那个产品
- * 如果设置为false,那么在搜索结果页面,所有的sku都显示。
- * 这里做设置的好处,譬如服装,一个spu的不同颜色尺码可能几十个产品,都显示出来会占用很多的位置,对于这种产品您可以选择设置true
- * 这个针对的京东模式的产品
- */
- public $productSpuShowOnlyOneSku = true;
-
-
- // 当前有效的子services
- protected $_childServiceNames;
- public function init()
- {
- //if($this->currentSearchEngine == 'MongoSearch'){
- // $this->_searchEngine = new MongoSearch;
- //}else if($this->currentSearchEngine == 'XunSearch'){
- // $this->_searchEngine = new XunSearch;
- //}
- parent::init();
- }
- /**
- * init search engine index.
- */
- protected function actionInitFullSearchIndex()
- {
- //exit;
- $searchEngineList = $this->getAllChildServiceName();
- if (is_array($searchEngineList) && !empty($searchEngineList)) {
- foreach ($searchEngineList as $sE) {
- $service = $this->{$sE};
- $service->initFullSearchIndex();
- }
- }
- }
-
-
-
- public function getAllChildServiceName()
- {
- if (!$this->_childServiceNames) {
- $childServiceName = parent::getAllChildServiceName();
- // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
- $searchConfig = Yii::$app->store->get('search_engine');
- $enableSearch = [];
- if (is_array($searchConfig)) {
- foreach ($searchConfig as $engine => $status) {
- if ($status == Yii::$app->store->enable) {
- $enableSearch[] = $engine;
- }
- }
- }
- // 当前的子search service,查看哪些是enable的
- if (is_array($childServiceName) && !empty($childServiceName)) {
- foreach ($childServiceName as $sE) {
- if (in_array($sE, $enableSearch)) {
- $this->_childServiceNames[] = $sE;
- }
- }
- }
- }
- return $this->_childServiceNames;
- }
- /**
- * @param $product_ids | Array 产品id数组
- * 批量处理,将所有产品批量同步到搜索工具的库里面。
- */
- protected function actionSyncProductInfo($product_ids, $numPerPage = 20)
- {
- $searchEngineList = $this->getAllChildServiceName();
- if (is_array($searchEngineList) && !empty($searchEngineList)) {
- foreach ($searchEngineList as $sE) {
- $service = $this->{$sE};
- $service->syncProductInfo($product_ids, $numPerPage);
- }
- }
- }
- /**
- * @param $nowTimeStamp | int
- * 批量更新过程中,被更新的产品都会更新字段sync_updated_at
- * 删除xunSearch引擎中sync_updated_at小于$nowTimeStamp的字段.
- */
- protected function actionDeleteNotActiveProduct($nowTimeStamp)
- {
- $searchEngineList = $this->getAllChildServiceName();
- if (is_array($searchEngineList) && !empty($searchEngineList)) {
- foreach ($searchEngineList as $sE) {
- $service = $this->{$sE};
- $service->deleteNotActiveProduct($nowTimeStamp);
- }
- }
- }
- /**
- * @param $select | Array
- * @param $where | Array
- * @param $pageNum | Int
- * @param $numPerPage | Array
- * @param $product_search_max_count | Int , 搜索结果最大产品数。
- * 对于上面的参数和以前的$filter类似,大致和下面的类似
- * [
- * 'category_id' => 1,
- * 'pageNum' => 2,
- * 'numPerPage' => 50,
- * 'orderBy' => 'name',
- * 'where' => [
- * ['>','price',11],
- * ['<','price',22],
- * ],
- * 'select' => ['xx','yy'],
- * 'group' => '$spu',
- * ]
- * 得到搜索的产品列表.
- */
- protected function actionGetSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count, $filterAttr = [])
- {
- // 当前有效的搜索引擎
- $searchEngineList = $this->getAllChildServiceName();
- // 语言对应
- $currentSearchEngine = $this->getCurrentSearchEngine();
- if (in_array($currentSearchEngine, $searchEngineList)) {
- $service = $this->{$currentSearchEngine};
- return $service->getSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count, $filterAttr);
- }
- }
- /**
- * 得到搜索的sku列表侧栏的过滤.
- * @param $filter_attr | Array
- * @param $where | Array , like
- * [
- * ['>','price',11],
- * ['<','price',22],
- * ],
- */
- protected function actionGetFrontSearchFilter($filter_attr, $where)
- {
-
- // 当前有效的搜索引擎
- $searchEngineList = $this->getAllChildServiceName();
- // 语言对应
- $currentSearchEngine = $this->getCurrentSearchEngine();
- if (in_array($currentSearchEngine, $searchEngineList)) {
- $service = $this->{$currentSearchEngine};
- return $service->getFrontSearchFilter($filter_attr, $where);
- }
- }
-
- protected $_currentSearchEngine;
- // 得到当前语言对应的搜索引擎
- public function getCurrentSearchEngine()
- {
- if (!$this->_currentSearchEngine) {
- $currentLangCode = Yii::$service->store->currentLangCode;
- if (!$currentLangCode) {
- return null;
- }
- $langArr = Yii::$app->store->get('mutil_lang');
- $langCodeAndEngine = [];
- foreach ($langArr as $one) {
- $langCodeAndEngine[$one['lang_code']] = $one['search_engine'];
- }
- if (!isset($langCodeAndEngine[$currentLangCode])) {
- return null;
- }
- $this->_currentSearchEngine = $langCodeAndEngine[$currentLangCode];
- }
-
- return $this->_currentSearchEngine;
-
- }
- /**
- * 通过product_id删除搜索数据.
- * @param $product_id | \mongoId
- */
- protected function actionRemoveByProductId($product_id)
- {
- $searchEngineList = $this->getAllChildServiceName();
- if (is_array($searchEngineList) && !empty($searchEngineList)) {
- foreach ($searchEngineList as $sE) {
- $service = $this->{$sE};
- $service->removeByProductId($product_id);
- }
- }
- }
- }
|