コード例 #1
0
ファイル: QueryBuilder.php プロジェクト: paulusova/VuFind-2.x
 /**
  * Return SOLR search parameters based on a user query and params.
  *
  * @param AbstractQuery $query User query
  *
  * @return ParamBag
  */
 public function build(AbstractQuery $query)
 {
     $params = new ParamBag();
     // Add spelling query if applicable -- note that we mus set this up before
     // we process the main query in order to avoid unwanted extra syntax:
     if ($this->createSpellingQuery) {
         $params->set('spellcheck.q', $query->getAllTerms());
     }
     if ($query instanceof QueryGroup) {
         $query = $this->reduceQueryGroup($query);
     } else {
         $query->setString($this->getLuceneHelper()->normalizeSearchString($query->getString()));
     }
     $string = $query->getString() ?: '*:*';
     if ($handler = $this->getSearchHandler($query->getHandler(), $string)) {
         if (!$handler->hasExtendedDismax() && $this->getLuceneHelper()->containsAdvancedLuceneSyntax($string)) {
             $newString = $this->createAdvancedInnerSearchString($string, $handler);
             if ($handler->hasDismax()) {
                 $oldString = $newString;
                 $newString = $handler->createBoostQueryString($newString);
                 // If a boost was added, we don't want to highlight based on
                 // the boost query, so we should use the non-boosted version:
                 if ($this->createHighlightingQuery && $oldString != $string) {
                     $params->set('hl.q', $oldString);
                 }
                 if ($string == '*:*') {
                     $params->set('hl.q', '*:*');
                 }
             }
             $string = $newString;
         } else {
             if ($handler->hasDismax()) {
                 $params->set('qf', implode(' ', $handler->getDismaxFields()));
                 $params->set('qt', $handler->getDismaxHandler());
                 foreach ($handler->getDismaxParams() as $param) {
                     $params->add(reset($param), next($param));
                 }
                 if ($handler->hasFilterQuery()) {
                     $params->add('fq', $handler->getFilterQuery());
                 }
             } else {
                 $string = $handler->createSimpleQueryString($string);
             }
         }
     }
     $params->set('q', $string);
     return $params;
 }
コード例 #2
0
ファイル: Backend.php プロジェクト: steenlibrary/vufind
 /**
  * Perform a search and return record collection.
  *
  * @param AbstractQuery $query  Search query
  * @param integer       $offset Search offset
  * @param integer       $limit  Search limit
  * @param ParamBag      $params Search backend parameters
  *
  *@return \VuFindSearch\Response\RecordCollectionInterface
  **/
 public function search(AbstractQuery $query, $offset, $limit, ParamBag $params = null)
 {
     // process EDS API communication tokens.
     $authenticationToken = $this->getAuthenticationToken();
     $sessionToken = $this->getSessionToken();
     $this->debugPrint("Authentication Token: {$authenticationToken}, SessionToken: {$sessionToken}");
     // check to see if there is a parameter to only process this call as a setup
     if (null !== $params && true == $params->get('setuponly')) {
         return false;
     }
     // create query parameters from VuFind data
     $queryString = !empty($query) ? $query->getAllTerms() : '';
     $paramsStr = implode('&', null !== $params ? $params->request() : []);
     $this->debugPrint("Query: {$queryString}, Limit: {$limit}, Offset: {$offset}, " . "Params: {$paramsStr}");
     $baseParams = $this->getQueryBuilder()->build($query);
     $paramsStr = implode('&', $baseParams->request());
     $this->debugPrint("BaseParams: {$paramsStr} ");
     if (null !== $params) {
         $baseParams->mergeWith($params);
     }
     $baseParams->set('resultsPerPage', $limit);
     $page = $limit > 0 ? floor($offset / $limit) + 1 : 1;
     $baseParams->set('pageNumber', $page);
     $searchModel = $this->paramBagToEBSCOSearchModel($baseParams);
     $qs = $searchModel->convertToQueryString();
     $this->debugPrint("Search Model query string: {$qs}");
     try {
         $response = $this->client->search($searchModel, $authenticationToken, $sessionToken);
     } catch (\EbscoEdsApiException $e) {
         // if the auth or session token was invalid, try once more
         switch ($e->getApiErrorCode()) {
             case 104:
             case 108:
             case 109:
                 try {
                     // For error 104, retry auth token; for 108/9, retry sess token:
                     if ($e->getApiErrorCode() == 104) {
                         $authenticationToken = $this->getAuthenticationToken(true);
                     } else {
                         $sessionToken = $this->getSessionToken(true);
                     }
                     $response = $this->client->search($searchModel, $authenticationToken, $sessionToken);
                 } catch (Exception $e) {
                     throw new BackendException($e->getMessage(), $e->getCode(), $e);
                 }
                 break;
             default:
                 $response = [];
                 break;
         }
     } catch (Exception $e) {
         $this->debugPrint("Exception found: " . $e->getMessage());
         throw new BackendException($e->getMessage(), $e->getCode(), $e);
     }
     $collection = $this->createRecordCollection($response);
     $this->injectSourceIdentifier($collection);
     return $collection;
 }
コード例 #3
0
ファイル: QueryBuilder.php プロジェクト: bbeckman/NDL-VuFind2
 /**
  * Return SOLR search parameters based on a user query and params.
  *
  * @param AbstractQuery $query User query
  *
  * @return ParamBag
  */
 public function build(AbstractQuery $query)
 {
     $params = new ParamBag();
     // Add spelling query if applicable -- note that we must set this up before
     // we process the main query in order to avoid unwanted extra syntax:
     if ($this->createSpellingQuery) {
         $params->set('spellcheck.q', $this->getLuceneHelper()->extractSearchTerms($query->getAllTerms()));
     }
     if ($query instanceof QueryGroup) {
         $query = $this->reduceQueryGroup($query);
     } else {
         $query->setString($this->getLuceneHelper()->normalizeSearchString($query->getString()));
     }
     $string = $query->getString() ?: '*:*';
     if ($handler = $this->getSearchHandler($query->getHandler(), $string)) {
         if (!$handler->hasExtendedDismax() && $this->getLuceneHelper()->containsAdvancedLuceneSyntax($string)) {
             $string = $this->createAdvancedInnerSearchString($string, $handler);
             if ($handler->hasDismax()) {
                 $oldString = $string;
                 $string = $handler->createBoostQueryString($string);
                 // If a boost was added, we don't want to highlight based on
                 // the boost query, so we should use the non-boosted version:
                 if ($this->createHighlightingQuery && $oldString != $string) {
                     $params->set('hl.q', $oldString);
                 }
             }
         } else {
             if ($handler->hasDismax()) {
                 // If we're using extended dismax, we'll miss out on the question
                 // mark fix in createAdvancedInnerSearchString(), so we should
                 // apply it here. If other query munges arise that are valuable
                 // to both dismax and edismax, we should add a wrapper function
                 // around them and call it from here instead of this one very
                 // specific check.
                 $string = $this->fixTrailingQuestionMarks($string);
                 $params->set('qf', implode(' ', $handler->getDismaxFields()));
                 $params->set('qt', $handler->getDismaxHandler());
                 foreach ($handler->getDismaxParams() as $param) {
                     $params->add(reset($param), next($param));
                 }
                 if ($handler->hasFilterQuery()) {
                     $params->add('fq', $handler->getFilterQuery());
                 }
             } else {
                 $string = $handler->createSimpleQueryString($string);
             }
         }
     }
     $params->set('q', $string);
     return $params;
 }