public function search() { //if search with OSS has not been activated yet in module configuration page OSS is not used $searchEnabled = OpensearchserverConfigQuery::read('enable_search'); if (!$searchEnabled) { //display results return $this->render('search'); } //get keywords $request = $this->getRequest(); $keywords = $request->query->get('q', null); $sort = $request->query->get('order', null); $page = $request->query->get('page', 1); $limit = $request->query->get('limit', 8); $offset = $limit == 100000 ? 0 : ($page - 1) * $limit; //get locale $locale = $request->getSession()->getLang()->getLocale(); //fix bug with FR locale? if ($locale == 'fr_FR') { $locale = array('fr_FR', 'fr_Fr'); $lang = \OpenSearchServer\Request::LANG_FR; } if (!isset($lang)) { switch ($locale) { case 'en_EN': case 'en_US': $lang = \OpenSearchServer\Request::LANG_EN; break; case 'es_ES': $lang = \OpenSearchServer\Request::LANG_ES; break; case 'it_IT': $lang = \OpenSearchServer\Request::LANG_IT; break; case 'ru_RU': $lang = \OpenSearchServer\Request::LANG_RU; break; default: $lang = \OpenSearchServer\Request::LANG_UNDEFINED; break; } } $index = OpensearchserverConfigQuery::read('index_name'); $queryTemplate = OpensearchserverConfigQuery::read('query_template'); //create handler for requests $oss_api = \OpenSearchServerSearch\Helper\OpenSearchServerSearchHelper::getHandler(); //create search request $request = new \OpenSearchServer\Search\Field\Search(); $request->index($index)->template($queryTemplate)->start($offset)->rows($limit)->lang($lang)->filterField('locale', $locale, \OpenSearchServer\Request::OPERATOR_OR)->enableLog()->query($keywords); //handle sorting switch ($sort) { case 'alpha': $request->sort('titleSort', \OpenSearchServer\Search\Search::SORT_ASC); break; case 'alpha_reverse': $request->sort('titleSort', \OpenSearchServer\Search\Search::SORT_DESC); break; case 'min_price': $request->sort('price', \OpenSearchServer\Search\Search::SORT_ASC); break; case 'max_price': $request->sort('price', \OpenSearchServer\Search\Search::SORT_DESC); break; } //send query $response = $oss_api->submit($request); $ids = array(); foreach ($response->getResults() as $result) { $ids[] = $result->getField('id'); } //number of pages $numberOfPages = $limit > 0 ? round($response->getTotalNumberFound() / $limit) : 1; //display results return $this->render('oss_results', array('module_code' => 'OpenSearchServerSearch', 'keywords' => $keywords, 'total' => $response->getTotalNumberFound(), 'results' => $response->getResults(), 'ids' => implode(',', $ids), 'numberOfPages' => $numberOfPages)); }
/** * Returns a new ChildOpensearchserverConfigQuery object. * * @param string $modelAlias The alias of a model in the query * @param Criteria $criteria Optional Criteria to build the query from * * @return ChildOpensearchserverConfigQuery */ public static function create($modelAlias = null, $criteria = null) { if ($criteria instanceof \OpenSearchServerSearch\Model\OpensearchserverConfigQuery) { return $criteria; } $query = new \OpenSearchServerSearch\Model\OpensearchserverConfigQuery(); if (null !== $modelAlias) { $query->setModelAlias($modelAlias); } if ($criteria instanceof Criteria) { $query->mergeWith($criteria); } return $query; }
/** * Delete all products */ private function deleteAllAction() { // Get name of index and handler to work with OSS API $index = OpensearchserverConfigQuery::read('index_name'); $oss_api = OpenSearchServerSearchHelper::getHandler(); //delete every documents from index $request = new \OpenSearchServer\Document\DeleteByQuery(); $request->index($index)->query('id:[* TO *]'); $response = $oss_api->submit($request); $this->getRequest()->getSession()->getFlashBag()->add('oss', $this->getTranslator()->trans('All data have been deleted.', [], OpenSearchServerSearch::MODULE_DOMAIN)); return $this->redirectToHome(); }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see OpensearchserverConfig::setDeleted() * @see OpensearchserverConfig::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(OpensearchserverConfigTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildOpensearchserverConfigQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
public static function deleteProduct(Product $product) { /************************************ * Get name of index and handler to work with OSS API ************************************/ $index = OpensearchserverConfigQuery::read('index_name'); $oss_api = OpenSearchServerSearchHelper::getHandler(); //delete every versions of this product (all locales) $request = new \OpenSearchServer\Document\Delete(); $request->index($index)->field('id')->value($product->getId()); $response = $oss_api->submit($request); return $response->isSuccess(); }
protected function buildForm() { $this->formBuilder->add('hostname', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('OpenSearchServer instance URL'), 'data' => OpensearchserverConfigQuery::read('hostname', 'http://localhost:9090'), 'label_attr' => array('for' => 'hostname', 'help' => $this->trans('URL to access your OpenSearchServer instance. Please include port information.'))))->add('login', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Login'), 'data' => OpensearchserverConfigQuery::read('login', ''), 'label_attr' => array('for' => 'login')))->add('apikey', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('API Key'), 'data' => OpensearchserverConfigQuery::read('apikey', ''), 'label_attr' => array('for' => 'apikey')))->add('index_name', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Index to use'), 'data' => OpensearchserverConfigQuery::read('index_name', ''), 'label_attr' => array('for' => 'index_name', 'help' => $this->trans('Index will be automatically created if it does not already exist.'))))->add('query_template', 'text', array('constraints' => array(new NotBlank()), 'required' => true, 'label' => $this->trans('Query template'), 'data' => OpensearchserverConfigQuery::read('query_template', 'search'), 'label_attr' => array('for' => 'query_template', 'help' => $this->trans('Query template to use for searching products. If it does not already exists in index it will be automatically created from a basic template.'))))->add('enable_search', 'checkbox', array('required' => false, 'label' => $this->trans('Enable search with OpenSearchServer'), 'data' => (bool) OpensearchserverConfigQuery::read('enable_search', ''), 'label_attr' => array('for' => 'enable_search', 'help' => $this->trans('Enable this option when configuration is completed.')))); }
/** * Performs an INSERT on the database, given a OpensearchserverConfig or Criteria object. * * @param mixed $criteria Criteria or OpensearchserverConfig object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(OpensearchserverConfigTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from OpensearchserverConfig object } // Set the correct dbName $query = OpensearchserverConfigQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }