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));
 }
 /**
  * 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();
 }
 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();
 }
コード例 #4
0
 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.'))));
 }