【问题标题】:de-dupe array of documents对文档数组进行重复数据删除
【发布时间】:2016-05-19 21:36:21
【问题描述】:

我运行 solr 查询,它返回作业的结果集。

有些职位是重复的(但来源不同),这取决于职位名称、描述和位置是否相同。

我想遍历我的结果集,并将任何重复的内容合并到一个作业中,而该作业有多个来源...类似于:

原始结果:

$jobs = array(
    array(
        'id'          => 'job1',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source1',
    ),
    array(
        'id'          => 'job2',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source2',
    ),
    array(
        'id'          => 'job3',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source3',
    ),
    array(
        'id'          => 'job4',
        'title'       => 'testing',
        'description' => 'testing',
        'location'    => 'testing',
        'source'      => 'source1',
    )
);

会变成:

$jobs = array(
    array(
        'id'          => 'job1',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source1',
        'other_sources' => array(
            array(
                'id'          => 'job2',
                'title'       => 'test',
                'description' => 'test',
                'location'    => 'test',
                'source'      => 'source2'
            ),
            array(
                'id'          => job3,
                'title'       => 'test',
                'description' => 'test',
                'location'    => 'test',
                'source'      => 'source3'
            ), 
        ),
    ),
    array(
        'id'          => 'job4',
        'title'       => 'testing',
        'description' => 'testing',
        'location'    => 'testing',
        'source'      => 'source1'
    )
);

我怎样才能做到这一点?在 PHP 中或者在 Solr 查询本身中(我正在使用 Solarium 进行 Solr 查询)

【问题讨论】:

    标签: php solr duplicates


    【解决方案1】:

    这样的事情怎么样?

    <?php
    
    $result = array();
    foreach ($jobs as $job) {
    
        if (!empty($result[$job['title']])) {
            $result[$job['title']]['other_sources'][] = $job;
        }
        else {
            $result[$job['title']] = $job;
        }
    
    }
    

    它初始化一个空数组($result),然后循环遍历作业数组。空数组将存储以标题为键的作业。如果结果数组中不存在职位名称,则将其添加。如果职位名称确实存在于作业数组中,则它将作业附加到现有作业内的数组中(在键'other_sources'下)

    【讨论】:

    • 这完全符合我的要求 - 并且比我尝试过的任何方法都简单得多,谢谢。然而,它让我意识到,我需要的是一种在 solr 查询本身中执行此操作的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多