Skip to content

Commit 26592a4

Browse files
mysql search services
1 parent 535f58b commit 26592a4

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

services/search/MysqlSearch.php

+22-56
Original file line numberDiff line numberDiff line change
@@ -299,49 +299,32 @@ protected function fullTearchText($filter)
299299
$pageNum = $filter['pageNum'];
300300
$numPerPage = $filter['numPerPage'];
301301
$orderBy = $filter['orderBy'];
302-
303-
$searchM = $this->_searchModel->find()->asArray()
304-
->where($whereArr)
305-
306-
;
307-
if ($orderBy) {
308-
$searchM->orderBy($orderBy);
309-
}
310-
311-
$search_data = $searchM->limit($numPerPage)->offset(($pageNum-1)*$numPerPage)->all();
312-
//var_dump($search_data);exit;
313-
314-
//var_dump($search_data);exit;
315-
/**
316-
* 在搜索页面, spu相同的sku,是否只显示其中score高的sku,其他的sku隐藏
317-
* 如果设置为true,那么在搜索结果页面,spu相同,sku不同的产品,只会显示score最高的那个产品
318-
* 如果设置为false,那么在搜索结果页面,所有的sku都显示。
319-
* 这里做设置的好处,譬如服装,一个spu的不同颜色尺码可能几十个产品,都显示出来会占用很多的位置,对于这种产品您可以选择设置true
320-
* 这个针对的京东模式的产品
321-
*/
322-
$data = [];
302+
$count = 0;
303+
$searchM = $this->_searchModel->find()->asArray()->where($whereArr);
323304
if (Yii::$service->search->productSpuShowOnlyOneSku) {
324-
foreach ($search_data as $one) {
325-
if (!isset($data[$one['spu']])) {
326-
$data[$one['spu']] = $one;
327-
}
328-
}
305+
/**
306+
* 如果产品spu存在多个sku(譬如同一款产品存在多个颜色尺码),但是分类页只显示一个sku,那么需要通过
307+
* 下面的逻辑,对spu进行group,对score倒序,取score最大的那个sku作为分类列表显示
308+
*/
309+
$orderBy['score'] = SORT_DESC;
310+
$query = $searchM->orderBy($orderBy)->groupBy('spu')->limit($numPerPage)->offset(($pageNum-1)*$numPerPage);
311+
$search_data = $query->all();
312+
$count = $query->limit(null)->offset(null)->count();
329313
} else {
330-
$data = $search_data;
331-
}
332-
333-
$count = count($data);
334-
$offset = ($pageNum - 1) * $numPerPage;
335-
$limit = $numPerPage;
314+
if ($orderBy) {
315+
$searchM->orderBy($orderBy);
316+
}
317+
$query = $searchM->limit($numPerPage)->offset(($pageNum-1)*$numPerPage);
318+
$search_data = $query->all();
319+
$count = $query->limit(null)->offset(null)->count();
320+
}
336321
$productIds = [];
337-
foreach ($data as $d) {
322+
foreach ($search_data as $d) {
338323
$productIds[] = $d['product_id'];
339324
}
340-
341-
$productIds = array_slice($productIds, $offset, $limit);
325+
// 通过productIds数组 得到产品数据
342326
$productPrimaryKey = Yii::$service->product->getPrimaryKey();
343327
if (!empty($productIds)) {
344-
//
345328
foreach ($select as $sk => $se) {
346329
if ($se == 'product_id') {
347330
unset($select[$sk]);
@@ -355,31 +338,14 @@ protected function fullTearchText($filter)
355338
],
356339
];
357340
$collData = Yii::$service->product->coll($filter);
358-
$data = $collData['coll'];
359-
/**
360-
* 下面的代码的作用:将结果按照上面in查询的顺序进行数组的排序,使结果和上面的搜索结果排序一致(_id)。
361-
*/
362-
//var_dump($data);exit;
363-
$s_data = [];
364-
foreach ($data as $one) {
365-
if ($one[$productPrimaryKey]) {
366-
$_id = (string) $one[$productPrimaryKey];
367-
$s_data[$_id] = $one;
368-
}
369-
}
370-
$return_data = [];
371-
foreach ($productIds as $product_id) {
372-
$pid = (string) $product_id;
373-
if (isset($s_data[$pid]) && $s_data[$pid]) {
374-
$return_data[] = $s_data[$pid];
375-
}
376-
}
377-
341+
$return_data = $collData['coll'];
378342
return [
379343
'coll' => $return_data,
380344
'count'=> $count,
381345
];
382346
}
347+
348+
return [];
383349
}
384350

385351
/**

0 commit comments

Comments
 (0)