public function testUpdate()
 {
     $this->connection->shouldReceive('getIndex')->andReturn($luceneIndex = m::mock());
     $luceneIndex->shouldReceive('addDocument')->with(m::on(function ($arg) {
         $doc = new Document();
         $doc->addField(Field::keyword('primary_key', 1));
         $doc->addField(Field::Keyword('class_uid', '12345'));
         $field = Field::unStored('name', 'test name');
         $field->boost = 1;
         $doc->addField($field);
         $field = Field::unStored('optional_attribute1', 'optional value');
         $field->boost = 1;
         $doc->addField($field);
         $this->assertEquals($doc, $arg);
         return true;
     }))->once();
     $luceneIndex->shouldReceive('find')->with(m::on(function ($arg) {
         $term = new MultiTerm();
         $term->addTerm(new Term(1, 'primary_key'), true);
         $term->addTerm(new Term('12345', 'class_uid'), true);
         $this->assertEquals($term, $arg);
         return true;
     }))->andReturnUsing(function () {
         $hitMock = m::mock();
         $hitMock->id = 10;
         return [$hitMock];
     })->once();
     $luceneIndex->shouldReceive('delete')->with(10)->once();
     $index = $this->createIndex();
     $index->update($this->model);
 }
Example #2
0
 public function add(Searchable $obj)
 {
     // Get Primary Key
     $attributes = $obj->getSearchAttributes();
     $index = $this->getIndex();
     $doc = new \ZendSearch\Lucene\Document();
     // Add Meta Data fields
     foreach ($this->getMetaInfoArray($obj) as $fieldName => $fieldValue) {
         $doc->addField(\ZendSearch\Lucene\Document\Field::keyword($fieldName, $fieldValue));
     }
     // Add provided search infos
     foreach ($attributes as $key => $val) {
         $doc->addField(\ZendSearch\Lucene\Document\Field::Text($key, $val, 'UTF-8'));
     }
     // Add comments - if record is content
     if ($obj instanceof ContentActiveRecord) {
         $comments = "";
         foreach (Comment::findAll(['object_id' => $obj->getPrimaryKey(), 'object_model' => $obj->className()]) as $comment) {
             $comments .= " " . $comment->message;
         }
         $doc->addField(\ZendSearch\Lucene\Document\Field::Text('comments', $comments, 'UTF-8'));
     }
     if (\Yii::$app->request->isConsoleRequest) {
         print ".";
     }
     $index->addDocument($doc);
     $index->commit();
 }
 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));
     }
 }
Example #4
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();
 }
 public function index()
 {
     $oldReqUri = $_SERVER['REQUEST_URI'];
     $_SERVER['REQUEST_URI'] = '';
     $pageModel = new PageModel($this->indexer->getDB());
     $elementModel = new ElementModel($this->indexer->getDB());
     $searchModel = new SearchModel($this->indexer->getDB());
     $stmntPages = $this->indexer->getDB()->prepare("\n\t\t\tSELECT p.ID, p.language_codeFK lang, p.title, p.description, r.pattern, p.role\n\t\t\tFROM page p\n\t\t\tLEFT JOIN route r ON r.page_IDFK = p.ID\n\t\t\tWHERE r.ID IS NOT NULL\n\t\t");
     $resPages = $this->indexer->getDB()->select($stmntPages);
     $indexedPages = 0;
     foreach ($resPages as $p) {
         if ($p->role !== 'page') {
             echo "  Skipped page #" . $p->ID . ": reason -> unusable role: " . $p->role . PHP_EOL;
             continue;
         }
         $searchIndexInterface = $this->indexer->getIndex($p->lang);
         // Index page
         echo "  Indexing page #" . $p->ID . " into index \"" . $p->lang . "\": ";
         $cmsPage = $pageModel->getPageByID($p->ID);
         $elementTree = $elementModel->getElementTree($cmsPage);
         try {
             $searchableContent = $this->renderElementTreeRecursive($elementTree, $cmsPage->getLanguage());
         } catch (\Exception $e) {
             echo " Error -> " . $e->getMessage() . "\n";
             continue;
         }
         $searchDoc = new Document();
         $searchDoc->setInternalID($p->ID);
         $searchDoc->setLanguage($p->lang);
         $searchDoc->setTitle($p->title);
         $searchDoc->setDescription($searchableContent);
         $searchDoc->setPath($p->pattern);
         $searchDoc->setType('core_page');
         $docID = $searchModel->saveDocument($searchDoc);
         $luceneDocument = new \ZendSearch\Lucene\Document();
         $luceneDocument->addField(Field::keyword('ID', $docID));
         $luceneDocument->addField(Field::unStored('content', $searchableContent));
         $luceneDocument->addField(Field::unStored('description', $p->description));
         $searchIndexInterface->addDocument($luceneDocument);
         echo "done";
         echo "\n";
         ++$indexedPages;
     }
     $_SERVER['REQUEST_URI'] = $oldReqUri;
     echo "  Total indexed pages: " . $indexedPages . "\n";
 }
Example #6
0
 /**
  * Add index
  * @param integer $id
  * @param SearchCollection $index
  */
 public function add($id, SearchCollection $index)
 {
     if ($this->config()->exists('zend_search', 'index')) {
         $document = new Document();
         $document->addField(Field::keyword('id', $id));
         foreach ($index as $field) {
             $document->addField($field);
         }
         $this->index()->addDocument($document);
         $this->index()->commit();
     }
 }
Example #7
0
File: Exam.php Project: ubc/examdb
 public function index($indexer, $commit = true, $optimize = true)
 {
     $document = new Document();
     $document->addField(Field::keyword('pk', $this->getId()));
     $document->addField(Field::Text('course', $this->getSubjectcode()));
     $document->addField(Field::Text('cross-listed', str_replace(array(';', ',', '|'), ' ', $this->getCrossListed())));
     $document->addField(Field::Text('instructor', $this->getLegalContentOwner()));
     $document->addField(Field::Unstored('comments', $this->getComments()));
     $indexer->addDocument($document);
     if ($commit) {
         $indexer->commit();
     }
     if ($optimize) {
         $indexer->optimize();
     }
 }
 public function generateSearchAction()
 {
     $searchIndexLocation = $this->getIndexLocation();
     $index = Lucene\Lucene::create($searchIndexLocation);
     $allUsers = $this->getUserTable()->fetchAll(false);
     foreach ($allUsers as $user) {
         $id = Document\Field::keyword('userId', $user->userId);
         $firstName = Document\Field::text('firstName', $user->firstName);
         $lastName = Document\Field::text('lastName', $user->lastName);
         $email = Document\Field::text('email', $user->email);
         $role = Document\Field::text('role', $user->role);
         $activated = Document\Field::keyword('activated', $user->activated);
         $indexDoc = new Lucene\Document();
         $indexDoc->addField($id);
         $indexDoc->addField($firstName);
         $indexDoc->addField($lastName);
         $indexDoc->addField($email);
         $indexDoc->addField($role);
         $indexDoc->addField($activated);
         $index->addDocument($indexDoc);
     }
     $index->commit();
 }
 /**
  * Update document in index for model
  *
  * @param Model $model
  */
 public function update(Model $model)
 {
     // Remove any existing documents for model.
     $this->delete($model);
     // Create new document for model.
     $doc = new Document();
     list($name, $value) = $this->config->primaryKeyPair($model);
     // Add private key.
     $doc->addField(Field::keyword($name, $value));
     // Add model's class UID.
     list($name, $value) = $this->config->classUidPair($model);
     // Add class uid for identification of model's class.
     $doc->addField(Field::Keyword($name, $value));
     // Get base fields.
     $fields = $this->config->fields($model);
     // Add fields to document to be indexed (but not stored).
     foreach ($fields as $fieldName => $options) {
         $fieldValue = $model->{trim($fieldName)};
         $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)));
         $field->boost = array_get($options, 'boost');
         $doc->addField($field);
     }
     // Get dynamic fields.
     $optionalAttributes = $this->config->optionalAttributes($model);
     // Add optional attributes to document to be indexed (but not stored).
     foreach ($optionalAttributes as $fieldName => $options) {
         $fieldValue = array_get($options, "value");
         $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)));
         $field->boost = array_get($options, "boost");
         $doc->addField($field);
     }
     // Set boost for model.
     $doc->boost = $this->config->boost($model);
     // Add document to index.
     $this->index()->addDocument($doc);
 }
 public function addAetCommunicationToSearchIndex($index, Communication $aetCommunication)
 {
     // Create a new document
     $document = new Document();
     $document->addField(Field::keyword('dbId', $aetCommunication->getId(), 'utf-8'));
     $document->addField(Field::unStored('title', $aetCommunication->getTitle(), 'utf-8'));
     $document->addField(Field::unStored('shortdesc', $aetCommunication->getShortDesc(), 'utf-8'));
     $document->addField(Field::unStored('body', html_entity_decode(strip_tags($aetCommunication->getBody()), ENT_SUBSTITUTE, 'UTF-8'), 'utf-8'));
     $document->addField(Field::unStored('author', $aetCommunication->getUser()->getFirstname() . " " . $aetCommunication->getUser()->getLastname(), 'utf-8'));
     // Add your document to the index
     $index->addDocument($document);
     // Commit your change
     $index->commit();
     $index->optimize();
 }
Example #11
0
 /**
  * create document from configured fields within extracted data
  * @param string $url
  * @param array $page
  * @return Document
  */
 protected function createDocument($url, $page)
 {
     $document = new Document();
     if (!isset($page['status_code'])) {
         $page['status_code'] = 00;
         //tmp
     }
     setlocale(LC_ALL, "cs_CZ.UTF-8");
     $document->addField(Field::keyword('url', $url));
     // ancestor URLs to search by URL
     $urlParts = parse_url($url);
     if (isset($urlParts['path']) && $urlParts['path'] && strlen($urlParts['path']) > 1) {
         $uri = $urlParts['path'];
         $uris = array($uri);
         do {
             $uri = substr($uri, 0, strrpos($uri, '/'));
             $uris[] = $uri;
         } while (strrpos($uri, '/') > 1);
         $document->addField(Field::text(Page::URIS_KEY, implode(' ', $uris)));
     }
     foreach (array(Page::TITLE_KEY, Page::DESCRIPTION_KEY, Page::BODY_KEY, Page::IMAGE_KEY) as $fieldName) {
         $fieldValue = isset($page[$fieldName]) ? $page[$fieldName] : '';
         switch ($fieldName) {
             case Page::TITLE_KEY:
             case Page::DESCRIPTION_KEY:
             case Page::BODY_KEY:
                 $field = Field::text($fieldName, $fieldValue);
                 // translit
                 $fieldTranslit = Field::text($fieldName . '_translit', str_replace("'", '', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $fieldValue)));
                 $fieldTranslit->boost = isset($this->parameters[self::BOOST_PARAM][$fieldName]) ? $this->parameters[self::BOOST_PARAM][$fieldName] : 1.25;
                 $document->addField($fieldTranslit);
                 break;
             case Page::IMAGE_KEY:
                 $field = Field::unIndexed($fieldName, $fieldValue);
                 break;
             default:
                 $translitValue = str_replace("'", '', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $fieldValue));
                 $field = Field::text($fieldName, $fieldValue . ($translitValue != $fieldValue ? ' ' . $translitValue : ''));
         }
         $field->boost = isset($this->parameters[self::BOOST_PARAM][$fieldName]) ? $this->parameters[self::BOOST_PARAM][$fieldName] : 1.25;
         $document->addField($field);
     }
     // title tags as configured i.e. h1, h2, ...
     foreach ($this->parameters[self::TITLE_TAGS_PARAM] as $fieldName) {
         $fieldValue = Page::hasHeadlineType($page, $fieldName) ? Page::getHeadline($page, $fieldName) : '';
         $field = Field::text($fieldName, $fieldValue);
         $field->boost = isset($this->parameters[self::BOOST_PARAM][$fieldName]) ? $this->parameters[self::BOOST_PARAM][$fieldName] : 1;
         $document->addField($field);
         $fieldTranslit = Field::text($fieldName . '_translit', str_replace("'", '', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $fieldValue)));
         $fieldTranslit->boost = isset($this->parameters[self::BOOST_PARAM][$fieldName]) ? $this->parameters[self::BOOST_PARAM][$fieldName] : 1.25;
         $document->addField($fieldTranslit);
     }
     // page ID if selector defined
     if ($this->parameters[self::PAGE_ID_PARAM]) {
         $fieldValue = isset($page[Page::PAGE_ID_KEY]) ? $page[Page::PAGE_ID_KEY] : '';
         $field = Field::unIndexed(Page::PAGE_ID_KEY, $fieldValue);
         $document->addField($field);
     }
     // route name if selector defined
     if ($this->parameters[self::ROUTE_NAME_PARAM]) {
         $fieldValue = isset($page[Page::ROUTE_NAME_KEY]) ? $page[Page::ROUTE_NAME_KEY] : '';
         $field = Field::unIndexed(Page::ROUTE_NAME_KEY, $fieldValue);
         $document->addField($field);
     }
     return $document;
 }
 /**
  * Update document in index for model
  *
  * @param Model $model
  */
 public function update(Model $model)
 {
     // Remove any existing documents for model.
     $this->delete($model);
     // Create new document for model.
     $doc = new Document();
     list($name, $value) = $this->config->privateKeyPair($model);
     // Add private key.
     $doc->addField(Field::keyword($name, $value));
     // Add model's class UID.
     list($name, $value) = $this->config->classUidPair($model);
     // Add class uid for identification of model's class.
     $doc->addField(Field::Keyword($name, $value));
     $fields = $this->config->fields($model);
     // Add fields to document to be indexed (but not stored).
     foreach ($fields as $field) {
         $doc->addField(Field::unStored(trim($field), strip_tags(trim($model->{trim($field)}))));
     }
     $optionalAttributes = $this->config->optionalAttributes($model);
     // Add optional attributes to document to be indexed (but not stored).
     foreach ($optionalAttributes as $fieldName => $fieldValue) {
         $doc->addField(Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))));
     }
     // Add document to index.
     $this->index()->addDocument($doc);
 }
Example #13
0
 /**
  * Add a new document to the index.
  * Any existing document with the given $id should be deleted first.
  * $fields should be indexed but not necessarily stored in the index.
  * $parameters should be stored in the index but not necessarily indexed.
  *
  * @param mixed $id
  * @param array $fields
  * @param array $parameters
  * 
  * @return bool
  */
 public function insert($id, array $fields, array $parameters = array())
 {
     // Remove any existing documents.
     $this->delete($id);
     // Create new document.
     $doc = new \ZendSearch\Lucene\Document();
     // Add id parameters.
     $doc->addField(\ZendSearch\Lucene\Document\Field::keyword('xref_id', $id));
     // Add fields to document to be indexed and stored.
     foreach ($fields as $field => $value) {
         if (is_array($value)) {
             $value = implode(' ', $value);
         }
         $doc->addField(\ZendSearch\Lucene\Document\Field::text(trim($field), trim($value)));
     }
     // Add parameters to document to be stored (but not indexed).
     $doc->addField(\ZendSearch\Lucene\Document\Field::unIndexed('_parameters', base64_encode(json_encode($parameters))));
     // Add document to index.
     $this->getIndex()->addDocument($doc);
     return true;
 }
 protected function createDocument($title, $body, $url)
 {
     $document = new Document();
     $document->addField(Field::text('title', $title));
     $document->addField(Field::text('body', strip_tags($body)));
     $document->addField(Field::keyword('url', $url));
     return $document;
 }
Example #15
0
 /**
  * Get keyword search field
  * @param string $name
  * @param string $value
  * @return Field
  */
 public function keyword($name, $value)
 {
     Field::keyword($name, $value, $this->config()->getCharset());
 }
 public function addAetUserToSearchIndex($index, User $aetUser)
 {
     // Create a new document
     $document = new Document();
     $document->addField(Field::keyword('dbId', $aetUser->getId(), 'utf-8'));
     $document->addField(Field::unStored('firstname', $aetUser->getFirstname(), 'utf-8'));
     $document->addField(Field::unStored('lastname', $aetUser->getLastname(), 'utf-8'));
     $document->addField(Field::unStored('activiteprincipale', $aetUser->getActivitePrincipale(), 'utf-8'));
     $document->addField(Field::unStored('codepostal', $aetUser->getCodePostale(), 'utf-8'));
     $document->addField(Field::unStored('email', $aetUser->getEmail(), 'utf-8'));
     $document->addField(Field::unStored('matricule', $aetUser->getMatricule(), 'utf-8'));
     $document->addField(Field::unStored('pays', $aetUser->getPays(), 'utf-8'));
     $document->addField(Field::unStored('promotion', strval($aetUser->getPromotion()->format("Y")), 'utf-8'));
     $document->addField(Field::unStored('telephone', strval($aetUser->getTelephone()), 'utf-8'));
     $document->addField(Field::unStored('ville', $aetUser->getVille(), 'utf-8'));
     $document->addField(Field::unStored('whoami', $aetUser->getWhoami(), 'utf-8'));
     // Add your document to the index
     $index->addDocument($document);
     // Commit your change
     $index->commit();
     $index->optimize();
 }