0

English is not my native language so please be patient.

Good afternoon, my Drupal 7 site is currently holding several articles, help topics, and document guides that are versioned, i.e search the "chocolate" word :

Product Cake:

  • title: Old aunt's Chocolate Cake. Version: 1.0
  • title: Old aunt's Chocolate Cake. Version: 1.2
  • title: Colombian Cake, Coffee, and Chocolate. Version: 1.0

Product Candy:

  • title: Chocolate candy for fitness. version: 1.0
  • title: Gretel's Chocolate recipe. version: 2.2
  • title: Gretel's Chocolate recipe. version: 3.0
  • title: Gretel's Chocolate recipe. version: 1.0

Product Beverage:

  • title: Milk & Chocolate. version: 1.0
  • title: Milkshake for the boys in the yard: Vanilla, Chocolate, and More. version: 1.0
  • title: Milkshake for the boys in the yard: Vanilla, Chocolate, and More. version: 2.1
  • title: Hot Chocolate for Cold Winters. version: 1.0

Currently, the site is using Apache Solr Search and related modules. I haven't been able to set a hook that would allow removing the duplicate results for versions with the same title but different versions, ideally I would like to have results for the latest version only.

The Apache Solr Sort module supposedly would do the trick, sadly I'm still getting the same results sorted by most recent creation, but due to those articles having (almost) the same score none of the duplicated versions are removed.

To handle the sort by most recent creation date I have modified the score using the creation date.

Also, is possible to achieve the sort using the Apache Solr search module. admin/config/search/apachesolr/settings/solr/bias?destination=admin/config/search/apachesolr/settings

But to remove duplicate results, still haven't found a solution. The documentation says that my necessity would be solved implementing Grouping, but no result comes from Apache Solr using the following hook_apachesolr_query_prepare which is based in the Sort module, Is this the correct way to implement Grouping? Is there another option to remove the duplicate versions?

/**
 * Implements hook_apachesolr_query_prepare().
 */
function custom_search_apachesolr_query_prepare($query) {

  $env_id = $query->solr('getId');
  $process_callback = apachesolr_environment_variable_get($env_id, 'process_response_callback', 'apachesolr_search_process_response');
  $group_field = apachesolr_environment_variable_get($env_id, 'group_field', 'title');
  $group_limit = apachesolr_environment_variable_get($env_id, 'group_limit', '10');

  if ($process_callback == "apachesolr_sort_process_response") {
    $query->addParams(
      array(
        'group' => 'true',
        'group.field' => $group_field,
        'group.limit' => $group_limit,
        'group.ngroups' => 'true',
        'group.sort' => 'ds_created desc',
        'group.facet' => 'true',
      )
    );
    // start params are different. You should move similar to the group.limit amount
    if (isset($query->page)) {
      $query->removeParam('start');
    }
    if (isset($query->page)) {
      $query->addParam('group.offset', $query->page * $group_limit);
    }
  }
}

Thanks in advance for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.