提交 ec664a54 编写于 作者: T Terry

搜索功能:新增sku精准搜索

上级 efbe92a1
...@@ -219,17 +219,26 @@ class MongoSearch extends Service implements SearchInterface ...@@ -219,17 +219,26 @@ class MongoSearch extends Service implements SearchInterface
*/ */
protected function actionGetSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count) protected function actionGetSearchProductColl($select, $where, $pageNum, $numPerPage, $product_search_max_count)
{ {
$filter = [ // 先进行sku搜索,如果有结果,说明是针对sku的搜索
'pageNum' => $pageNum,
'numPerPage' => $numPerPage, $searchText = $where['$text']['$search'];
'where' => $where, $productM = Yii::$service->product->getBySku($searchText);
'product_search_max_count' => $product_search_max_count, if ($productM) {
'select' => $select, $collection['coll'][] = $productM;
]; $collection['count'] = 1;
//var_dump($filter);exit; } else {
$collection = $this->fullTearchText($filter); $filter = [
'pageNum' => $pageNum,
'numPerPage' => $numPerPage,
'where' => $where,
'product_search_max_count' => $product_search_max_count,
'select' => $select,
];
//var_dump($filter);exit;
$collection = $this->fullTearchText($filter);
}
$collection['coll'] = Yii::$service->category->product->convertToCategoryInfo($collection['coll']); $collection['coll'] = Yii::$service->category->product->convertToCategoryInfo($collection['coll']);
//var_dump($collection);
return $collection; return $collection;
} }
......
...@@ -132,44 +132,52 @@ class XunSearch extends Service implements SearchInterface ...@@ -132,44 +132,52 @@ class XunSearch extends Service implements SearchInterface
protected function fullTearchText($select, $where, $pageNum, $numPerPage, $product_search_max_count) protected function fullTearchText($select, $where, $pageNum, $numPerPage, $product_search_max_count)
{ {
$XunSearchQuery = $this->_searchModel->find()->asArray();
$XunSearchQuery->fuzzy($this->fuzzy); $searchText = $where['$text']['$search'];
$XunSearchQuery->synonyms($this->synonyms); $productM = Yii::$service->product->getBySku($searchText);
$productIds = [];
if (is_array($where) && !empty($where)) { if ($productM) {
if (isset($where['$text']['$search']) && $where['$text']['$search']) { $productIds[] = $productM['_id'];
$XunSearchQuery->where($where['$text']['$search']); } else {
} else { $XunSearchQuery = $this->_searchModel->find()->asArray();
return []; $XunSearchQuery->fuzzy($this->fuzzy);
$XunSearchQuery->synonyms($this->synonyms);
if (is_array($where) && !empty($where)) {
if (isset($where['$text']['$search']) && $where['$text']['$search']) {
$XunSearchQuery->where($where['$text']['$search']);
} else {
return [];
}
foreach ($where as $k => $v) {
if ($k != '$text') {
$XunSearchQuery->andWhere([$k => $v]);
}
}
} }
foreach ($where as $k => $v) { $XunSearchQuery->orderBy(['score' => SORT_DESC]);
if ($k != '$text') { $XunSearchQuery->limit($product_search_max_count);
$XunSearchQuery->andWhere([$k => $v]); $XunSearchQuery->offset(0);
$search_data = $XunSearchQuery->all();
$data = [];
foreach ($search_data as $one) {
if (!isset($data[$one['spu']])) {
$data[$one['spu']] = $one;
} }
} }
}
$XunSearchQuery->orderBy(['score' => SORT_DESC]); $count = count($data);
$XunSearchQuery->limit($product_search_max_count); $offset = ($pageNum - 1) * $numPerPage;
$XunSearchQuery->offset(0); $limit = $numPerPage;
$search_data = $XunSearchQuery->all(); $productIds = [];
foreach ($data as $d) {
$data = []; $productIds[] = new \MongoDB\BSON\ObjectId($d['_id']);
foreach ($search_data as $one) {
if (!isset($data[$one['spu']])) {
$data[$one['spu']] = $one;
} }
}
$count = count($data); $productIds = array_slice($productIds, $offset, $limit);
$offset = ($pageNum - 1) * $numPerPage;
$limit = $numPerPage;
$productIds = [];
foreach ($data as $d) {
$productIds[] = new \MongoDB\BSON\ObjectId($d['_id']);
} }
$productIds = array_slice($productIds, $offset, $limit);
if (!empty($productIds)) { if (!empty($productIds)) {
$query = $this->_productModel->find()->asArray() $query = $this->_productModel->find()->asArray()
->select($select) ->select($select)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册