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(); }
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"; }
/** * 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; }
private function indexConnection($created, $data) { $doc = new ZendSearch\Lucene\Document(); $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('created', $created)); $doc->addField(ZendSearch\Lucene\Document\Field::Text('version', $data['version'])); if (!empty($data['site'])) { if (!empty($data['site']['connect_site_title'])) { $doc->addField(ZendSearch\Lucene\Document\Field::Text('title', $data['site']['connect_site_title'])); } if (!empty($data['site']['connect_site_url'])) { $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('url', $data['site']['connect_site_url'])); } if (!empty($data['site']['connect_site_email'])) { $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('email', $data['site']['connect_site_email'])); // hmm } if (!empty($data['site']['connect_site_keywords'])) { $doc->addField(ZendSearch\Lucene\Document\Field::Text('keywords', $data['site']['connect_site_keywords'])); } if (!empty($data['site']['connect_site_location'])) { $loc = TikiLib::lib('geo')->parse_coordinates($data['site']['connect_site_location']); if (count($loc) > 1) { $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('geo_lat', $loc['lat'])); $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('geo_lon', $loc['lon'])); if (count($loc) > 2) { $doc->addField(ZendSearch\Lucene\Document\Field::Keyword('geo_zoom', $loc['zoom'])); } } } } else { $doc->addField(ZendSearch\Lucene\Document\Field::Text('title', tra('Anonymous'))); } if (!empty($data['tables'])) { $doc->addField(ZendSearch\Lucene\Document\Field::UnIndexed('tables', serialize($data['tables']))); } if (!empty($data['prefs'])) { $doc->addField(ZendSearch\Lucene\Document\Field::UnIndexed('prefs', serialize($data['prefs']))); if (!empty($data['prefs']['language'])) { $langLib = TikiLib::lib('language'); $languages = $langLib->get_language_map(); $doc->addField(ZendSearch\Lucene\Document\Field::Text('language', $languages[$data['prefs']['language']])); } } if (!empty($data['server'])) { $doc->addField(ZendSearch\Lucene\Document\Field::UnIndexed('server', serialize($data['server']))); } if (!empty($data['votes'])) { $doc->addField(ZendSearch\Lucene\Document\Field::UnIndexed('votes', serialize($data['votes']))); } return $doc; }
private function generateDocument($data) { $document = new ZendSearch\Lucene\Document(); $typeMap = array('Search_Type_WikiText' => 'UnStored', 'Search_Type_PlainText' => 'UnStored', 'Search_Type_Whole' => 'Keyword', 'Search_Type_Numeric' => 'Keyword', 'Search_Type_Timestamp' => 'Keyword', 'Search_Type_MultivalueText' => 'UnStored', 'Search_Type_ShortText' => 'Text'); foreach ($data as $key => $value) { $luceneType = $typeMap[get_class($value)]; $field = ZendSearch\Lucene\Document\Field::$luceneType($key, $value->getValue(), 'UTF-8'); $document->addField($field); } return $document; }