Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
yii2_fecshop
提交
2d88bb68
Y
yii2_fecshop
项目概览
OpenDocCN
/
yii2_fecshop
通知
17
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yii2_fecshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2d88bb68
编写于
7月 22, 2019
作者:
T
Terry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Search service 后台配置读取配置,指定数据库model
上级
75ac0a7d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
157 addition
and
72 deletion
+157
-72
services/Search.php
services/Search.php
+69
-34
services/search/MongoSearch.php
services/search/MongoSearch.php
+49
-25
services/search/MysqlSearch.php
services/search/MysqlSearch.php
+21
-9
services/search/XunSearch.php
services/search/XunSearch.php
+18
-4
未找到文件。
services/Search.php
浏览文件 @
2d88bb68
...
...
@@ -32,6 +32,10 @@ class Search extends Service
* 这个针对的京东模式的产品
*/
public
$productSpuShowOnlyOneSku
=
true
;
// 当前有效的子services
protected
$_childServiceNames
;
public
function
init
()
{
...
...
@@ -57,6 +61,34 @@ class Search extends Service
}
}
}
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数组
...
...
@@ -112,26 +144,15 @@ class Search extends Service
*/
protected
function
actionGetSearchProductColl
(
$select
,
$where
,
$pageNum
,
$numPerPage
,
$product_search_max_count
,
$filterAttr
=
[])
{
$currentLangCode
=
Yii
::
$service
->
store
->
currentLangCode
;
if
(
!
$currentLangCode
)
{
Yii
::
$service
->
helper
->
errors
->
add
(
'current language code is empty'
);
return
;
}
// 当前有效的搜索引擎
$searchEngineList
=
$this
->
getAllChildServiceName
();
// 根据当前store的语言,选择相应的搜索引擎
if
(
is_array
(
$searchEngineList
)
&&
!
empty
(
$searchEngineList
))
{
foreach
(
$searchEngineList
as
$sE
)
{
$service
=
$this
->
{
$sE
};
$searchLang
=
$service
->
searchLang
;
if
(
is_array
(
$searchLang
)
&&
!
empty
(
$searchLang
))
{
$searchLangCode
=
array_keys
(
$searchLang
);
// 如果当前store的语言,在当前的搜索引擎中支持,则会使用这个搜索,作为支持。
if
(
in_array
(
$currentLangCode
,
$searchLangCode
))
{
return
$service
->
getSearchProductColl
(
$select
,
$where
,
$pageNum
,
$numPerPage
,
$product_search_max_count
,
$filterAttr
);
}
}
}
// 语言对应
$currentSearchEngine
=
$this
->
getCurrentSearchEngine
();
if
(
in_array
(
$currentSearchEngine
,
$searchEngineList
))
{
$service
=
$this
->
{
$currentSearchEngine
};
return
$service
->
getSearchProductColl
(
$select
,
$where
,
$pageNum
,
$numPerPage
,
$product_search_max_count
,
$filterAttr
);
}
}
/**
...
...
@@ -145,25 +166,39 @@ class Search extends Service
*/
protected
function
actionGetFrontSearchFilter
(
$filter_attr
,
$where
)
{
$currentLangCode
=
Yii
::
$service
->
store
->
currentLangCode
;
if
(
!
$currentLangCode
)
{
return
;
}
// 当前有效的搜索引擎
$searchEngineList
=
$this
->
getAllChildServiceName
();
if
(
is_array
(
$searchEngineList
)
&&
!
empty
(
$searchEngineList
))
{
foreach
(
$searchEngineList
as
$sE
)
{
$service
=
$this
->
{
$sE
};
$searchLang
=
$service
->
searchLang
;
if
(
is_array
(
$searchLang
)
&&
!
empty
(
$searchLang
))
{
$searchLangCode
=
array_keys
(
$searchLang
);
// 如果当前store的语言,在当前的搜索引擎中支持,则会使用这个搜索,作为支持。
if
(
in_array
(
$currentLangCode
,
$searchLangCode
))
{
return
$service
->
getFrontSearchFilter
(
$filter_attr
,
$where
);
}
}
// 语言对应
$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
;
}
/**
...
...
services/search/MongoSearch.php
浏览文件 @
2d88bb68
...
...
@@ -24,9 +24,28 @@ class MongoSearch extends Service implements SearchInterface
{
public
$searchIndexConfig
;
public
$searchLang
;
//
public $searchLang;
public
$enable
;
// https://docs.mongodb.com/manual/reference/text-search-languages/#text-search-languages
public
$searchLanguages
=
[
'da'
=>
'danish'
,
'nl'
=>
'dutch'
,
'en'
=>
'english'
,
'fi'
=>
'finnish'
,
'fr'
=>
'french'
,
'de'
=>
'german'
,
'hu'
=>
'hungarian'
,
'it'
=>
'italian'
,
'nb'
=>
'norwegian'
,
'pt'
=>
'portuguese'
,
'ro'
=>
'romanian'
,
'ru'
=>
'russian'
,
'es'
=>
'spanish'
,
'sv'
=>
'swedish'
,
'tr'
=>
'turkish'
,
];
//protected $_productModelName = '\fecshop\models\mongodb\Product';
...
...
@@ -54,6 +73,23 @@ class MongoSearch extends Service implements SearchInterface
$sModel
::
$_filterColumns
=
$filterAttr
;
}
}
protected
$_searchLang
;
protected
function
getActiveLangConfig
()
{
if
(
!
$this
->
_searchLang
)
{
$langArr
=
Yii
::
$app
->
store
->
get
(
'mutil_lang'
);
foreach
(
$langArr
as
$one
)
{
if
(
$one
[
'search_engine'
]
==
'mongoSearch'
)
{
$langCode
=
$one
[
'lang_code'
];
$langName
=
isset
(
$this
->
searchLanguages
[
$langCode
])
?
$this
->
searchLanguages
[
$langCode
]
:
$this
->
searchLanguages
[
'en'
];
$this
->
_searchLang
[
$langCode
]
=
$langName
;
}
}
}
return
$this
->
_searchLang
;
}
/**
* 创建索引.
...
...
@@ -72,8 +108,9 @@ class MongoSearch extends Service implements SearchInterface
}
//$langCodes = Yii::$service->fecshoplang->allLangCode;
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLang
=
$this
->
getActiveLangConfig
();
if
(
!
empty
(
$searchLang
)
&&
is_array
(
$searchLang
))
{
foreach
(
$searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
/*
* 如果语言不存在,譬如中文,mongodb的fullSearch是不支持中文的,
* 这种情况是不能搜索的。
...
...
@@ -88,22 +125,6 @@ class MongoSearch extends Service implements SearchInterface
}
}
}
/*
$searchModel->getCollection()->ensureIndex(
[
'name' => 'text',
'description' => 'text',
],
[
'weights' => [
'name' => 10,
'description' => 5,
],
'default_language'=>$store,
]
);
*/
}
//
protected
function
getProductSelectData
()
...
...
@@ -174,8 +195,9 @@ class MongoSearch extends Service implements SearchInterface
$one_name
=
$one
[
'name'
];
$one_description
=
$one
[
'description'
];
$one_short_description
=
$one
[
'short_description'
];
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLang
=
$this
->
getActiveLangConfig
();
if
(
!
empty
(
$searchLang
)
&&
is_array
(
$searchLang
))
{
foreach
(
$searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$sModel
::
$_lang
=
$langCode
;
$searchModel
=
$this
->
_searchModel
->
findOne
([
'product_id'
=>
$one
[
'product_id'
]]);
...
...
@@ -214,8 +236,9 @@ class MongoSearch extends Service implements SearchInterface
//$langCodes = Yii::$service->fecshoplang->allLangCode;
//if(!empty($langCodes) && is_array($langCodes)){
// foreach($langCodes as $langCodeInfo){
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLang
=
$this
->
getActiveLangConfig
();
if
(
!
empty
(
$searchLang
)
&&
is_array
(
$searchLang
))
{
foreach
(
$searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$sModel
::
$_lang
=
$langCode
;
// 更新时间方式删除。
$this
->
_searchModel
->
deleteAll
([
...
...
@@ -234,8 +257,9 @@ class MongoSearch extends Service implements SearchInterface
protected
function
actionRemoveByProductId
(
$product_id
)
{
$sModel
=
$this
->
_searchModel
;
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLang
=
$this
->
getActiveLangConfig
();
if
(
!
empty
(
$searchLang
)
&&
is_array
(
$searchLang
))
{
foreach
(
$searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$sModel
::
$_lang
=
$langCode
;
$this
->
_searchModel
->
deleteAll
([
'_id'
=>
$product_id
,
...
...
services/search/MysqlSearch.php
浏览文件 @
2d88bb68
...
...
@@ -24,7 +24,7 @@ class MysqlSearch extends Service implements SearchInterface
{
public
$searchIndexConfig
;
public
$searchLang
;
//
public $searchLang;
public
$enable
;
...
...
@@ -78,7 +78,20 @@ class MysqlSearch extends Service implements SearchInterface
];
}
protected
$_searchLangCode
;
// 从配置中得到当前的搜索引擎对应的有效语言。
protected
function
getActiveLangCode
()
{
if
(
!
$this
->
_searchLangCode
)
{
$langArr
=
Yii
::
$app
->
store
->
get
(
'mutil_lang'
);
foreach
(
$langArr
as
$one
)
{
if
(
$one
[
'search_engine'
]
==
'xunSearch'
)
{
$this
->
_searchLangCode
[]
=
$one
[
'lang_code'
];
}
}
}
return
$this
->
_searchLangCode
;
}
/**
* @param $product_ids | Array ,里面的子项是MongoId类型。
* 将产品表的数据同步到各个语言对应的搜索表中。
...
...
@@ -120,8 +133,9 @@ class MysqlSearch extends Service implements SearchInterface
$one_name
=
$one
[
'name'
];
$one_description
=
$one
[
'description'
];
$one_short_description
=
$one
[
'short_description'
];
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLangCode
=
$this
->
getActiveLangCode
();
if
(
!
empty
(
$searchLangCode
)
&&
is_array
(
$searchLangCode
))
{
foreach
(
$searchLangCode
as
$langCode
)
{
$one
[
'lang'
]
=
$langCode
;
$one
[
'image'
]
=
serialize
(
$one
[
'image'
]);
...
...
@@ -162,11 +176,9 @@ class MysqlSearch extends Service implements SearchInterface
{
$sModel
=
$this
->
_searchModel
;
echo
"begin delete Mongodb Search Date
\n
"
;
//$langCodes = Yii::$service->fecshoplang->allLangCode;
//if(!empty($langCodes) && is_array($langCodes)){
// foreach($langCodes as $langCodeInfo){
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$this
->
searchLang
as
$langCode
=>
$mongoSearchLangName
)
{
$searchLangCode
=
$this
->
getActiveLangCode
();
if
(
!
empty
(
$searchLangCode
)
&&
is_array
(
$searchLangCode
))
{
foreach
(
$searchLangCode
as
$langCode
)
{
//$sModel::$_lang = $langCode;
// 更新时间方式删除。
$this
->
_searchModel
->
deleteAll
([
...
...
services/search/XunSearch.php
浏览文件 @
2d88bb68
...
...
@@ -24,7 +24,7 @@ class XunSearch extends Service implements SearchInterface
{
public
$searchIndexConfig
;
public
$searchLang
;
//
public $searchLang;
public
$fuzzy
=
false
;
...
...
@@ -77,6 +77,20 @@ class XunSearch extends Service implements SearchInterface
];
}
protected
$_searchLangCode
;
protected
function
getActiveLangCode
()
{
if
(
!
$this
->
_searchLangCode
)
{
$langArr
=
Yii
::
$app
->
store
->
get
(
'mutil_lang'
);
foreach
(
$langArr
as
$one
)
{
if
(
$one
[
'search_engine'
]
==
'xunSearch'
)
{
$this
->
_searchLangCode
[]
=
$one
[
'lang_code'
];
}
}
}
return
$this
->
_searchLangCode
;
}
/**
* 将产品信息同步到xunSearch引擎中.
*/
...
...
@@ -111,9 +125,9 @@ class XunSearch extends Service implements SearchInterface
$one_name
=
$one
[
'name'
];
$one_description
=
$one
[
'description'
];
$one_short_description
=
$one
[
'short_description'
];
if
(
!
empty
(
$this
->
searchLang
)
&&
is_array
(
$this
->
searchLang
))
{
foreach
(
$
this
->
searchLang
as
$langCode
=>
$langNam
e
)
{
$searchLangCode
=
$this
->
getActiveLangCode
();
if
(
!
empty
(
$searchLangCode
)
&&
is_array
(
$searchLangCode
))
{
foreach
(
$
searchLangCode
as
$langCod
e
)
{
//echo $langCode;
$xunSearchModel
=
new
$this
->
_searchModelName
();
$xunSearchModel
->
_id
=
(
string
)
$one
[
$productPrimaryKey
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录