1、fullTearchText方法搜索到_id后,然后通过产品信息表去产品数据,是这样的么?
2、但是搜索出来的_id和产品的_id不同,导致没有获取到产品信息,是什么情况引起的
protected function fullTearchText($filter)
{
$sModel = $this->_searchModel;
$where = $filter['where'];
$product_search_max_count = $filter['product_search_max_count'] ? $filter['product_search_max_count'] : 1000;
$select = $filter['select'];
$pageNum = $filter['pageNum'];
$numPerPage = $filter['numPerPage'];
$orderBy = $filter['orderBy'];
//
/*
* 说明:1.'search_score'=>['$meta'=>"textScore" ,这个是text搜索为了排序,
* 详细参看:https://docs.mongodb.com/manual/core/text-search-operators/
* 2. sort排序:search_score是全文搜索匹配后的得分,score是product表的一个字段,这个字段可以通过销售量或者其他作为参考设置。
*/
$sModel::$_lang = Yii::$service->store->currentLangCode;
//$search_data = $this->_searchModel->getCollection();
//$mongodb = Yii::$app->mongodb;
//$search_data = $mongodb->getCollection('full_search_product_en')
// $search_data = $this->_searchModel->getCollection()->find();
// var_dump($search_data);die;
$search_data = $this->_searchModel->getCollection()->find(
$where,
['search_score' => ['$meta' => 'textScore'], 'id' => 1, 'spu' => 1, 'score' => 1],
[
'sort' => ['search_score' => ['$meta' => 'textScore'], 'score' => -1],
'limit' => $product_search_max_count,
]
);
/**
* 通过下面的数组,在spu相同的多个sku产品,只显示一个,因为上面已经排序,
* 因此,spu相同的sku产品,显示的是score最高的一个。
*/
$data = [];
foreach ($search_data as $one) {
if (!isset($data[$one['spu']])) {
$data[$one['spu']] = $one;
}
}
$count = count($data);
$offset = ($pageNum - 1) * $numPerPage;
$limit = $numPerPage;
$productIds = [];
foreach ($data as $d) {
$productIds[] = $d['_id'];
}
$productIds = array_slice($productIds, $offset, $limit);
if (!empty($productIds)) {
$query = $this->_productModel->find()->asArray()
->select($select)
->where(['_id' => ['$in' => $productIds]]);
$data = $query->all();
/**
* 下面的代码的作用:将结果按照上面in查询的顺序进行数组的排序,使结果和上面的搜索结果排序一致(_id)。
*/
$s_data = [];
foreach ($data as $one) {
$_id = (string)$one['_id'];
$s_data[$_id] = $one;
}
$return_data = [];
foreach ($productIds as $product_id) {
$return_data[] = $s_data[(string)$product_id];
}
var_dump($productIds);
var_dump($return_data);die;
return [
'coll' => $return_data,
'count' => $count,
];
}
}