搜索原理

文档问题 · fecshoper · 于 6年前 发布 · 1897 次阅读

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,
            ];
        }
    }
共收到 2 条回复
1楼 已删除.
Fecmall#26年前 0 个赞

稍等,我看一下

Fecmall#36年前 0 个赞

1.你把你的搜索表里面的数据清空

2.按照 https://github.com/fecshop/yii2_fecshop/commit/ccbfabd543d3195291c15d54ce74ada63485db64 修改一下相应文件,然后重新跑一次搜索脚本,试试。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics