retrieveResults() public method

Execute a search against the Solr search server.
public retrieveResults ( &$searchRequest, &$totalResults ) : array
$searchRequest SolrSearchRequest
$totalResults integer An output parameter returning the total number of search results found by the query. This differs from the actual number of returned results as the search can be limited.
return array An array of search results. The main keys are result types. These are "scoredResults" and "alternativeSpelling". The keys in the "scoredResults" sub-array are scores (1-9999) and the values are article IDs. The alternative spelling sub-array returns an alternative query string (if any) and the number of hits for this string. Null if an error occurred while querying the server.
コード例 #1
0
 /**
  * SCENARIO: re-index one journal (CLI)
  *   GIVEN I am on the command line
  *    WHEN I execute the tools/rebuildSearchIndex.php script
  *     AND I enter the path of a journal as command line argument
  *    THEN all articles of that journal will be deleted from
  *         the index and then re-indexed.
  *
  * SCENARIO: re-index all journals (CLI)
  *   GIVEN I am on the command line
  *    WHEN I execute the tools/rebuildSearchIndex.php script
  *         without parameters
  *    THEN all articles of all journals of the installation will
  *         be deleted from the index and then re-indexed.
  */
 public function testReindexJournalsByCli()
 {
     // Delete all documents from the index. As we use the embedded server
     // for testint we can use the well-known service parameters.
     import('plugins/generic/lucene/classes/SolrWebService');
     $solrWebService = new SolrWebService('http://localhost:8983/solr/ojs/search', 'admin', 'please change', 'test-inst');
     $solrWebService->deleteArticlesFromIndex(1);
     $solrWebService->deleteArticlesFromIndex(2);
     $searchRequest = new SolrSearchRequest();
     $searchRequest->addQueryFieldPhrase('query', '*:*');
     $totalResults = null;
     $solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertEquals(0, $totalResults);
     // Assemble the command line script name.
     $scriptName = Core::getBaseDir() . '/tools/rebuildSearchIndex.php -d';
     // Execute the script for one journal only.
     $output = null;
     exec("php {$scriptName} test", $output);
     // Check the script output.
     $expectedOutput = array('LucenePlugin: Clearing index \\.\\.\\. done', 'LucenePlugin: Indexing "test" \\. [0-9]+ articles indexed', 'LucenePlugin: Rebuilding dictionaries \\.\\.\\. done');
     foreach ($output as $outputLine) {
         $expectedRegex = array_shift($expectedOutput);
         self::assertRegExp("/{$expectedRegex}/", $outputLine);
     }
     // Check the index.
     $solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertGreaterThan(0, $totalResults);
     self::assertLessThan(37, $totalResults);
     // Execute the script for all journals.
     $output = null;
     exec("php {$scriptName}", $output);
     // Check the script output.
     $expectedOutput = array('LucenePlugin: Clearing index \\.\\.\\. done', 'LucenePlugin: Indexing "lucene-test" \\. [0-9]+ articles indexed', 'LucenePlugin: Indexing "test" \\. [0-9]+ articles indexed');
     foreach ($output as $outputLine) {
         $expectedRegex = array_shift($expectedOutput);
         self::assertRegExp("/{$expectedRegex}/", $outputLine);
     }
     // Rebuild the dictionaries only
     $output = null;
     exec("php {$scriptName} -d -n", $output);
     // Check the script output.
     $expectedRegex = 'LucenePlugin: Rebuilding dictionaries \\.\\.\\. done';
     self::assertEquals(1, count($output));
     self::assertRegExp("/{$expectedRegex}/", array_shift($output));
     // Check the index.
     $solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertGreaterThanOrEqual(37, $totalResults);
 }
コード例 #2
0
ファイル: SolrWebServiceTest.php プロジェクト: jalperin/ojs
 /**
  * @covers SolrWebService::retrieveResults()
  *
  * NB: Depends on correct journal indexing
  * and must therefore be run after testIndexJournal().
  * We run journal indexing as the last test and
  * this test as the first test as journal indexing
  * is asynchronous. This means that a prior test
  * run must be successful for this test to pass.
  */
 public function testRetrieveResults()
 {
     $embeddedServer = new EmbeddedServer();
     $this->_startServer($embeddedServer);
     // Make sure that the journal is properly indexed.
     $this->_indexTestJournals();
     // Make a search on specific fields.
     $searchRequest = new SolrSearchRequest();
     $journal = new Journal();
     $journal->setId(2);
     $searchRequest->setJournal($journal);
     $searchRequest->setQuery(array('suppFiles' => 'pizza', 'authors' => 'Author', 'galleyFullText' => 'Nutella', 'title' => 'Article'));
     $searchRequest->setFromDate(date('Y-m-d\\TH:i:s\\Z', strtotime('2000-01-01')));
     $searchRequest->setHighlighting(true);
     $totalResults = null;
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     // Check search results.
     self::assertTrue(is_int($totalResults), $totalResults > 0);
     self::assertTrue(isset($results['scoredResults']));
     $scoredResults = $results['scoredResults'];
     self::assertTrue(is_array($scoredResults));
     self::assertTrue(!empty($scoredResults));
     self::assertTrue(in_array('3', $scoredResults));
     // Check highlighting results.
     self::assertTrue(isset($results['highlightedArticles']));
     $highlightedArticles = $results['highlightedArticles'];
     self::assertTrue(is_array($highlightedArticles));
     self::assertTrue(!empty($highlightedArticles));
     self::assertTrue(isset($highlightedArticles['3']));
     self::assertContains('Lucene Test <em>Article</em> 1 Abstract', $highlightedArticles['3']);
     $searchRequest->setHighlighting(false);
     // Test result set ordering via simple (default field) search.
     $searchRequest->setQuery(array('title' => 'lucene'));
     $searchRequest->setOrderBy('authors');
     $searchRequest->setOrderDir('asc');
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     $scoredResults = isset($results['scoredResults']) ? $results['scoredResults'] : null;
     self::assertEquals(array(4, 3), array_values($scoredResults));
     $searchRequest->setOrderBy('title');
     $searchRequest->setOrderDir('desc');
     $searchRequest->setFacetCategories(array('discipline', 'subject', 'coverage', 'journalTitle', 'authors', 'publicationDate'));
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     $scoredResults = isset($results['scoredResults']) ? $results['scoredResults'] : null;
     self::assertEquals(array(3, 4), array_values($scoredResults));
     // Check faceting results.
     self::assertTrue(isset($results['facets']));
     $facets = $results['facets'];
     self::assertTrue(is_array($facets));
     $expectedFacets = array('discipline' => array('exotic food' => 1, 'dietary research' => 1), 'subject' => array('lunchtime no lunch' => 1), 'coverage' => array('daily probes' => 1, 'the 21st century' => 1, 'world wide' => 1), 'journalTitle' => array(), 'authors' => array('author, some' => 1, 'authorname, second a' => 1, 'author, another' => 1), 'publicationDate' => array('2011' => 1, '2012' => 1));
     self::assertEquals($expectedFacets, $facets);
     $searchRequest->setFacetCategories(array());
     // Test translation of search terms.
     // If the word "und" is not correctly translated to "AND" then
     // the search should return results due to our "implicit OR" strategy.
     // We confirm that by confirming that the two words without the keyword
     // will return something.
     $searchRequest->setQuery(array('galleyFullText' => 'nutella und quatsch'));
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertTrue(empty($results['scoredResults']));
     $searchRequest->setQuery(array('galleyFullText' => 'nutella quatsch'));
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertFalse(empty($results['scoredResults']));
     // Test spelling suggestions.
     $searchRequest->setQuery(array('galleyFullText' => 'Nutela'));
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertTrue(empty($results['spellingSuggestion']));
     $searchRequest->setSpellcheck(true);
     $results = $this->solrWebService->retrieveResults($searchRequest, $totalResults);
     self::assertFalse(empty($results['spellingSuggestion']));
     self::assertEquals('nutella', $results['spellingSuggestion']);
 }