Assembla home | Assembla project page
 

Changeset 7

Show
Ignore:
Timestamp:
09/30/07 12:46:41 (11 months ago)
Author:
Norm2782
Message:

Re-worked search classes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Intermentis/Search/Indexable.php

    r6 r7  
    2626{ 
    2727        /** 
    28          * Get an array with the following construction: 
    29          *  
    30          * array( 
    31          *     'fieldname' => array( 
    32          *         'type'  => Intermentis_Search_Lucene_Indexer::FIELD_TEXT, 
    33          *         'value' => 'foo' 
    34          *     ), 
    35          *     ... 
    36          * ) 
    37          *  
    38          * @return array 
     28         * Gets a complete search document used for indexing. 
     29         * 
     30         * @return Zend_Search_Lucene_Document 
    3931         */ 
    40         public function getIndexFields(); 
     32        public function getSearchDocument(); 
    4133} 
  • trunk/Intermentis/Search/Lucene/Indexer.php

    r6 r7  
    2525class Intermentis_Search_Lucene_Indexer 
    2626{ 
    27         /** 
    28      * Keyword field type 
    29      * 
    30      * Stored, Indexed 
    31      * 
    32      */ 
    33     const FIELD_KEYWORD = 'keyword'; 
    34  
    35     /** 
    36      * Unindexed field type 
    37      * 
    38      * Stored 
    39      * 
    40      */ 
    41     const FIELD_UNINDEXED = 'unindexed'; 
    42  
    43     /** 
    44      * Binary field type 
    45      * 
    46      * Stored, Binary 
    47      * 
    48      */ 
    49     const FIELD_BINARY = 'binary'; 
    50  
    51     /** 
    52      * Text field type 
    53      * 
    54      * Stored, Indexed, Tokenized 
    55      * 
    56      */ 
    57     const FIELD_TEXT = 'text'; 
    58  
    59     /** 
    60      * Unstored field type 
    61      * 
    62      * Indexed, Tokenized 
    63      * 
    64      */ 
    65     const FIELD_UNSTORED = 'unstored'; 
    66  
    6727    /** 
    6828     * The search index 
     
    8545     * Remove a record from the search index 
    8646     * 
    87      * @param int $id 
     47     * @param string $searchField 
     48     * @param string $value 
    8849     * @return Intermentis_Search_Lucene_Indexer 
    8950     */ 
    90     public function delete($id
     51    public function delete($searchField, $value
    9152    { 
    92             $hits = $this->_index->find('id:' . $id); 
     53            $hits = $this->_index->find($searchField . ':' . $value); 
    9354             
    9455                foreach ($hits as $hit) { 
     
    10061     
    10162    /** 
    102      * Index an Intermentis_Search_Indexable. 
    103      * The $fields array specifies which fields need to be indexed as what. 
    104      *  
    105      * Format: 
    106      *  
    107      * array( 
    108      *     'fieldname' => array( 
    109      *         'type'  => Intermentis_Search_Lucene_Indexer::FIELD_TEXT, 
    110      *         'value' => 'foo' 
    111      *     ), 
    112      *     ... 
    113      * ) 
     63     * First deletes and then re-adds the model. 
    11464     * 
    11565     * @param Intermentis_Search_Indexable $model 
    116      * @param array $fields 
     66     * @param string $searchField 
     67     * @param string $value 
    11768     * @return Intermentis_Search_Lucene_Indexer 
    11869     */ 
    119         public function index(Intermentis_Search_Indexable $model, array $fields = null) 
     70    public function update(Intermentis_Search_Indexable $model, $searchField, $value) 
     71    { 
     72        return $this->delete($searchField, $value) 
     73                    ->index($model); 
     74    } 
     75     
     76    /** 
     77     * Index an Intermentis_Search_Indexable. 
     78     * 
     79     * @param Intermentis_Search_Indexable $model 
     80     * @return Intermentis_Search_Lucene_Indexer 
     81     */ 
     82        public function index(Intermentis_Search_Indexable $model) 
    12083        { 
    121                 $document = new Zend_Search_Lucene_Document(); 
    122                  
    123                 if ($fields == null) { 
    124                         $fields = $model->getIndexFields(); 
    125                 } 
    126                  
    127                 foreach ($fields as $fieldName => $fieldData) { 
    128                         if ($fieldName == 'id') { 
    129                                 $fieldName = 'primaryKeyID'; 
    130                         } 
    131                          
    132                         switch ($fieldData['type']) { 
    133                                 case Intermentis_Search_Lucene_Indexer::FIELD_BINARY: 
    134                                         $field = Zend_Search_Lucene_Field::Binary($fieldName, $fieldData['value']); 
    135                                         break; 
    136                                          
    137                                 case Intermentis_Search_Lucene_Indexer::FIELD_KEYWORD: 
    138                                     $field = Zend_Search_Lucene_Field::Keyword($fieldName, $fieldData['value']); 
    139                                     break; 
    140                                      
    141                                 case Intermentis_Search_Lucene_Indexer::FIELD_TEXT: 
    142                                         $field = Zend_Search_Lucene_Field::Text($fieldName, $fieldData['value']); 
    143                                     break; 
    144                                      
    145                                 case Intermentis_Search_Lucene_Indexer::FIELD_UNINDEXED: 
    146                                         $field = Zend_Search_Lucene_Field::UnIndexed($fieldName, $fieldData['value']); 
    147                                         break; 
    148                                          
    149                         case Intermentis_Search_Lucene_Indexer::FIELD_UNSTORED: 
    150                                 $field = Zend_Search_Lucene_Field::UnStored($fieldName, $fieldData['value']); 
    151                                 break; 
    152                                          
    153                                 default: 
    154                                     throw new Intermentis_Exception('Field type not recognized'); 
    155                                         break; 
    156                         } 
    157                          
    158                         $document->addField($field); 
    159                 } 
    160                  
    161                 $this->_index->addDocument($document); 
     84                $this->_index->addDocument($model->getSearchDocument()); 
    16285                 
    16386                return $this;