Example #1
0
 /**
  * Create or update an indexed document
  *
  * @param object $object
  */
 public function index($object)
 {
     // create property accessor
     $accessor = PropertyAccess::createPropertyAccessor();
     // delete existing documents with same id
     foreach ($this->index->find('id:' . $accessor->getValue($object, 'id')) as $hit) {
         $this->index->delete($hit->id);
     }
     // create new Lucene document
     $doc = new Document();
     // add primary key to identify it in the search results
     $doc->addField(Field::keyword('id', $accessor->getValue($object, 'id')));
     // add entity class reference to identify it in the search results
     $doc->addField(Field::unIndexed('entityClass', get_class($object)));
     // analyze each property's annotations to see which ones must be add to the document
     $reflClass = new ReflectionClass($object);
     foreach ($reflClass->getProperties() as $property) {
         $reflProperty = new \ReflectionProperty($object, $property->name);
         $annotation = $this->reader->getPropertyAnnotation($reflProperty, '\\Keratine\\Lucene\\Mapping\\Annotation\\DocumentField');
         if ($annotation) {
             $value = $accessor->getValue($object, $property->name);
             $value = $this->ensureString($value);
             // use the appropriate indexing strategy for the field
             switch ($annotation->type) {
                 case 'keyword':
                     $doc->addField(Field::keyword($property->name, $value, 'UTF-8'));
                     break;
                 case 'unIndexed':
                     $doc->addField(Field::unIndexed($property->name, $value, 'UTF-8'));
                     break;
                 case 'binary':
                     $doc->addField(Field::binary($property->name, $value));
                     break;
                 case 'text':
                     $doc->addField(Field::text($property->name, $value, 'UTF-8'));
                     break;
                 case 'unStored':
                 default:
                     $doc->addField(Field::unStored($property->name, $value, 'UTF-8'));
                     break;
             }
         }
     }
     // add the document to the index and commit it
     $this->index->addDocument($doc);
     $this->index->commit();
 }
Example #2
0
 /**
  * Get binary search field
  * @param string $name
  * @param string $value
  * @return Field
  */
 public function binary($name, $value)
 {
     return Field::binary($name, $value);
 }
 private function appendToDocument(Lucene\Document $document, $name, $value)
 {
     if (is_string($value)) {
         $document->addField(Lucene\Document\Field::keyword($name, utf8_decode($value)));
     } elseif ($value instanceof \DateTime) {
         $document->addField(Lucene\Document\Field::keyword($name, $value->getTimestamp()));
     } elseif (is_array($value)) {
         $document->addField(Lucene\Document\Field::text($name, implode(',', $value)));
     } else {
         $document->addField(Lucene\Document\Field::binary($name, $value));
     }
 }