My project is setup in Drupal 7 and i am using search API for display my custom search result. Actually i want to show search result based on two content types content so i have configure the search api for those content type and select the fields which i want for search and then indexing the content.

For display the search result i am using drupal View of Index node type. But the problem is that in my search result i am getting duplicate result because of some contents have same titles but having different body text.

I want to remove search result which have duplicate title. (Custom field created by myself)

I have tried some custom solution by View hook but its not working properly its give pager issue

function custom_views_pre_render(&$view) {
  if ($view->name == 'search_books') {
   $view_ISBN = array();
    foreach ($view->result AS $key => $res_view) {
        $ISBN = $res_view->entity->field_isbn[und][0]['value'];
       if (!in_array($ISBN,$view_ISBN)) {
                    $view_ISBN[] = $ISBN;

    $view->query->pager->total_items = count($view->result);


This remove the duplicate result but it gives wrong pager result.

I have also tried the Query alter hook but its not working for me.

function custom_query_alter($query) {

  if (isset($query->alterMetaData)) {
    if (isset($query->alterMetaData['view'])) {
      if($query->alterMetaData['view']->name == 'search_books') {
        $fields =& $query->getGroupBy();
        // Tried various fields to check which was the field creating the problem.

        $query->distinct = TRUE;


I have also tried to install "views_distinct" module and distinct the field from view but its also gives me pager issue.

Any Idea how to solve this issue?

  • From the description these are not really duplicates, but different records with the same title, is that correct? If so, I can see why what you have tried won't work. Search API queries can't be altered as if they were SQL, either. What's the value in having the duplicates in your data? Could you unpublish them or flag them in some other way so you can remove them from the query? – Alfred Armstrong Nov 21 '19 at 9:55
  • @AlfredArmstrong Yes, They are different records with same title. title and some other field is same for some records so i want to display only single entry in search result and on click of title i want to display all content list which have same titles because those field are referenced by taxonomy. Sorry, I can't remove or unpublished those contents. – arun kamboj Nov 21 '19 at 10:17
  • Recording to your comment above the field_isbn field is term reference ? – berramou Nov 21 '19 at 11:44
  • @berramou Yes this is term reference – arun kamboj Nov 21 '19 at 11:51
  • What exactly do you mean by "wrong pager result"? Wrong total count, missing items, pages without items? Is it possible to have an item without a duplicate? If so, maybe your problem is that custom_views_pre_render(&$view) also removes any item without a duplicate. Unless I am misunderstanding what is going on, you should change your if block to something like if (!in_array($ISBN,$view_ISBN)) { $view_ISBN[] = $ISBN; } else{ unset($view->result[$key]); } This would remove all duplicates. FYI, $view->result contains just the items for the current page, not all the results from the query. – runswithscissors Nov 21 '19 at 16:13

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.