tethys/app/Http/Controllers/Frontend/SearchController.php

159 lines
5.0 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
2018-09-07 13:31:05 +00:00
namespace App\Http\Controllers\Frontend;
2018-08-06 12:30:51 +00:00
use App\Book;
2018-09-10 13:09:10 +00:00
use App\Models\Dataset;
2018-09-07 13:31:05 +00:00
use App\Http\Controllers\Controller;
2018-08-06 12:30:51 +00:00
use App\Library\Search\Navigation;
use App\Library\Util\SolrSearchSearcher;
2018-09-07 13:31:05 +00:00
use Illuminate\Http\Request;
2018-08-06 12:30:51 +00:00
use Illuminate\Support\Facades\Log;
2018-09-07 13:31:05 +00:00
use Illuminate\View\View;
2018-08-06 12:30:51 +00:00
class SearchController extends Controller
{
2018-09-07 13:31:05 +00:00
private $query;
private $numOfHits;
private $searchtype;
private $resultList;
private $facetMenu;
2018-08-06 12:30:51 +00:00
protected $client;
/**
* Initialize controller.
*/
public function __construct(\Solarium\Client $client)
{
$this->client = $client;
// $config = config('solarium');
// $config = array(
// 'endpoint' => array(
// 'localhost' => array(
// 'host' => '127.0.0.1',
// 'port' => '8983',
// 'path' => '/solr/#',
// 'core' => 'opus4'
// )
// )
// );
// $this->client = new \Solarium\Client($config);
}
public function ping()
{
// create a ping query
$ping = $this->client->createPing();
// execute the ping query
try {
$this->client->ping($ping);
return response()->json('OK');
} catch (\Solarium\Exception\HttpException $e) {
return response()->json('ERROR', 500);
}
}
2018-09-07 13:31:05 +00:00
public function search1(Request $request): View
2018-08-06 12:30:51 +00:00
{
$this->_request = $request;
2018-09-07 13:31:05 +00:00
$data = $request->all();
//$this->searchtype = $request->input('searchtype');
$this->searchtype = $request->input('searchtype');
return view('frontend.solrsearch.index');
2018-08-06 12:30:51 +00:00
}
2018-09-07 13:31:05 +00:00
public function search(Request $request): View
2018-08-06 12:30:51 +00:00
{
Log::info('Received new search request. Redirecting to search action of IndexController.');
$this->_request = $request;
//$filter =$request->input('query');
// $query = $this->client->createSelect();
// $query->setQuery('%P1%', array($filter));
// // $query->setQuery('*:*');
// $results = $this->client->select($query);
// // // display the total number of documents found by solr
// echo 'NumFound: ' .$results->getNumFound();
2018-09-07 13:31:05 +00:00
//$this->query = Navigation::getQueryUrl($request);
2018-08-06 12:30:51 +00:00
$query = $this->buildQuery();
if (!is_null($query)) {
2018-09-07 13:31:05 +00:00
$this->query = $query;
2018-08-06 12:30:51 +00:00
$this->performSearch();
// set start and rows param (comparable to SQL limit) using fluent interface
//$query->setStart(2)->setRows(20);
// set fields to fetch (this overrides the default setting 'all fields')
//$query->setFields(array('id','year'));
2018-09-07 13:31:05 +00:00
$results = $this->resultList->getResults();
$numOfHits = $this->numOfHits;
2018-08-06 12:30:51 +00:00
2018-09-07 13:31:05 +00:00
return view('frontend.solrsearch.index', compact('results', 'numOfHits'));
2018-08-06 12:30:51 +00:00
}
2018-09-07 13:31:05 +00:00
return view('frontend.solrsearch.index');
2018-08-06 12:30:51 +00:00
}
/**
* Displays simple search form.
*/
2018-09-07 13:31:05 +00:00
public function index(): View
2018-08-06 12:30:51 +00:00
{
$totalNumOfDocs = Dataset::count();
2018-09-07 13:31:05 +00:00
return view('frontend.solrsearch.index', compact('totalNumOfDocs'));
2018-08-06 12:30:51 +00:00
}
2018-09-07 13:31:05 +00:00
public function searchDb(Request $request): View
2018-08-06 12:30:51 +00:00
{
$searchType = "simple";
$params = $request->all();
//build query
2018-09-07 13:31:05 +00:00
$this->searchtype = $request->input('searchtype');
2018-08-06 12:30:51 +00:00
// Gets the query string from our form submission
//$query = Request::input('search');
$filter = $request->input('search');
//$query = Input::get('search', '');
// Returns an array of articles that have the query string located somewhere within
// our articles titles. Paginates them so we can break up lots of search results.
$books = Book::where('title', 'LIKE', '%' . $filter . '%')
2018-09-07 13:31:05 +00:00
->get(); //paginate(10);
2018-08-06 12:30:51 +00:00
// returns a view and passes the view the list of articles and the original query.
2018-09-07 13:31:05 +00:00
return view('frontend.solrsearch.index', compact('books'));
2018-08-06 12:30:51 +00:00
}
#region private helper
private function buildQuery()
{
$request = $this->_request;
2018-09-07 13:31:05 +00:00
$this->searchtype = $request->input('searchtype');
2018-08-06 12:30:51 +00:00
return Navigation::getQueryUrl($request);
}
/**
* TODO this should happen in model class so it can be tested directly
*/
private function performSearch()
{
//$this->getLogger()->debug('performing search');
try {
$searcher = new SolrSearchSearcher();
2018-09-07 13:31:05 +00:00
// $openFacets = $this->facetMenu->buildFacetArray( $this->getRequest()->getParams() );
2018-08-06 12:30:51 +00:00
// $searcher->setFacetArray($openFacets);
2018-09-07 13:31:05 +00:00
$this->resultList = $searcher->search($this->query);
2018-08-06 12:30:51 +00:00
// $this->view->openFacets = $openFacets;
} catch (Exception $e) {
// $this->getLogger()->err(__METHOD__ . ' : ' . $e);
//throw new Application_SearchException($e);
echo 'Exception abgefangen: ', $e->getMessage(), "\n";
}
2018-09-07 13:31:05 +00:00
$this->numOfHits = $this->resultList->getNumberOfHits();
2018-08-06 12:30:51 +00:00
}
#endregion private helper
}