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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭