Author: mj
Date: Fri Oct 14 08:56:20 2011
New Revision: 10093

Log:
Refactor back to ZSL API to fix boolean behaviour; code clean-up

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

Modified: trunk/apps/qubit/modules/search/actions/advancedAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/search/actions/advancedAction.class.php    Fri Oct 
14 06:42:33 2011        (r10092)
+++ trunk/apps/qubit/modules/search/actions/advancedAction.class.php    Fri Oct 
14 08:56:20 2011        (r10093)
@@ -50,8 +50,6 @@
     {
       if (isset($request->searchFields)) {
         // we are handling a search request
-        $this->buildAdvancedSearch($request);
-
         parent::execute($request);
       }
     }
@@ -119,64 +117,79 @@
     }
   }
 
-  public function buildAdvancedSearch($request)
+  public function parseQuery()
   {
-    foreach ($request->searchFields as $searchField) {
+
+    QubitSearch::getInstance();
+    $queryBuilt = new Zend_Search_Lucene_Search_Query_Boolean();
+
+    foreach ($this->request->searchFields as $searchField)
+    {
       // if no terms for this field, skip it
-      if (empty($searchField['query'])) continue;
+      if (empty($searchField['query']))
+      {
+        continue;
+      }
 
       // enclose phrase searches in quotes (strip existing ones)
       if ('phrase' == $searchField['match']) {
-        $terms = array('"'.str_replace(array('"', "'"), '', 
$searchField['query']).'"');
+        $term = '"'.str_replace(array('"', "'"), '', 
$searchField['query']).'"';
       } else {
-        $terms = explode(' ', $searchField['query']);
+        $term = $searchField['query'];
       }
 
       // select which boolean operator to use
       if (!isset($searchField['operator'])) $searchField['operator'] = null;
-      switch ($searchField['operator']) {
+      switch ($searchField['operator'])
+      {
         case 'not':
-          $token = '-';
+          $token = false;
           break;
+
         case 'or':
-          $token = '';
+          $token = null;
           break;
+
         case 'and':
         default:
-          $token = '+';
+          $token = true;
           break;
       }
 
-      if (!empty($searchField['field'])) {
-        // search on a specific field
-        $string = '';
-        foreach ($terms as $term) {
-          $string .= $searchField['field'].':'.$term.' ';
-        }
-        $token .= '('.$string.') ';
+      $queryBuilt->addSubquery(QubitSearch::getInstance()->addTerm($term, 
$searchField['field']), $token);
+    }
 
-      } else {
-        // search across all fields
-        $token .= '('.implode(' ', $terms).') ';
-      }
+    $query = new Zend_Search_Lucene_Search_Query_Boolean();
+    $query->addSubquery($queryBuilt, true);
 
-      $this->querystring .= $token;
-    }
+    return $query;
+  }
 
+  public function filterQuery($query)
+  {
     // limit to a repository if selected
-    if (!empty($request->repository)) {
-      $this->prefix .= ' +repositoryId:' . $request->repository;
+    if (!empty($this->request->repository))
+    {
+      
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->repository,
 'repositoryId'), true);
     }
 
     // digital object filters
-    if ('true' == $request->hasDigitalObject) {
-      $this->prefix .= ' +hasDigitalObject:true ';
-    } elseif ('false' == $request->hasDigitalObject) {
-      $this->prefix .= ' +hasDigitalObject:false ';
+    if ('true' == $this->request->hasDigitalObject)
+    {
+      $query->addSubquery(QubitSearch::getInstance()->addTerm('true', 
'hasDigitalObject'), true);
+    }
+    else if ('false' == $this->request->hasDigitalObject)
+    {
+      $query->addSubquery(QubitSearch::getInstance()->addTerm('false', 
'hasDigitalObject'), true);
     }
 
-    if (!empty($request->media)) {
-      $this->prefix .= '+do_mediaTypeId:'.$request->media.' ';
+    if (!empty($this->request->media))
+    {
+      
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->media, 
'do_mediaTypeId'), true);
     }
+
+    $query = parent::filterQuery($query);
+
+    return $query;
   }
 }

Modified: trunk/apps/qubit/modules/search/actions/indexAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/search/actions/indexAction.class.php       Fri Oct 
14 06:42:33 2011        (r10092)
+++ trunk/apps/qubit/modules/search/actions/indexAction.class.php       Fri Oct 
14 08:56:20 2011        (r10093)
@@ -19,9 +19,6 @@
 
 class SearchIndexAction extends sfAction
 {
-  var $querystring = '';
-  var $prefix = '';
-
   public function execute($request)
   {
     if (!isset($request->limit))
@@ -29,33 +26,26 @@
       $request->limit = sfConfig::get('app_hits_per_page');
     }
 
-    // Limit search to current culture and info. objects
-    $this->prefix .= ' +culture:' . $this->context->user->getCulture() . ' ';
-    $this->prefix .= ' +className:QubitInformationObject ';
-
     // Simple search
-    if (empty($this->querystring) && !empty($request->query))
+    if (isset($request->query))
     {
       $this->title = $this->context->i18n->__('Search for [%1%]', array('%1%' 
=> $request->query));
-
-      $this->querystring = $request->query;
-
-      // Limit to a repository if in context
-      if (isset($this->getRoute()->resource) && $this->getRoute()->resource 
instanceof QubitRepository)
-      {
-        $this->prefix .= ' +repositoryId:' . $this->getRoute()->resource->id;
-        $this->title .= $this->context->i18n->__(' in %1%', array('%1%' => 
$this->getRoute()->resource->authorizedFormOfName));
-      }
-
       $this->response->setTitle("{$this->title} - 
{$this->response->getTitle()}");
     }
 
-    if (empty($this->querystring))
+    $query = $this->parseQuery();
+    $query = $this->filterQuery($query);
+
+    try
     {
-      $this->prefix = '';
+      $hits = 
QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
     }
+    catch (Exception $e)
+    {
+      $this->error = $e->getMessage();
 
-    $hits = $this->executeQuery();
+      return;
+    }
 
     if (!empty($hits))
     {
@@ -72,12 +62,12 @@
     }
   }
 
-  public function executeQuery()
+  public function parseQuery()
   {
     try
     {
       // Parse query string
-      $query = QubitSearch::getInstance()->parse($this->prefix .' '. 
$this->querystring);
+      $queryParsed = QubitSearch::getInstance()->parse($this->request->query);
     }
     catch (Exception $e)
     {
@@ -86,20 +76,28 @@
       return;
     }
 
+    $query = new Zend_Search_Lucene_Search_Query_Boolean();
+    $query->addSubquery($queryParsed, true);
+
+    return $query;
+  }
+
+  public function filterQuery($query)
+  {
+    // Limit search to current culture and info. objects
+    
$query->addSubquery(QubitSearch::getInstance()->addTerm('QubitInformationObject',
 'className'), true);
+    
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->context->user->getCulture(),
 'culture'), true);
+
     $query = QubitAcl::searchFilterByRepository($query, 'read');
     $query = QubitAcl::searchFilterDrafts($query);
 
-    try
+    // Limit to a repository if in context
+    if (isset($this->getRoute()->resource) && $this->getRoute()->resource 
instanceof QubitRepository)
     {
-      $hits = 
QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
-    }
-    catch (Exception $e)
-    {
-      $this->error = $e->getMessage();
-
-      return;
+      
$query->addSubquery(QubitSearch::getInstance()->addTerm($this->getRoute()->resource->id,
 'repositoryId'), true);
+      $this->title .= $this->context->i18n->__(' in %1%', array('%1%' => 
$this->getRoute()->resource->authorizedFormOfName));
     }
 
-    return $hits;
+    return $query;
   }
 }

-- 
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