/** * @see ArticleSearch::retrieveResults() */ function callbackRetrieveResults($hookName, $params) { assert($hookName == 'ArticleSearch::retrieveResults'); // Unpack the parameters. list($journal, $keywords, $fromDate, $toDate, $page, $itemsPerPage, $dummy) = $params; $totalResults =& $params[6]; // need to use reference $error =& $params[7]; // need to use reference // Instantiate a search request. $searchRequest = new SolrSearchRequest(); $searchRequest->setJournal($journal); $searchRequest->setFromDate($fromDate); $searchRequest->setToDate($toDate); $searchRequest->setPage($page); $searchRequest->setItemsPerPage($itemsPerPage); $searchRequest->addQueryFromKeywords($keywords); // Get the ordering criteria. list($orderBy, $orderDir) = $this->_getResultSetOrdering($journal); $searchRequest->setOrderBy($orderBy); $searchRequest->setOrderDir($orderDir == 'asc' ? true : false); // Configure alternative spelling suggestions. $spellcheck = (bool) $this->getSetting(0, 'spellcheck'); $searchRequest->setSpellcheck($spellcheck); // Configure highlighting. $highlighting = (bool) $this->getSetting(0, 'highlighting'); $searchRequest->setHighlighting($highlighting); // Configure faceting. // 1) Faceting will be disabled for filtered search categories. $activeFilters = array_keys($searchRequest->getQuery()); if (is_a($journal, 'Journal')) { $activeFilters[] = 'journalTitle'; } if (!empty($fromDate) || !empty($toDate)) { $activeFilters[] = 'publicationDate'; } // 2) Switch faceting on for enabled categories that have no // active filters. $facetCategories = array_values(array_diff($this->_getEnabledFacetCategories(), $activeFilters)); $searchRequest->setFacetCategories($facetCategories); // Configure custom ranking. $customRanking = (bool) $this->getSetting(0, 'customRanking'); if ($customRanking) { $sectionDao =& DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */ if (is_a($journal, 'Journal')) { $sections = $sectionDao->getJournalSections($journal->getId()); } else { $sections = $sectionDao->getSections(); } while (!$sections->eof()) { /* @var $sections DAOResultFactory */ $section =& $sections->next(); $rankingBoost = $section->getData('rankingBoost'); if (isset($rankingBoost)) { $sectionBoost = (double) $rankingBoost; } else { $sectionBoost = LUCENE_PLUGIN_DEFAULT_RANKING_BOOST; } if ($sectionBoost != LUCENE_PLUGIN_DEFAULT_RANKING_BOOST) { $searchRequest->addBoostFactor('section_id', $section->getId(), $sectionBoost); } unset($section); } unset($sections); } // Call the solr web service. $solrWebService =& $this->getSolrWebService(); $result =& $solrWebService->retrieveResults($searchRequest, $totalResults); if (is_null($result)) { $error = $solrWebService->getServiceMessage(); $this->_informTechAdmin($error, $journal, true); $error .= ' ' . __('plugins.generic.lucene.message.techAdminInformed'); return array(); } else { // Store spelling suggestion, highlighting and faceting info // internally. We cannot route these back through the request // as the default search implementation does not support // these features. if ($spellcheck && isset($result['spellingSuggestion'])) { $this->_spellingSuggestion = $result['spellingSuggestion']; // Identify the field for which we got the suggestion. foreach ($keywords as $bitmap => $searchPhrase) { if (!empty($searchPhrase)) { switch ($bitmap) { case null: $queryField = 'query'; break; case ARTICLE_SEARCH_INDEX_TERMS: $queryField = 'indexTerms'; break; default: $indexFieldMap = ArticleSearch::getIndexFieldMap(); assert(isset($indexFieldMap[$bitmap])); $queryField = $indexFieldMap[$bitmap]; } } } $this->_spellingSuggestionField = $queryField; } if ($highlighting && isset($result['highlightedArticles'])) { $this->_highlightedArticles = $result['highlightedArticles']; } if (!empty($facetCategories) && isset($result['facets'])) { $this->_facets = $result['facets']; } // Return the scored results. if (isset($result['scoredResults']) && !empty($result['scoredResults'])) { return $result['scoredResults']; } else { return array(); } } }
/** * Configure the search request from a keywords * array as required by ArticleSearch::retrieveResults() * * @param $keywords array See ArticleSearch::retrieveResults() */ function addQueryFromKeywords($keywords) { // Get a mapping of OJS search fields bitmaps to index fields. $indexFieldMap = ArticleSearch::getIndexFieldMap(); // The keywords list is indexed with a search field bitmap. foreach ($keywords as $searchFieldBitmap => $searchPhrase) { // Translate the search field from OJS to solr nomenclature. if (empty($searchFieldBitmap)) { // An empty search field means "all fields". $solrFields = array_values($indexFieldMap); } else { $solrFields = array(); foreach ($indexFieldMap as $ojsField => $solrField) { // The search field bitmap may stand for // several actual index fields (e.g. the index terms // field). if ($searchFieldBitmap & $ojsField) { $solrFields[] = $solrField; } } } $solrFieldString = implode('|', $solrFields); $this->addQueryFieldPhrase($solrFieldString, $searchPhrase); } }
/** * Retrieve auto-suggestions from the faceting service. * @param $url string * @param $searchRequest SolrSearchRequest * @param $userInput string * @param $fieldName string * @return array The generated suggestions. */ function _getFacetingAutosuggestions($url, $searchRequest, $userInput, $fieldName) { // Remove special characters from the user input. $searchTerms = strtr($userInput, '"()+-|&!', ' '); // Cut off the last search term. $searchTerms = explode(' ', $searchTerms); $facetPrefix = array_pop($searchTerms); if (empty($facetPrefix)) { return array(); } // Use the remaining search query to pre-filter // facet results. This may be an invalid query // but edismax will deal gracefully with syntax // errors. $userInput = String::substr($userInput, 0, -String::strlen($facetPrefix)); switch ($fieldName) { case 'query': // The 'query' filter goes agains all fields. $articleSearch = new ArticleSearch(); $solrFields = array_values($articleSearch->getIndexFieldMap()); break; case 'indexTerms': // The 'index terms' filter goes against keyword index fields. $solrFields = array('discipline', 'subject', 'type', 'coverage'); break; default: // All other filters can be used directly. $solrFields = array($fieldName); } $solrFieldString = implode('|', $solrFields); $searchRequest->addQueryFieldPhrase($solrFieldString, $userInput); // Construct the main query. $params = $this->_getSearchQueryParameters($searchRequest); if (!isset($params['q'])) { // Use a catch-all query in case we have no limiting // search. $params['q'] = '*:*'; } if ($fieldName == 'query') { $params['facet.field'] = 'default_spell'; } else { $params['facet.field'] = $fieldName . '_spell'; } $facetPrefixLc = String::strtolower($facetPrefix); $params['facet.prefix'] = $facetPrefixLc; // Make the request. $response = $this->_makeRequest($url, $params); if (!is_a($response, 'DOMXPath')) { return array(); } // Extract term suggestions. $nodeList = $response->query('//lst[@name="facet_fields"]/lst/int/@name'); if ($nodeList->length == 0) { return array(); } $termSuggestions = array(); foreach ($nodeList as $childNode) { $termSuggestions[] = $childNode->value; } // Add the term suggestion to the remaining user input. $suggestions = array(); foreach ($termSuggestions as $termSuggestion) { // Restore case if possible. if (strpos($termSuggestion, $facetPrefixLc) === 0) { $termSuggestion = $facetPrefix . String::substr($termSuggestion, String::strlen($facetPrefix)); } $suggestions[] = $userInput . $termSuggestion; } return $suggestions; }
/** * Load the keywords array from a given search filter. * @param $searchFilters array Search filters as returned from * ArticleSearch::getSearchFilters() * @return array Keyword array as required by ArticleSearch::retrieveResults() */ function getKeywordsFromSearchFilters($searchFilters) { $indexFieldMap = ArticleSearch::getIndexFieldMap(); $indexFieldMap[ARTICLE_SEARCH_INDEX_TERMS] = 'indexTerms'; $keywords = array(); if (isset($searchFilters['query'])) { $keywords[null] = $searchFilters['query']; } foreach ($indexFieldMap as $bitmap => $searchField) { if (isset($searchFilters[$searchField]) && !empty($searchFilters[$searchField])) { $keywords[$bitmap] = $searchFilters[$searchField]; } } return $keywords; }