/** * php index.php db index * */ public function index() { $query = "SELECT * FROM Products AS p JOIN Categories AS c ON p.CategoryID = c.CategoryId JOIN Suppliers AS s ON p.SupplierID = s.SupplierID"; $stmt = $this->db->prepare($query); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $indexDir = APP_PATH . '/' . self::INDEX_DIR; is_dir($indexDir) || mkdir($indexDir, 0777, true); $index = self::create($indexDir); foreach ($rows as $row) { $doc = new Zend_Search_Lucene_Document(); $doc->addField(Zend_Search_Lucene_Field::keyword('ProductName', $row['ProductName'])); $doc->addField(Zend_Search_Lucene_Field::text('Quantity', $row['QuantityPerUnit'])); $doc->addField(Zend_Search_Lucene_Field::keyword('Category', $row['CategoryName'])); $doc->addField(Zend_Search_Lucene_Field::unIndexed('Description', $row['Description'])); $doc->addField(Zend_Search_Lucene_Field::unStored('City', $row['City'])); $doc->addField(Zend_Search_Lucene_Field::keyword('CompanyName', $row['CompanyName'])); $doc->addField(Zend_Search_Lucene_Field::binary('Picture', $row['Picture'])); $index->addDocument($doc); } }
/** * addToIndex * @param string $strIndexPath * @param string $strKey * @author Thomas Schedler <*****@*****.**> * @version 1.0 */ protected final function addToIndex($strIndexPath, $strKey) { try { if (!is_object($this->objIndex) || !$this->objIndex instanceof Zend_Search_Lucene) { if (count(scandir($strIndexPath)) > 2) { $this->objIndex = Zend_Search_Lucene::open($strIndexPath); } else { $this->objIndex = Zend_Search_Lucene::create($strIndexPath); } } $objDoc = new Zend_Search_Lucene_Document(); $objDoc->addField(Zend_Search_Lucene_Field::keyword('key', $strKey)); $objDoc->addField(Zend_Search_Lucene_Field::unIndexed('date', $this->setup->getPublishDate('d.m.Y'))); $objDoc->addField(Zend_Search_Lucene_Field::unIndexed('rootLevelId', $this->setup->getRootLevelId())); /** * index fields */ foreach ($this->setup->FieldNames() as $strField => $intFieldType) { $objField = $this->setup->getField($strField); if (is_object($objField) && $objField->idSearchFieldTypes != Search::FIELD_TYPE_NONE) { $strValue = ''; if (is_array($objField->getValue()) && $objField->sqlSelect != '') { $arrIds = $objField->getValue(); $sqlSelect = $objField->sqlSelect; if (is_array($arrIds)) { if (count($arrIds) > 0) { $strReplaceWhere = ''; foreach ($arrIds as $strId) { $strReplaceWhere .= $strId . ','; } $strReplaceWhere = trim($strReplaceWhere, ','); $objReplacer = new Replacer(); $sqlSelect = $objReplacer->sqlReplacer($sqlSelect, $this->setup->getLanguageId(), $this->setup->getRootLevelId(), ' AND tbl.id IN (' . $strReplaceWhere . ')'); $objCategoriesData = $this->core->dbh->query($sqlSelect)->fetchAll(Zend_Db::FETCH_OBJ); if (count($objCategoriesData) > 0) { foreach ($objCategoriesData as $objCategories) { $strValue .= $objCategories->title . ', '; } $strValue = rtrim($strValue, ', '); } } } } else { $strValue = $objField->getValue(); } if ($strValue != '') { switch ($objField->idSearchFieldTypes) { case Search::FIELD_TYPE_KEYWORD: $objDoc->addField(Zend_Search_Lucene_Field::keyword($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_UNINDEXED: $objDoc->addField(Zend_Search_Lucene_Field::unIndexed($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_BINARY: $objDoc->addField(Zend_Search_Lucene_Field::binary($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_TEXT: $objDoc->addField(Zend_Search_Lucene_Field::text($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_UNSTORED: $objDoc->addField(Zend_Search_Lucene_Field::unStored($strField, strip_tags($strValue), $this->core->sysConfig->encoding->default)); break; } } } } // Add document to the index. $this->objIndex->addDocument($objDoc); $this->objIndex->optimize(); } catch (Exception $exc) { $this->core->logger->err($exc); } }
/** * indexFieldNow * @param GenericElementField $objField * @param string $strField * @param integer $intFieldType * @param string|array|object $mixedFieldValue * @param Zend_Search_Lucene_Document $objDoc * @return void * @author Thomas Schedler <*****@*****.**> */ protected final function indexFieldNow($objField, $strField, $intFieldType, $mixedFieldValue, Zend_Search_Lucene_Document &$objDoc) { try { $strValue = ''; $strValueIds = ''; if ($objField->typeId == GenericSetup::FIELD_TYPE_ID_TAG) { $mixedValue = $mixedFieldValue; if (is_object($mixedValue) || is_array($mixedValue)) { foreach ($mixedValue as $objTag) { $strValue .= $objTag->title . ', '; $strValueIds .= '[' . $objTag->id . ']'; } $strValue = rtrim($strValue, ', '); } } elseif (!is_object($mixedFieldValue) && $objField->sqlSelect != '') { $sqlSelect = $objField->sqlSelect; $arrIds = array(); if (is_array($mixedFieldValue)) { $arrIds = $mixedFieldValue; } else { if ($mixedFieldValue != '') { if (strpos($mixedFieldValue, '[') !== false) { $mixedFieldValue = trim($mixedFieldValue, '[]'); $arrIds = explode('][', $mixedFieldValue); } else { $arrIds = array($mixedFieldValue); } } } if (is_array($arrIds)) { if (count($arrIds) > 0) { $strReplaceWhere = ''; foreach ($arrIds as $strId) { $strReplaceWhere .= $strId . ','; } $strReplaceWhere = trim($strReplaceWhere, ','); $objReplacer = new Replacer(); $sqlSelect = $objReplacer->sqlReplacer($sqlSelect, $this->setup->getLanguageId(), $this->setup->getRootLevelId(), ' AND tbl.id IN (' . $strReplaceWhere . ')'); $objCategoriesData = $this->core->dbh->query($sqlSelect)->fetchAll(Zend_Db::FETCH_OBJ); if (count($objCategoriesData) > 0) { foreach ($objCategoriesData as $objCategories) { $strValue .= $objCategories->title . ', '; $strValueIds .= '[' . $objCategories->id . ']'; } $strValue = rtrim($strValue, ', '); } } } } else { $strValue = html_entity_decode($mixedFieldValue, ENT_COMPAT, $this->core->sysConfig->encoding->default); } if (is_string($strValue) && $strValue != '') { if ($intFieldType == GenericSetup::FILE_FIELD) { $objFiles = $this->getModelFiles()->loadFilesById($strValue); $arrValues = array(); if (count($objFiles) > 0) { foreach ($objFiles as $objFile) { $arrValues[] = array('path' => $objFile->path, 'filename' => $objFile->filename, 'version' => $objFile->version); } } $strValueIds = $strValue; $strValue = serialize($arrValues); } if ($strValueIds != '') { $objDoc->addField(Zend_Search_Lucene_Field::unIndexed($strField . 'Ids', $strValueIds, $this->core->sysConfig->encoding->default)); } $this->core->logger->debug($strField . ': ' . $strValue); switch ($objField->idSearchFieldTypes) { case Search::FIELD_TYPE_KEYWORD: $objDoc->addField(Zend_Search_Lucene_Field::keyword($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_UNINDEXED: $objDoc->addField(Zend_Search_Lucene_Field::unIndexed($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_BINARY: $objDoc->addField(Zend_Search_Lucene_Field::binary($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_TEXT: $objDoc->addField(Zend_Search_Lucene_Field::text($strField, $strValue, $this->core->sysConfig->encoding->default)); break; case Search::FIELD_TYPE_UNSTORED: $objDoc->addField(Zend_Search_Lucene_Field::unStored($strField, strip_tags($strValue), $this->core->sysConfig->encoding->default)); break; } } } catch (Exception $exc) { $this->core->logger->err($exc); } }
/** * converts the map into a document so Lucene can save it * * @param MingoTable $table * @param array $map the raw map passed to {@link insert()} or {@link update()} * @return Zend_Search_Lucene_Document */ protected function normalizeMap(MingoTable $table, array $map) { $document = new Zend_Search_Lucene_Document(); // add some fields that will be present in all documents... $document->addField(Zend_Search_Lucene_Field::unStored('_id', $map['_id'])); $document->addField(Zend_Search_Lucene_Field::binary('body', $this->getBody($map))); // add all the indexes into the document... foreach ($table->getIndexes() as $index) { foreach ($index->getFields() as $name => $options) { // use array_key... to account for null values... if (array_key_exists($name, $map)) { $val = null; if (is_array($map[$name])) { $val = join(' ', $map[$name]); } else { $val = $map[$name]; } //if/else $document->addField(Zend_Search_Lucene_Field::UnStored($name, $val)); // let's not try and add it twice... unset($map[$name]); } //if } //foreach } //foreach return $document; }