/**
  * Return the associated index document
  * 
  * @return \Tollwerk\TwLucenesearch\Domain\Model\Document 			Associated index document
  * @see \Zend_Search_Lucene_Search_QueryHit::getDocument()
  */
 public function getDocument()
 {
     if (!$this->_document instanceof \Tollwerk\TwLucenesearch\Domain\Model\Document) {
         $this->_document = \Tollwerk\TwLucenesearch\Domain\Model\Document::cast(parent::getDocument());
     }
     return $this->_document;
 }
Example #2
0
 public function find($query)
 {
     if (is_string($query)) {
         $query = Zend_Search_Lucene_Search_QueryParser::parse($query);
     }
     if (!$query instanceof Zend_Search_Lucene_Search_Query) {
         require_once 'Zend/Search/Lucene/Exception.php';
         throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
     }
     $this->commit();
     $hits = array();
     $scores = array();
     $ids = array();
     $query = $query->rewrite($this)->optimize($this);
     $query->execute($this);
     $topScore = 0;
     foreach ($query->matchedDocs() as $id => $num) {
         $docScore = $query->score($id, $this);
         if ($docScore != 0) {
             $hit = new Zend_Search_Lucene_Search_QueryHit($this);
             $hit->id = $id;
             $hit->score = $docScore;
             $hits[] = $hit;
             $ids[] = $id;
             $scores[] = $docScore;
             if ($docScore > $topScore) {
                 $topScore = $docScore;
             }
         }
         if (self::$_resultSetLimit != 0 && count($hits) >= self::$_resultSetLimit) {
             break;
         }
     }
     if (count($hits) == 0) {
         // skip sorting, which may cause a error on empty index
         return array();
     }
     if ($topScore > 1) {
         foreach ($hits as $hit) {
             $hit->score /= $topScore;
         }
     }
     if (func_num_args() == 1) {
         // sort by scores
         array_multisort($scores, SORT_DESC, SORT_NUMERIC, $ids, SORT_ASC, SORT_NUMERIC, $hits);
     } else {
         // sort by given field names
         $argList = func_get_args();
         $fieldNames = $this->getFieldNames();
         $sortArgs = array();
         require_once 'Zend/Search/Lucene/Exception.php';
         for ($count = 1; $count < count($argList); $count++) {
             $fieldName = $argList[$count];
             if (!is_string($fieldName)) {
                 throw new Zend_Search_Lucene_Exception('Field name must be a string.');
             }
             if (!in_array($fieldName, $fieldNames)) {
                 throw new Zend_Search_Lucene_Exception('Wrong field name.');
             }
             $valuesArray = array();
             foreach ($hits as $hit) {
                 try {
                     $value = $hit->getDocument()->getFieldValue($fieldName);
                 } catch (Zend_Search_Lucene_Exception $e) {
                     if (strpos($e->getMessage(), 'not found') === false) {
                         throw $e;
                     } else {
                         $value = null;
                     }
                 }
                 $valuesArray[] = $value;
             }
             $sortArgs[] = $valuesArray;
             if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                 $count++;
                 $sortArgs[] = $argList[$count];
                 if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                     $count++;
                     $sortArgs[] = $argList[$count];
                 } else {
                     if ($argList[$count] == SORT_ASC || $argList[$count] == SORT_DESC) {
                         $sortArgs[] = SORT_REGULAR;
                     } else {
                         $sortArgs[] = SORT_ASC;
                     }
                 }
             } else {
                 $sortArgs[] = SORT_ASC;
                 $sortArgs[] = SORT_REGULAR;
             }
         }
         // Sort by id's if values are equal
         $sortArgs[] = $ids;
         $sortArgs[] = SORT_ASC;
         $sortArgs[] = SORT_NUMERIC;
         // Array to be sorted
         $sortArgs[] =& $hits;
         // Do sort
         call_user_func_array('array_multisort', $sortArgs);
     }
     return $hits;
 }
Example #3
0
 /**
  * Performs a query against the index and returns an array
  * of Zend_Search_Lucene_Search_QueryHit objects.
  * Input is a string or Zend_Search_Lucene_Search_Query.
  *
  * @param Zend_Search_Lucene_Search_QueryParser|string $query
  * @return array Zend_Search_Lucene_Search_QueryHit
  * @throws Zend_Search_Lucene_Exception
  */
 public function find($query)
 {
     if (is_string($query)) {
         // require_once 'Zend/Search/Lucene/Search/QueryParser.php';
         $query = Zend_Search_Lucene_Search_QueryParser::parse($query);
     }
     if (!$query instanceof Zend_Search_Lucene_Search_Query) {
         // require_once 'Zend/Search/Lucene/Exception.php';
         throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
     }
     $this->commit();
     $hits = array();
     $scores = array();
     $ids = array();
     $query = $query->rewrite($this)->optimize($this);
     $query->execute($this);
     $topScore = 0;
     /** Zend_Search_Lucene_Search_QueryHit */
     // require_once 'Zend/Search/Lucene/Search/QueryHit.php';
     foreach ($query->matchedDocs() as $id => $num) {
         $docScore = $query->score($id, $this);
         if ($docScore != 0) {
             $hit = new Zend_Search_Lucene_Search_QueryHit($this);
             $hit->id = $id;
             $hit->score = $docScore;
             $hits[] = $hit;
             $ids[] = $id;
             $scores[] = $docScore;
             if ($docScore > $topScore) {
                 $topScore = $docScore;
             }
         }
         if (self::$_resultSetLimit != 0 && count($hits) >= self::$_resultSetLimit) {
             break;
         }
     }
     if (count($hits) == 0) {
         // skip sorting, which may cause a error on empty index
         return array();
     }
     if ($topScore > 1) {
         foreach ($hits as $hit) {
             $hit->score /= $topScore;
         }
     }
     if (func_num_args() == 1) {
         // sort by scores
         array_multisort($scores, SORT_DESC, SORT_NUMERIC, $ids, SORT_ASC, SORT_NUMERIC, $hits);
     } else {
         // sort by given field names
         $argList = func_get_args();
         $fieldNames = $this->getFieldNames();
         $sortArgs = array();
         // PHP 5.3 now expects all arguments to array_multisort be passed by
         // reference (if it's invoked through call_user_func_array());
         // since constants can't be passed by reference, create some placeholder variables.
         $sortReg = SORT_REGULAR;
         $sortAsc = SORT_ASC;
         $sortNum = SORT_NUMERIC;
         $sortFieldValues = array();
         // require_once 'Zend/Search/Lucene/Exception.php';
         for ($count = 1; $count < count($argList); $count++) {
             $fieldName = $argList[$count];
             if (!is_string($fieldName)) {
                 throw new Zend_Search_Lucene_Exception('Field name must be a string.');
             }
             if (strtolower($fieldName) == 'score') {
                 $sortArgs[] =& $scores;
             } else {
                 if (!in_array($fieldName, $fieldNames)) {
                     throw new Zend_Search_Lucene_Exception('Wrong field name.');
                 }
                 if (!isset($sortFieldValues[$fieldName])) {
                     $valuesArray = array();
                     foreach ($hits as $hit) {
                         try {
                             $value = $hit->getDocument()->getFieldValue($fieldName);
                         } catch (Zend_Search_Lucene_Exception $e) {
                             if (strpos($e->getMessage(), 'not found') === false) {
                                 throw new Zend_Search_Lucene_Exception($e->getMessage(), $e->getCode(), $e);
                             } else {
                                 $value = null;
                             }
                         }
                         $valuesArray[] = $value;
                     }
                     // Collect loaded values in $sortFieldValues
                     // Required for PHP 5.3 which translates references into values when source
                     // variable is destroyed
                     $sortFieldValues[$fieldName] = $valuesArray;
                 }
                 $sortArgs[] =& $sortFieldValues[$fieldName];
             }
             if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                 $count++;
                 $sortArgs[] =& $argList[$count];
                 if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                     $count++;
                     $sortArgs[] =& $argList[$count];
                 } else {
                     if ($argList[$count] == SORT_ASC || $argList[$count] == SORT_DESC) {
                         $sortArgs[] =& $sortReg;
                     } else {
                         $sortArgs[] =& $sortAsc;
                     }
                 }
             } else {
                 $sortArgs[] =& $sortAsc;
                 $sortArgs[] =& $sortReg;
             }
         }
         // Sort by id's if values are equal
         $sortArgs[] =& $ids;
         $sortArgs[] =& $sortAsc;
         $sortArgs[] =& $sortNum;
         // Array to be sorted
         $sortArgs[] =& $hits;
         // Do sort
         call_user_func_array('array_multisort', $sortArgs);
     }
     return $hits;
 }
Example #4
0
 /**
  * Get twig properties for matching search document.
  *
  * @param Zend_Search_Lucene_Search_QueryHit $hit
  * @return array
  */
 public function getHitProperties(Zend_Search_Lucene_Search_QueryHit $hit)
 {
     $r = $this->getRequest();
     $snippet = Curry_String::toInternalEncoding($hit->body, 'utf-8');
     $snippet = self::createSearchSnippet($snippet, $r->get['query'], $this->snippetLength);
     $relatedObject = null;
     $model = Curry_String::toInternalEncoding($hit->model, 'utf-8');
     $fields = array();
     foreach ($hit->getDocument()->getFieldNames() as $fieldName) {
         $fields[$fieldName] = $hit->{$fieldName};
     }
     return array('Title' => Curry_String::toInternalEncoding($hit->title, 'utf-8'), 'Description' => Curry_String::toInternalEncoding($hit->description, 'utf-8'), 'Url' => Curry_String::toInternalEncoding($hit->url, 'utf-8'), 'Snippet' => $snippet, 'Score' => $hit->score, 'Fields' => $fields, 'RelatedObject' => new Curry_OnDemand(array($this, 'getRelatedObject'), $model, unserialize($hit->model_id)));
 }
Example #5
0
 public function find($query)
 {
     if (is_string($query)) {
         $query = Zend_Search_Lucene_Search_QueryParser::parse($query);
     }
     if (!$query instanceof Zend_Search_Lucene_Search_Query) {
         throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
     }
     $this->commit();
     $hits = array();
     $scores = array();
     $ids = array();
     $query = $query->rewrite($this)->optimize($this);
     $query->execute($this);
     $topScore = 0;
     foreach ($query->matchedDocs() as $id => $num) {
         $docScore = $query->score($id, $this);
         if ($docScore != 0) {
             $hit = new Zend_Search_Lucene_Search_QueryHit($this);
             $hit->id = $id;
             $hit->score = $docScore;
             $hits[] = $hit;
             $ids[] = $id;
             $scores[] = $docScore;
             if ($docScore > $topScore) {
                 $topScore = $docScore;
             }
         }
         if (self::$_resultSetLimit != 0 && count($hits) >= self::$_resultSetLimit) {
             break;
         }
     }
     if (count($hits) == 0) {
         return array();
     }
     if ($topScore > 1) {
         foreach ($hits as $hit) {
             $hit->score /= $topScore;
         }
     }
     if (func_num_args() == 1) {
         array_multisort($scores, SORT_DESC, SORT_NUMERIC, $ids, SORT_ASC, SORT_NUMERIC, $hits);
     } else {
         $argList = func_get_args();
         $fieldNames = $this->getFieldNames();
         $sortArgs = array();
         $sortReg = SORT_REGULAR;
         $sortAsc = SORT_ASC;
         $sortNum = SORT_NUMERIC;
         $sortFieldValues = array();
         for ($count = 1; $count < count($argList); $count++) {
             $fieldName = $argList[$count];
             if (!is_string($fieldName)) {
                 throw new Zend_Search_Lucene_Exception('Field name must be a string.');
             }
             if (strtolower($fieldName) == 'score') {
                 $sortArgs[] =& $scores;
             } else {
                 if (!in_array($fieldName, $fieldNames)) {
                     throw new Zend_Search_Lucene_Exception('Wrong field name.');
                 }
                 if (!isset($sortFieldValues[$fieldName])) {
                     $valuesArray = array();
                     foreach ($hits as $hit) {
                         try {
                             $value = $hit->getDocument()->getFieldValue($fieldName);
                         } catch (Zend_Search_Lucene_Exception $e) {
                             if (strpos($e->getMessage(), 'not found') === false) {
                                 throw new Zend_Search_Lucene_Exception($e->getMessage(), $e->getCode(), $e);
                             } else {
                                 $value = null;
                             }
                         }
                         $valuesArray[] = $value;
                     }
                     $sortFieldValues[$fieldName] = $valuesArray;
                 }
                 $sortArgs[] =& $sortFieldValues[$fieldName];
             }
             if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                 $count++;
                 $sortArgs[] =& $argList[$count];
                 if ($count + 1 < count($argList) && is_integer($argList[$count + 1])) {
                     $count++;
                     $sortArgs[] =& $argList[$count];
                 } else {
                     if ($argList[$count] == SORT_ASC || $argList[$count] == SORT_DESC) {
                         $sortArgs[] =& $sortReg;
                     } else {
                         $sortArgs[] =& $sortAsc;
                     }
                 }
             } else {
                 $sortArgs[] =& $sortAsc;
                 $sortArgs[] =& $sortReg;
             }
         }
         $sortArgs[] =& $ids;
         $sortArgs[] =& $sortAsc;
         $sortArgs[] =& $sortNum;
         $sortArgs[] =& $hits;
         call_user_func_array('array_multisort', $sortArgs);
     }
     return $hits;
 }
Example #6
0
 /**
  * Get twig properties for matching search document.
  *
  * @param \Zend_Search_Lucene_Search_QueryHit $hit
  * @return array
  */
 public function getHitProperties(\Zend_Search_Lucene_Search_QueryHit $hit)
 {
     $r = $this->app->request;
     $snippet = self::createSearchSnippet($hit->body, $r->query->get('query'), $this->snippetLength);
     $fields = array();
     foreach ($hit->getDocument()->getFieldNames() as $fieldName) {
         $fields[$fieldName] = $hit->{$fieldName};
     }
     return array('Title' => $hit->title, 'Description' => $hit->description, 'Url' => $hit->url, 'Snippet' => $snippet, 'Score' => $hit->score, 'Fields' => $fields, 'RelatedObject' => new OnDemand(array($this, 'getRelatedObject'), $hit->model, unserialize($hit->model_id)));
 }