Example #1
0
 /**
  * just shows empty page. need to determine what to show on default page.
  *
  * @param Request $request
  *
  * @return \Symfony\Component\HttpFoundation\Response
  *
  * @Route("/", name="ubc_exam_main_homepage")
  */
 public function indexAction(Request $request)
 {
     $pagination = array();
     $pagerHtml = null;
     $q = $request->get('q');
     if (!is_null($q) && !empty($q)) {
         // search the index
         QueryParser::setDefaultOperator(QueryParser::B_AND);
         Wildcard::setMinPrefixLength(1);
         $hits = $this->get('ivory_lucene_search')->getIndex('exams')->find($q . '*');
         $ids = array();
         foreach ($hits as $hit) {
             $ids[] = $hit->pk;
         }
         $ids = array_unique($ids);
         // search the db by ids, because we need to get the exams only visible for current user
         if (!empty($ids)) {
             // find out the current user registered courses and faculty
             $em = $this->getDoctrine()->getManager();
             $coursesWithKeys = $request->getSession()->get('courses') ? $request->getSession()->get('courses') : array();
             $courses = array_keys($coursesWithKeys);
             $faculties = array_values($em->getRepository('UBCExamMainBundle:SubjectFaculty')->getFacultiesByCourses($courses));
             $userId = $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') ? -1 : $this->getCurrentUserId();
             $qb = $this->getDoctrine()->getRepository('UBCExamMainBundle:Exam')->queryExamsByIds($ids, $userId, $faculties, $courses);
             $paginator = $this->get('knp_paginator');
             $pagination = $paginator->paginate($qb, $request->query->get('page', 1), 20);
         }
     }
     return $this->render('UBCExamMainBundle:Default:index.html.twig', array('pagination' => $pagination, 'q' => $q, 'subjectCode' => '', 'subjectCodeLabel' => ''));
 }
 /**
  * @param string $path Path to search index
  */
 public function __construct($path, NodeTypeManagerInterface $nodeTypeManager = null, $hideDestructException = false)
 {
     $this->path = $path;
     $this->filesystem = new Filesystem();
     $this->nodeTypeManager = $nodeTypeManager;
     $this->hideDestructException = $hideDestructException;
     Analyzer::setDefault(new ExactMatchAnalyzer());
     Wildcard::setMinPrefixLength(0);
 }
 public function tearDown()
 {
     Query\Wildcard::setMinPrefixLength($this->_wildcardMinPrefix);
     Query\Fuzzy::setDefaultPrefixLength($this->_defaultPrefixLength);
 }
Example #4
0
 public function testWildcardQuery()
 {
     $index = Lucene\Lucene::open(__DIR__ . '/_index23Sample/_files');
     $wildcardMinPrefix = Query\Wildcard::getMinPrefixLength();
     Query\Wildcard::setMinPrefixLength(0);
     $hits = $index->find('*cont*');
     $this->assertEquals(count($hits), 9);
     $expectedResultset = array(array(8, 0.328087, 'IndexSource/contributing.html'), array(2, 0.318592, 'IndexSource/contributing.patches.html'), array(7, 0.260137, 'IndexSource/contributing.bugs.html'), array(0, 0.203372, 'IndexSource/contributing.documentation.html'), array(1, 0.202366, 'IndexSource/contributing.wishlist.html'), array(4, 0.052931, 'IndexSource/copyright.html'), array(3, 0.01707, 'IndexSource/about-pear.html'), array(5, 0.01015, 'IndexSource/authors.html'), array(9, 0.003504, 'IndexSource/core.html'));
     foreach ($hits as $resId => $hit) {
         $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
         $this->assertTrue(abs($hit->score - $expectedResultset[$resId][1]) < 1.0E-6);
         $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
     }
     Query\Wildcard::setMinPrefixLength($wildcardMinPrefix);
 }
Example #5
0
 protected function buildQuery($keyword, $options)
 {
     // Allow *Token*
     \ZendSearch\Lucene\Search\Query\Wildcard::setMinPrefixLength(0);
     $query = new \ZendSearch\Lucene\Search\Query\Boolean();
     foreach (explode(" ", $keyword) as $k) {
         // Require at least 3 non-wildcard characters
         if (strlen($k) > 2) {
             $term = new \ZendSearch\Lucene\Index\Term("*" . $k . "*");
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Wildcard($term), true);
         }
     }
     // Add model filter
     if (isset($options['model']) && $options['model'] != "") {
         if (is_array($options['model'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['model'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($model, 'model'));
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['model'], 'model');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     // Add type filter
     if (isset($options['type']) && $options['type'] != "") {
         if (is_array($options['type'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['type'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($type), 'type');
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['type'], 'type');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     // Add custom filters
     if (isset($options['filters']) && is_array($options['filters'])) {
         foreach ($options['filters'] as $field => $value) {
             $term = new \ZendSearch\Lucene\Index\Term($value, $field);
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     if ($options['checkPermissions'] && !Yii::$app->request->isConsoleRequest) {
         $permissionQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         if (Yii::$app->user->isGuest) {
             // Guest Content
             $guestContentQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')), true);
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_CONTENT, 'type')), true);
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
             $guestSpaceListQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach (Space::find()->where(['visibility' => Space::VISIBILITY_ALL])->all() as $space) {
                 $guestSpaceListQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
             }
             $guestContentQuery->addSubquery($guestSpaceListQuery, true);
             $permissionQuery->addSubquery($guestContentQuery);
             // Guest Spaces
             $guestSpacesQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $guestSpacesQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_SPACE, 'type')), true);
             $guestSpacesQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')), true);
             $permissionQuery->addSubquery($guestSpacesQuery);
             $permissionQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_USER, 'type')));
         } else {
             //--- Public Content
             $permissionQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')));
             //--- Private Space Content
             $privateSpaceContentQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PRIVATE, 'visibility')), true);
             $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
             $privateSpacesListQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach (\humhub\modules\space\models\Membership::GetUserSpaces() as $space) {
                 $privateSpacesListQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
             }
             $privateSpaceContentQuery->addSubquery($privateSpacesListQuery, true);
             $permissionQuery->addSubquery($privateSpaceContentQuery);
         }
         $query->addSubquery($permissionQuery, true);
     }
     if (count($options['limitSpaces']) > 0) {
         $spaceBaseQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         $spaceBaseQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
         $spaceIdQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
         foreach ($options['limitSpaces'] as $space) {
             $spaceIdQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
         }
         $spaceBaseQuery->addSubquery($spaceIdQuery, true);
         $query->addSubquery($spaceBaseQuery, true);
     }
     return $query;
 }
Example #6
0
 public function testWildcardQuery()
 {
     $index = Lucene\Lucene::open(__DIR__ . '/_indexSample/_files');
     $wildcardMinPrefix = Query\Wildcard::getMinPrefixLength();
     Query\Wildcard::setMinPrefixLength(0);
     $hits = $index->find('*cont*');
     $this->assertEquals(count($hits), 9);
     $expectedResultset = array(array(8, 0.125253, 'IndexSource/contributing.html'), array(4, 0.112122, 'IndexSource/copyright.html'), array(2, 0.108491, 'IndexSource/contributing.patches.html'), array(7, 0.07771599999999999, 'IndexSource/contributing.bugs.html'), array(0, 0.05076, 'IndexSource/contributing.documentation.html'), array(1, 0.049163, 'IndexSource/contributing.wishlist.html'), array(3, 0.036159, 'IndexSource/about-pear.html'), array(5, 0.0215, 'IndexSource/authors.html'), array(9, 0.007422, 'IndexSource/core.html'));
     foreach ($hits as $resId => $hit) {
         $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
         $this->assertTrue(abs($hit->score - $expectedResultset[$resId][1]) < 1.0E-6);
         $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
     }
     Query\Wildcard::setMinPrefixLength($wildcardMinPrefix);
 }
 /**
  * Search page for the term in the index.
  * @param string $term
  * @return array ('results' => array \ZendSearch\Lucene\Search\QueryHit, 'query' => string)
  */
 public function find($term)
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     return ['results' => $this->luceneIndex->find($term), 'query' => $term];
 }
Example #8
0
 /**
  * Выполнить поисковый запрос. Результат представляет собой
  *
  * @param string $queryWord строка запроса
  * @param int $totalCount
  * @param string $queryEncoding кодировка строки запроса
  * @param ModelFilter|null $filter фильтр моделей
  * @return QueryHit[]
  */
 public function search($queryWord, &$totalCount, $queryEncoding = 'utf-8', ModelFilter $filter = null)
 {
     Wildcard::setMinPrefixLength(0);
     $this->lastQuery = mb_convert_encoding($queryWord, 'utf-8', $queryEncoding);
     // фильтрация всех символов, кроме букв и цифр
     $this->lastQuery = mb_ereg_replace("[^а-яА-Яa-zA-Z0-9]+", " ", $this->lastQuery);
     $query = $this->buildQuery($this->lastQuery, $this->getFields());
     $hitList = $this->connection->find($query);
     if (null !== $filter) {
         $hitList = $filter->doFilter($hitList);
     }
     $totalCount = count($hitList);
     return $hitList;
 }
Example #9
0
 /**
  * Query specific matches highlighting
  *
  * @param Highlighter $highlighter  Highlighter object (also contains doc for highlighting)
  */
 protected function _highlightMatches(Highlighter $highlighter)
 {
     /** Skip fields detection. We don't need it, since we expect all fields presented in the HTML body and don't differentiate them */
     /** Skip exact term matching recognition, keyword fields highlighting is not supported */
     // -------------------------------------
     // Recognize wildcard queries
     /** 
      * @todo check for PCRE unicode support may be performed through Zend_Environment in some future 
      */
     ErrorHandler::start(E_WARNING);
     $result = preg_match('/\\pL/u', 'a');
     ErrorHandler::stop();
     if ($result == 1) {
         $word = iconv($this->_encoding, 'UTF-8', $this->_word);
         $wildcardsPattern = '/[*?]/u';
         $subPatternsEncoding = 'UTF-8';
     } else {
         $word = $this->_word;
         $wildcardsPattern = '/[*?]/';
         $subPatternsEncoding = $this->_encoding;
     }
     $subPatterns = preg_split($wildcardsPattern, $word, -1, PREG_SPLIT_OFFSET_CAPTURE);
     if (count($subPatterns) > 1) {
         // Wildcard query is recognized
         $pattern = '';
         foreach ($subPatterns as $id => $subPattern) {
             // Append corresponding wildcard character to the pattern before each sub-pattern (except first)
             if ($id != 0) {
                 $pattern .= $word[$subPattern[1] - 1];
             }
             // Check if each subputtern is a single word in terms of current analyzer
             $tokens = Analyzer\Analyzer::getDefault()->tokenize($subPattern[0], $subPatternsEncoding);
             if (count($tokens) > 1) {
                 // Do nothing (nothing is highlighted)
                 return;
             }
             foreach ($tokens as $token) {
                 $pattern .= $token->getTermText();
             }
         }
         $term = new Index\Term($pattern, $this->_field);
         $query = new Query\Wildcard($term);
         $query->_highlightMatches($highlighter);
         return;
     }
     // -------------------------------------
     // Recognize one-term multi-term and "insignificant" queries
     $tokens = Analyzer\Analyzer::getDefault()->tokenize($this->_word, $this->_encoding);
     if (count($tokens) == 0) {
         // Do nothing
         return;
     }
     if (count($tokens) == 1) {
         $highlighter->highlight($tokens[0]->getTermText());
         return;
     }
     //It's not insignificant or one term query
     $words = array();
     foreach ($tokens as $token) {
         $words[] = $token->getTermText();
     }
     $highlighter->highlight($words);
 }
 /**
  * Search page for the term in the index.
  * @param string $term
  * @param array $fields (string => string)
  * @return array ('results' => \ZendSearch\Lucene\Search\QueryHit[], 'query' => string)
  */
 public function find($term, $fields = [])
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     if (empty($fields)) {
         return ['results' => $this->luceneIndex->find($term), 'query' => $term];
     }
     $fieldTerms[] = new IndexTerm($term);
     foreach ($fields as $field => $fieldText) {
         $fieldTerms[] = new IndexTerm($fieldText, $field);
     }
     return ['results' => $this->luceneIndex->find(new MultiTerm($fieldTerms)), 'query' => $term];
 }
 /**
  * Search page for the term in the index.
  *
  * @param $term
  * @param array $fields
  * @return array|\ZendSearch\Lucene\Search\QueryHit
  */
 public function search($term, $fields = [])
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     $query = new MultiTerm();
     if (count($fields)) {
         foreach ($fields as $field => $value) {
             $query->addTerm(new IndexTerm($value, $field), true);
         }
     }
     $subTerm = explode(' ', $term);
     foreach ($subTerm as $value) {
         $query->addTerm(new IndexTerm($value), true);
     }
     return $this->luceneIndex->find($query);
 }