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); }
/** * 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"; }
/** * 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(); }
/** * 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); }
/** * 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 (but not stored). foreach ($fields as $field => $value) { if (is_array($value)) { continue; } $doc->addField(\ZendSearch\Lucene\Document\Field::unStored(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; }
/** * Обновить индекс для объекта ISearchable * * @param ISearchable $item */ public function updateIndex(ISearchable $item) { // получаем доступ собственно к индексу $index = $this->connection; // удаляем старый индекс $this->deleteIndex($item); // недоступные не индексируем if (!$item->isAvailableForIndexing()) { return; } $document = new Document(); // сохраняем первичный ключ модели для идентификации ее в результатах поиска $document->addField(Field::Keyword('pk', $item->getId())); // id моделей могут пересекаться (например, у продуктов и услуг), // поэтому добавим второе поле для однозначной идентификации $document->addField(Field::Keyword('model', get_class($item))); // индексируем поля модели foreach ($item->getAttributesForIndexing() as $attribute) { $field = $attribute->getFieldName(); $value = $attribute->getValue(); $document->addField(Field::unStored($field, strip_tags($value))); } // добавляем запись в индекс $index->addDocument($document); $index->commit(); }
/** * Get un stored search field * @param string $name * @param string $value * @return Field */ public function unStored($name, $value) { return Field::unStored($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(); }