Author: sevein
Date: Sun Apr 29 10:48:37 2012
New Revision: 11551

Log:
Facets are computed based in the current query but we want to build an ancestor 
lookup table from the current page, there is not way out the box to do that in 
ES. I did it manually, and used SQL without culture fallback to do just one SQL 
query instead of as much as ancestors show in the current results page. This 
affects single and advanced search.

Modified:
   branches/2.0/apps/qubit/modules/search/actions/advancedAction.class.php
   branches/2.0/apps/qubit/modules/search/actions/indexAction.class.php

Modified: 
branches/2.0/apps/qubit/modules/search/actions/advancedAction.class.php
==============================================================================
--- branches/2.0/apps/qubit/modules/search/actions/advancedAction.class.php     
Sun Apr 29 00:06:13 2012        (r11550)
+++ branches/2.0/apps/qubit/modules/search/actions/advancedAction.class.php     
Sun Apr 29 10:48:37 2012        (r11551)
@@ -36,6 +36,7 @@
 
     $this->form->bind($request->getRequestParameters() + 
$request->getGetParameters() + $request->getPostParameters());
 
+    // Check that we have at least a query
     $exist = false;
     if (isset($this->request->searchFields))
     {
@@ -144,11 +145,12 @@
 
       $query->setQuery($queryBool);
 
+      // Add facets to list related items
       foreach (array('repository.id', 'digitalObject.mediaTypeId') as $field)
       {
         $facet = new Elastica_Facet_Terms($field);
         $facet->setField($field);
-        $facet->setSize(50);
+        $facet->setSize(1);
         $query->addFacet($facet);
       }
 
@@ -195,7 +197,7 @@
 
               foreach ($repos as $repo)
               {
-                $reponames[$repo->id] = 
$repo->getAuthorizedFormOfName(array('cultureFallback' => true, 'culture' => 
$this->context->user->getCulture()));
+                $reponames[$repo->id] = 
$repo->getAuthorizedFormOfName(array('cultureFallback' => true));
               }
 
               foreach ($facet['terms'] as $term)
@@ -227,6 +229,36 @@
           }
         }
 
+        $ancestorsIds = array();
+        foreach ($this->pager->getResults() as $hit)
+        {
+          $doc = $hit->getData();
+          foreach ($doc['ancestors'] as $item)
+          {
+            if (in_array($item, $ancestorsIds))
+            {
+              continue;
+            }
+
+            $ancestorsIds[] = $item;
+          }
+        }
+
+        $sql = 'SELECT
+            io.id,
+            o.slug,
+            io.title
+          FROM '.QubitInformationObjectI18n::TABLE_NAME.' AS io
+          LEFT JOIN '.QubitSlug::TABLE_NAME.' AS o ON (o.object_id = io.id AND 
io.culture = ?)
+          WHERE o.object_id IN ('.implode(',', $ancestorsIds).')';
+        $this->pager->ancestors = array();
+        foreach (QubitPdo::fetchAll($sql, 
array($this->context->user->getCulture())) as $ancestor)
+        {
+          $this->pager->ancestors[$ancestor->id] = array(
+            'slug' => $ancestor->slug,
+            'title' => $ancestor->title);
+        }
+
         $this->pager->facets = $facets;
       }
     }

Modified: branches/2.0/apps/qubit/modules/search/actions/indexAction.class.php
==============================================================================
--- branches/2.0/apps/qubit/modules/search/actions/indexAction.class.php        
Sun Apr 29 00:06:13 2012        (r11550)
+++ branches/2.0/apps/qubit/modules/search/actions/indexAction.class.php        
Sun Apr 29 10:48:37 2012        (r11551)
@@ -20,7 +20,6 @@
 class SearchIndexAction extends sfAction
 {
   public $facets = array(
-    'ancestors',
     'repository.id',
     'subjects.id',
     'digitalObject.mediaTypeId',
@@ -107,10 +106,7 @@
       {
         $facets = array();
 
-        $this->pager->ancestors = array();
-
         // build lookup tables for I18nized values
-        // echo "<pre>"; var_dump($resultSet->getFacets()); die();
         foreach ($resultSet->getFacets() as $name => $facet)
         {
           if (isset($facet['terms']))
@@ -124,27 +120,6 @@
 
           switch ($name)
           {
-            // NB: this is actually used as a lookup table, not a facet
-            // it could alternately be done by iterating over the result 
documents
-            // for a potential performance increase
-            case 'ancestors':
-              // do not include the root node
-              unset($ids[QubitInformationObject::ROOT_ID]);
-
-              $criteria = new Criteria;
-              $criteria->add(QubitInformationObject::ID, array_keys($ids), 
Criteria::IN);
-
-              $ancestors = QubitInformationObject::get($criteria);
-
-              foreach ($ancestors as $ancestor)
-              {
-                $this->pager->ancestors[$ancestor->id] = array(
-                  'slug' => $ancestor->slug,
-                  'title' => $ancestor->getTitle(array('cultureFallback' => 
true)));
-              }
-
-              break;
-
             case 'repository.id':
               $criteria = new Criteria;
               $criteria->add(QubitRepository::ID, array_keys($ids), 
Criteria::IN);
@@ -256,6 +231,36 @@
           }
         }
 
+        $ancestorsIds = array();
+        foreach ($this->pager->getResults() as $hit)
+        {
+          $doc = $hit->getData();
+          foreach ($doc['ancestors'] as $item)
+          {
+            if (in_array($item, $ancestorsIds))
+            {
+              continue;
+            }
+
+            $ancestorsIds[] = $item;
+          }
+        }
+
+        $sql = 'SELECT
+            io.id,
+            o.slug,
+            io.title
+          FROM '.QubitInformationObjectI18n::TABLE_NAME.' AS io
+          LEFT JOIN '.QubitSlug::TABLE_NAME.' AS o ON (o.object_id = io.id AND 
io.culture = ?)
+          WHERE o.object_id IN ('.implode(',', $ancestorsIds).')';
+        $this->pager->ancestors = array();
+        foreach (QubitPdo::fetchAll($sql, 
array($this->context->user->getCulture())) as $ancestor)
+        {
+          $this->pager->ancestors[$ancestor->id] = array(
+            'slug' => $ancestor->slug,
+            'title' => $ancestor->title);
+        }
+
         $this->pager->facets = $facets;
       }
     }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to