Example #1
0
 /**
  * Calculate result vector for non Conjunction query
  * (like '+something -another')
  *
  * @param Zend_Search_Lucene $reader
  */
 private function _calculateNonConjunctionResult($reader)
 {
     if (extension_loaded('bitset')) {
         $required = null;
         $neither = bitset_empty();
         $prohibited = bitset_empty();
         foreach ($this->_terms as $termId => $term) {
             $termDocs = bitset_from_array($reader->termDocs($term));
             if ($this->_signs[$termId] === true) {
                 // required
                 if ($required !== null) {
                     $required = bitset_intersection($required, $termDocs);
                 } else {
                     $required = $termDocs;
                 }
             } elseif ($this->_signs[$termId] === false) {
                 // prohibited
                 $prohibited = bitset_union($prohibited, $termDocs);
             } else {
                 // neither required, nor prohibited
                 $neither = bitset_union($neither, $termDocs);
             }
             $this->_termsPositions[$termId] = $reader->termPositions($term);
         }
         if ($required === null) {
             $required = $neither;
         }
         $this->_resVector = bitset_intersection($required, bitset_invert($prohibited, $reader->count()));
     } else {
         $required = null;
         $neither = array();
         $prohibited = array();
         foreach ($this->_terms as $termId => $term) {
             $termDocs = array_flip($reader->termDocs($term));
             if ($this->_signs[$termId] === true) {
                 // required
                 if ($required !== null) {
                     // substitute for bitset_intersection
                     foreach ($required as $key => $value) {
                         if (!isset($termDocs[$key])) {
                             unset($required[$key]);
                         }
                     }
                 } else {
                     $required = $termDocs;
                 }
             } elseif ($this->_signs[$termId] === false) {
                 // prohibited
                 // substitute for bitset_union
                 foreach ($termDocs as $key => $value) {
                     $prohibited[$key] = $value;
                 }
             } else {
                 // neither required, nor prohibited
                 // substitute for bitset_union
                 foreach ($termDocs as $key => $value) {
                     $neither[$key] = $value;
                 }
             }
             $this->_termsPositions[$termId] = $reader->termPositions($term);
         }
         if ($required === null) {
             $required = $neither;
         }
         foreach ($required as $key => $value) {
             if (isset($prohibited[$key])) {
                 unset($required[$key]);
             }
         }
         $this->_resVector = $required;
     }
 }
Example #2
0
 /**
  * Score specified document
  *
  * @param integer $docId
  * @param Zend_Search_Lucene $reader
  * @return float
  */
 public function score($docId, $reader)
 {
     if ($this->_docVector === null) {
         if (extension_loaded('bitset')) {
             $this->_docVector = bitset_from_array($reader->termDocs($this->_term));
         } else {
             $this->_docVector = array_flip($reader->termDocs($this->_term));
         }
         $this->_termPositions = $reader->termPositions($this->_term);
         $this->_initWeight($reader);
     }
     $match = extension_loaded('bitset') ? bitset_in($this->_docVector, $docId) : isset($this->_docVector[$docId]);
     if ($this->_sign && $match) {
         return $reader->getSimilarity()->tf(count($this->_termPositions[$docId])) * $this->_weight->getValue() * $reader->norm($docId, $this->_term->field);
     } else {
         return 0;
     }
 }
 /**
  * Calculate result vector
  *
  * @param Zend_Search_Lucene $reader
  */
 private function _calculateResult($reader)
 {
     if (extension_loaded('bitset')) {
         foreach ($this->_terms as $termId => $term) {
             if ($this->_resVector === null) {
                 $this->_resVector = bitset_from_array($reader->termDocs($term));
             } else {
                 $this->_resVector = bitset_intersection($this->_resVector, bitset_from_array($reader->termDocs($term)));
             }
             $this->_termsPositions[$termId] = $reader->termPositions($term);
         }
     } else {
         foreach ($this->_terms as $termId => $term) {
             if ($this->_resVector === null) {
                 $this->_resVector = array_flip($reader->termDocs($term));
             } else {
                 $termDocs = array_flip($reader->termDocs($term));
                 foreach ($this->_resVector as $key => $value) {
                     if (!isset($termDocs[$key])) {
                         unset($this->_resVector[$key]);
                     }
                 }
             }
             $this->_termsPositions[$termId] = $reader->termPositions($term);
         }
     }
 }
Example #4
0
 /**
  * Execute query in context of index reader
  * It also initializes necessary internal structures
  *
  * @param Zend_Search_Lucene $reader
  */
 public function execute($reader)
 {
     $this->_docVector = array_flip($reader->termDocs($this->_term));
     $this->_termPositions = $reader->termPositions($this->_term);
     // Initialize weight if it's not done yet
     $this->_initWeight($reader);
 }
Example #5
0
 /**
  * Calculate result vector for non Conjunction query
  * (like '+something -another')
  *
  * @param Zend_Search_Lucene $reader
  */
 private function _calculateNonConjunctionResult($reader)
 {
     $required = null;
     $optional = array();
     $prohibited = array();
     foreach ($this->_terms as $termId => $term) {
         $termDocs = array_flip($reader->termDocs($term));
         if ($this->_signs[$termId] === true) {
             // required
             if ($required !== null) {
                 // array intersection
                 $required = array_intersect_key($required, $termDocs);
             } else {
                 $required = $termDocs;
             }
         } elseif ($this->_signs[$termId] === false) {
             // prohibited
             // array union
             $prohibited += $termDocs;
         } else {
             // neither required, nor prohibited
             // array union
             $optional += $termDocs;
         }
         $this->_termsPositions[$termId] = $reader->termPositions($term);
     }
     if ($required !== null) {
         $this->_resVector = array_diff_key($required, $prohibited);
     } else {
         $this->_resVector = array_diff_key($optional, $prohibited);
     }
     ksort($this->_resVector, SORT_NUMERIC);
 }
Example #6
0
 /**
  * Execute query in context of index reader
  * It also initializes necessary internal structures
  *
  * @param Zend_Search_Lucene $reader
  */
 public function execute($reader)
 {
     $this->_resVector = null;
     if (count($this->_terms) == 0) {
         $this->_resVector = array();
     }
     foreach ($this->_terms as $termId => $term) {
         if ($this->_resVector === null) {
             $this->_resVector = array_flip($reader->termDocs($term));
         } else {
             $this->_resVector = array_intersect_key($this->_resVector, array_flip($reader->termDocs($term)));
         }
         if (count($this->_resVector) == 0) {
             // Empty result set, we don't need to check other terms
             break;
         }
         $this->_termsPositions[$termId] = $reader->termPositions($term);
     }
     ksort($this->_resVector, SORT_NUMERIC);
     // Initialize weight if it's not done yet
     $this->_initWeight($reader);
 }
Example #7
0
 /**
  * This will refresh the search index for this topic (for all message content under this topic)
  * @param Zend_Search_Lucene $objIndex should be null if we are updating just one -- but for bulk index updates, you can pass in an already loaded index file
  * @return void
  */
 public function RefreshSearchIndex($objIndex = null)
 {
     // Currently only implemented for Forum-based topic/message searches
     if ($this->TopicLink->TopicLinkTypeId != TopicLinkType::Forum) {
         return;
     }
     if (!$objIndex) {
         $objIndex = new Zend_Search_Lucene(__SEARCH_INDEXES__ . '/topics');
         $blnIndexProvided = false;
     } else {
         $blnIndexProvided = true;
     }
     // Retrievew the Index Documents (if applicable) to delete them from the index
     $objSearchTerm = new Zend_Search_Lucene_Index_Term($this->Id, 'db_id');
     foreach ($objIndex->termDocs($objSearchTerm) as $intDocId) {
         $objIndex->delete($intDocId);
     }
     // Create the Message Contents for this Topic
     $strContents = null;
     foreach ($this->GetMessageArray(QQ::OrderBy(QQN::Message()->ReplyNumber)) as $objMessage) {
         $strMessage = strip_tags(trim($objMessage->CompiledHtml));
         $strMessage = html_entity_decode($strMessage, ENT_QUOTES, 'UTF-8');
         $strContents .= $strMessage . "\r\n\r\n";
     }
     // Create the Document
     $objDocument = new Zend_Search_Lucene_Document();
     $objDocument->addField(Zend_Search_Lucene_Field::Keyword('db_id', $this->Id));
     $objDocument->addField(Zend_Search_Lucene_Field::UnIndexed('topic_link_id', $this->TopicLinkId));
     $objDocument->addField(Zend_Search_Lucene_Field::UnIndexed('topic_link_type_id', $this->TopicLink->TopicLinkTypeId));
     $objDocument->addField(Zend_Search_Lucene_Field::UnIndexed('message_count', $this->MessageCount));
     $objDocument->addField(Zend_Search_Lucene_Field::UnIndexed('last_post_date', $this->LastPostDate->Timestamp));
     $objDocument->addField(Zend_Search_Lucene_Field::Text('title', $this->Name));
     $objDocument->addField(Zend_Search_Lucene_Field::UnStored('contents', trim($strContents)));
     // Add Document to Index
     $objIndex->addDocument($objDocument);
     // Only call commit on the index if it was provided for us
     if (!$blnIndexProvided) {
         $objIndex->commit();
     }
 }