douna3367 2013-01-09 17:45
浏览 43
已采纳

Sphinx / PHP:Sphinx可以返回给定数组的匹配顺序吗?

Using Sphinx 2.0.6, is there a way to have sphinx return a specific order based on the document ID?

For example, say there are 1000 documents all having id 1-1000. But I want to return, in order, ID 999,1000,4,5,2,and so on.

This use case: The positioning is dynamic and needs to be done through Sphinx. The positioning value needs to be as an attribute that can change on-the-fly. This is also paged -- so I can't simply gather the ID Set and request a SQL. Sphinx itself needs to return the specific order I give it.

$cl->setSelect("*,FIND_IN_SET(id,".implode($id_array).") AS id_position");
$cl->SetSortMode(SPH_SORT_EXTENDED, 'id_position DESC');

$cl->setSelect("*,FIELD(id,".implode($id_array).") AS id_position");
$cl->SetSortMode(SPH_SORT_EXTENDED, 'id_position DESC');

Unfortunately, doesn't look like Sphinx supports FIELD() and FIELD_IN_SET().

Any ideas how to complete this task? I'm at a loss right now and could use the help!

  • 写回答

2条回答 默认 最新

  • dqzpt40064 2013-01-09 22:19
    关注

    Figured it out thankfully! This is an excellent solution to being able to order by a dynamic id array. (real world use... user's favorites, recommended products, user's most visited)

    Sphinx PHP Code:

    $cl->SetOverride("id_position", SPH_ATTR_INTEGER, user_id_position());
    $cl->SetSortMode(SPH_SORT_EXTENDED, "id_position ASC");
    

    PHP Function to create the associative array:

    function user_id_position() {
            $id = $_original_id_list // This variable is the original id list that is in the correct order
            $max = count($id);
            $set = array();
            for ($i=0;$i < $max;$i++)
                $set[$id[$i]] = $i; // turns the array into array(document_id1 => position, document_id2 => position, ...)
            return $set;
    }
    

    sphinx.conf

    source index_name
    {
        sql_query                       = \
                SELECT \
                        *, 0 as id_position \
                FROM \
                        database_table;
        sql_attr_uint         = id_position
    }
    

    After adding the information to sphinx.conf you'll need to --rotate and it'll work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿