Example #1
0
 function getDescriptionByRecordId()
 {
     global $configArray;
     //Load the record id that the user wants to search for
     $recordId = trim($_REQUEST['recordId']);
     // Setup Search Engine Connection
     $class = $configArray['Index']['engine'];
     $url = $configArray['Index']['url'];
     /** @var SearchObject_Solr db */
     $this->db = new $class($url);
     //Search the database by title and author
     if ($recordId) {
         if (preg_match('/^b\\d{7}[\\dx]$/', $recordId)) {
             $recordId = '.' . $recordId;
         }
         $searchResults = $this->db->search("{$recordId}", 'Id');
     } else {
         $results = array('result' => false, 'message' => 'Please enter the record Id to look for');
         return $results;
     }
     if ($searchResults['response']['numFound'] == 0) {
         $results = array('result' => false, 'message' => 'Sorry, we could not find a description for that record id');
     } else {
         $firstRecord = $searchResults['response']['docs'][0];
         require_once ROOT_DIR . '/RecordDrivers/GroupedWorkDriver.php';
         $groupedWork = new GroupedWorkDriver($firstRecord);
         $results = array('result' => true, 'message' => 'Found a summary for record ' . $firstRecord['title_display'] . ' by ' . $firstRecord['author_display'], 'recordsFound' => $searchResults['response']['numFound'], 'description' => $groupedWork->getDescription());
     }
     return $results;
 }
 function assessSimilarity($array, $strict = 1, $drift = 2)
 {
     foreach ($array as $key => $value) {
         $solr = new Solr();
         $results = $solr->search($key, count($value) * $drift);
         $matches = 0;
         foreach ($results as $pid) {
             if (in_array($pid, $value)) {
                 $matches++;
             }
         }
         $this->assertTrue($matches * $strict >= count($value), "Search for " . Helpers::search_link($key) . " returned " . $matches . " of it's expected results in the top " . count($value) * $drift);
     }
 }
 function test_nzmaths_where_appropriate()
 {
     # Checks that first 10 results for nzmaths related keywords
     # return good proportion of MZMaths items. Ensures fix for
     # previous function doesn't work too well.
     $test_items = array('ALiM school stories nzmaths', 'Algebra information', 'Attribute Blocks: Exploring Shape', 'scuba');
     foreach ($test_items as $value) {
         $solr = new Solr();
         $pids = $solr->search($value, 50);
         $i = 0;
         foreach ($pids as $id) {
             if (preg_match('/nzmaths/', $id)) {
                 $i++;
             }
         }
         $this->assertTrue($i > 0, "Search for " . Helpers::search_link($value) . " has insufficient nzmaths items");
     }
 }
 /**
  * @param string $sourceSolrId
  * @param string $issn
  * @param integer $numResourcesToLoad
  * @param Solr $db
  * @return null
  */
 private static function getXISSN($sourceSolrId, $issn, $numResourcesToLoad, $db)
 {
     global $configArray;
     // Build URL
     $url = 'http://xissn.worldcat.org/webservices/xid/issn/' . urlencode(is_array($issn) ? $issn[0] : $issn) . '?method=getEditions&format=xml';
     if (isset($configArray['WorldCat']['id'])) {
         $url .= '&ai=' . $configArray['WorldCat']['id'];
     }
     // Print Debug code
     if ($configArray['System']['debug']) {
         global $logger;
         $logger->log("<pre>XISSN: {$url}</pre>", PEAR_LOG_INFO);
     }
     // Fetch results
     $query = '';
     $data = @file_get_contents($url);
     if (empty($data)) {
         return null;
     }
     $unxml = new XML_Unserializer();
     $unxml->unserialize($data);
     $data = $unxml->getUnserializedData($data);
     if (!empty($data) && isset($data['group']['issn'])) {
         if (is_array($data['group']['issn'])) {
             foreach ($data['group']['issn'] as $issn) {
                 if ($query != '') {
                     $query .= ' OR issn:' . $issn;
                 } else {
                     $query = 'issn:' . $issn;
                 }
             }
         } else {
             $query = 'issn:' . $data['group']['issn'];
         }
     }
     if ($query) {
         // Filter out current record
         $query .= ' NOT id:' . $sourceSolrId;
         $result = $db->search($query, null, null, 0, $numResourcesToLoad);
         if (!PEAR_Singleton::isError($result)) {
             if (isset($result['response']['docs']) && !empty($result['response']['docs'])) {
                 return $result['response']['docs'];
             } else {
                 return null;
             }
         } else {
             return $result;
         }
     } else {
         return null;
     }
 }
Example #5
0
 /**
  * Actually process and submit the search
  *
  * @access  public
  * @param   bool   $returnIndexErrors  Should we die inside the index code if
  *                                     we encounter an error (false) or return
  *                                     it for access via the getIndexError()
  *                                     method (true)?
  * @param   bool   $recommendations    Should we process recommendations along
  *                                     with the search itself?
  * @return  object solr result structure (for now)
  */
 public function processSearch($returnIndexErrors = false, $recommendations = false)
 {
     global $timer;
     global $analytics;
     // Our search has already been processed in init()
     $search = $this->searchTerms;
     // Build a recommendations module appropriate to the current search:
     if ($recommendations) {
         $this->initRecommendations();
     }
     $timer->logTime("initRecommendations");
     // Tag searches need to be handled differently
     if (count($search) == 1 && isset($search[0]['index']) && $search[0]['index'] == 'tag') {
         // If we managed to find some tag matches, let's override the search
         // array.  If we didn't find any tag matches, we should return an
         // empty record set.
         $newSearch = $this->processTagSearch($search[0]['lookfor']);
         $timer->logTime("process Tag search");
         // Save search so it displays correctly on the "no hits" page:
         $this->publicQuery = $search[0]['lookfor'];
         if (empty($newSearch)) {
             return array('response' => array('numFound' => 0, 'docs' => array()));
         } else {
             $search = $newSearch;
         }
     }
     // Build Query
     $query = $this->indexEngine->buildQuery($search);
     $timer->logTime("build query");
     if (PEAR_Singleton::isError($query)) {
         return $query;
     }
     // Only use the query we just built if there isn't an override in place.
     if ($this->query == null) {
         $this->query = $query;
     }
     // Define Filter Query
     $filterQuery = $this->hiddenFilters;
     //Remove any empty filters if we get them
     //(typically happens when a subdomain has a function disabled that is enabled in the main scope)
     foreach ($this->filterList as $field => $filter) {
         if (empty($field)) {
             unset($this->filterList[$field]);
         }
     }
     foreach ($this->filterList as $field => $filter) {
         foreach ($filter as $value) {
             $analytics->addEvent('Apply Facet', $field, $value);
             // Special case -- allow trailing wildcards:
             if (substr($value, -1) == '*') {
                 $filterQuery[] = "{$field}:{$value}";
             } elseif (preg_match('/\\A\\[.*?\\sTO\\s.*?]\\z/', $value)) {
                 $filterQuery[] = "{$field}:{$value}";
             } else {
                 if (!empty($value)) {
                     $filterQuery[] = "{$field}:\"{$value}\"";
                 }
             }
         }
     }
     // If we are only searching one field use the DisMax handler
     //    for that field. If left at null let solr take care of it
     if (count($search) == 1 && isset($search[0]['index'])) {
         $this->index = $search[0]['index'];
     }
     // Build a list of facets we want from the index
     $facetSet = array();
     if (!empty($this->facetConfig)) {
         $facetSet['limit'] = $this->facetLimit;
         foreach ($this->facetConfig as $facetField => $facetName) {
             $facetSet['field'][] = $facetField;
         }
         if ($this->facetOffset != null) {
             $facetSet['offset'] = $this->facetOffset;
         }
         if ($this->facetPrefix != null) {
             $facetSet['prefix'] = $this->facetPrefix;
         }
         if ($this->facetSort != null) {
             $facetSet['sort'] = $this->facetSort;
         }
     }
     $timer->logTime("create facets");
     // Build our spellcheck query
     if ($this->spellcheck) {
         if ($this->spellSimple) {
             $this->useBasicDictionary();
         }
         $spellcheck = $this->buildSpellingQuery();
         // If the spellcheck query is purely numeric, skip it if
         // the appropriate setting is turned on.
         if ($this->spellSkipNumeric && is_numeric($spellcheck)) {
             $spellcheck = "";
         }
     } else {
         $spellcheck = "";
     }
     $timer->logTime("create spell check");
     // Get time before the query
     $this->startQueryTimer();
     // The "relevance" sort option is a VuFind reserved word; we need to make
     // this null in order to achieve the desired effect with Solr:
     $finalSort = $this->sort == 'relevance' ? null : $this->sort;
     // The first record to retrieve:
     //  (page - 1) * limit = start
     $recordStart = ($this->page - 1) * $this->limit;
     $this->indexResult = $this->indexEngine->search($this->query, $this->index, $filterQuery, $recordStart, $this->limit, $facetSet, $spellcheck, $this->dictionary, $finalSort, $this->fields, $this->method, $returnIndexErrors);
     $timer->logTime("run solr search");
     // Get time after the query
     $this->stopQueryTimer();
     // How many results were there?
     if (!isset($this->indexResult['response']['numFound'])) {
         //An error occurred
         $this->resultsTotal = 0;
     } else {
         $this->resultsTotal = $this->indexResult['response']['numFound'];
     }
     // Process spelling suggestions if no index error resulted from the query
     if ($this->spellcheck && !isset($this->indexResult['error'])) {
         // Shingle dictionary
         $this->processSpelling();
         // Make sure we don't endlessly loop
         if ($this->dictionary == 'default') {
             // Expand against the basic dictionary
             $this->basicSpelling();
         }
     }
     // If extra processing is needed for recommendations, do it now:
     if ($recommendations && is_array($this->recommend)) {
         foreach ($this->recommend as $currentSet) {
             foreach ($currentSet as $current) {
                 $current->process();
             }
         }
     }
     //Add debug information to the results if available
     if ($this->debug) {
         $explainInfo = $this->indexResult['debug']['explain'];
         foreach ($this->indexResult['response']['docs'] as $key => $result) {
             if (array_key_exists($result['id'], $explainInfo)) {
                 $result['explain'] = $explainInfo[$result['id']];
                 $this->indexResult['response']['docs'][$key] = $result;
             }
         }
     }
     // Return the result set
     return $this->indexResult;
 }