解决xunsearch热门搜索,不按照数量排序问题

public function getHotQuery($limit = 6, $type = 'total')
{
    $ret = array();
    $limit = max(1, min(50, intval($limit)));
    $this->xs->setScheme(XSFieldScheme::logger());
    try {
        $this->setDb(self::LOG_DB)->setLimit($limit);
        if ($type !== 'lastnum' && $type !== 'currnum') {
            $type = 'total';
        }
        // 加上下面这一句就OK了
        $this->setSort('total');
        $result = $this->search($type . ':1');
        foreach ($result as $doc) /* @var $doc XSDocument */ {
            $body = $doc->body;
            $ret[$body] = $doc->f($type);
        }
        $this->restoreDb();
    } catch (XSException $e) {
        if ($e->getCode() != XS_CMD_ERR_XAPIAN) {
            throw $e;
        }
    }
    $this->xs->restoreScheme();
    return $ret;
}
$this->setSort('total');

不正确的

{
    "errno": 0,
    "errdesc": "操作成功",
    "timestamp": 1576057980,
    "data": {
        "项目测试": "2",
        "俗话说": "1",
        "测试": "12",
        "项目": "1"
    }
}

正确的

{
    "errno": 0,
    "errdesc": "操作成功",
    "timestamp": 1576057928,
    "data": {
        "测试": "12",
        "项目测试": "2",
        "俗话说": "1",
        "项目": "1"
    }
}
posted @ 2019-12-11 17:53  TBHacker  阅读(448)  评论(0编辑  收藏  举报