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();
    }
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();
    }
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 ( ) ; }
Create or update an indexed document

@param object $object
Create or update an indexed document