开发者社区> 问答> 正文

postgresql RUM计算相似度问题

数据库存储为tsvector字段类型,
insert into rum_test(content) values(to_tsvector('simple', '11320272,11254479,11122893,11122893,11188686'));
插入结果:
'11122893':3,4 '11188686':5 '11254479':2 '11320272':1

现在,我用相同的数据去查询,并按相似度排序,结果是这样?不是100%?
select id, content <=> to_tsquery('11320272,11254479,11122893,11122893,11188686') as rank from rum_test
结果:
{"id":10,"rank":3.28987}

展开
收起
摇头 2017-02-22 17:59:59 4068 0
1 条回答
写回答
取消 提交回答
  • 公益是一辈子的事, I am digoal, just do it. 阿里云数据库团队, 擅长PolarDB, PostgreSQL, DuckDB, ADB等, 长期致力于推动开源数据库技术、生态在中国的发展与开源产业人才培养. 曾荣获阿里巴巴麒麟布道师称号、2018届OSCAR开源尖峰人物.

    HI,tsvector,tsquery 的相似度计算用到的这个函数。

    Datum
    rum_ts_distance_tt(PG_FUNCTION_ARGS)
    {
        TSVector    txt = PG_GETARG_TSVECTOR(0);
        TSQuery        query = PG_GETARG_TSQUERY(1);
        float4        res;
    
        res = calc_score(weights, txt, query, DEF_NORM_METHOD);
    
        PG_FREE_IF_COPY(txt, 0);
        PG_FREE_IF_COPY(query, 1);
        if (res == 0)
            PG_RETURN_FLOAT4(get_float4_infinity());
        else
            PG_RETURN_FLOAT4(1.0 / res);
    }
    
    static float4
    calc_score(float4 *arrdata, TSVector txt, TSQuery query, int method)
    {
        DocRepresentation *doc;
        uint32        len,
                    doclen = 0;
        double        Wdoc = 0.0;
        QueryRepresentation qr;
    
        qr.query = query;
        qr.map_item_operand = NULL;
        qr.operandexist = (bool *) palloc0(sizeof(bool) * query->size);
        qr.lenght = query->size;
    
        doc = get_docrep(txt, &qr, &doclen);
        if (!doc)
        {
            pfree(qr.operandexist);
            return 0.0;
        }
    
        Wdoc = calc_score_docr(arrdata, doc, doclen, &qr, method);
    
        if ((method & RANK_NORM_LOGLENGTH) && txt->size > 0)
            Wdoc /= log((double) (count_length(txt) + 1));
    
        if (method & RANK_NORM_LENGTH)
        {
            len = count_length(txt);
            if (len > 0)
                Wdoc /= (double) len;
        }
    
        if ((method & RANK_NORM_UNIQ) && txt->size > 0)
            Wdoc /= (double) (txt->size);
    
        if ((method & RANK_NORM_LOGUNIQ) && txt->size > 0)
            Wdoc /= log((double) (txt->size + 1)) / log(2.0);
    
        pfree(doc);
        pfree(qr.operandexist);
    
        return (float4) Wdoc;
    }
    2019-07-17 20:49:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2023云栖大会:和客户一起玩转PolarDB新特性 立即下载
2023云栖大会:PolarDB for AI 立即下载
2023云栖大会:AnalyticDB PostgreSQL 立即下载

相关镜像