Bento box search with multiple targets
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

114 lines
3.3 KiB

<?php
namespace Drupal\roblib_search_solr_site\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller for d3 graphs.
*/
class RoblibSearchSolrsiteController extends ControllerBase {
/**
* Print JSON.
*
*/
public function bestbetResults($query = NULL) {
print $this->getResults($query, 'BestBet');
exit();
}
/**
* Print JSON.
*
*/
public function generalResults($query = NULL) {
print $this->getResults($query, 'General');
exit();
}
/**
* Query Solr for site results.
* @param string $queryString
* The search term(s) to send to Solr.
* @param string type
* The type of search, either BestBet or General.
* @return string
* A JSON string
*/
function getResults($queryString = NULL, $solr_type = 'General') {
global $base_url;
$config = \Drupal::config('roblib_search_solr_site.settings');
// Url parameters.
if (!isset($queryString)) {
if (isset($_GET['roblib_query'])) {
$queryString = $_GET['roblib_query'];
}
else {
return '';
}
}
$queryString = urlencode($queryString);
// TODO make the index configurable.
$index = \Drupal\search_api\Entity\Index::load('library');
$query = $index->query();
// Change the parse mode for the search.
$parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$parse_mode->setConjunction('OR');
$query->setParseMode($parse_mode);
// Restrict the search to specific languages.
//$query->setLanguages(['en']);
$query->keys($queryString);
if ($solr_type == 'BestBet') {
$query->addCondition('type', 'bestbet', '=');
} else {
$query->addCondition('type', 'bestbet', '<>');
}
// $query->setFulltextFields(['rendered_item', 'title', 'body']);
$query->range(0, 5);
$query->sort('search_api_relevance', 'DESC');
$results = $query->execute();
$output = [];
$output['resultCount'] = $results->getResultCount();
$output['results'] = [];
foreach ($results as $result) {
$id = $result->getId();
$title = $result->getField('title');
if(empty($title)){
break;
}
$titles = $title->getValues();
$output['results'][$id]['title'] = reset($titles);
$body = $result->getField('body')->getValues();
if (!empty($body)) {
$bodyOutput = strip_tags($body[0]->getText());
if (strlen($bodyOutput) > 500) {
$bodyOutput = substr($bodyOutput, 0, 500) . '...';
}
}
$output['results'][$id]['body'] = empty($bodyOutput) ? '' : $bodyOutput;
//$rendered_items = $result->getField('rendered_item')->getValues();
// $output['rendered_item'] = $rendered_items[0]->getText();
$summarys = $result->getField('summary')->getValues();
$output['results'][$id]['summary'] = empty($summmarys) ? '' : reset($summarys);
$type = $result->getField('type')->getValues();
$output['results'][$id]['type'] = reset($type);
$urls = $result->getField('url')->getValues();
$output['results'][$id]['url'] = reset($urls);
$links = $result->getField('field_link')->getValues();
$output['results'][$id]['field_link'] = empty($links) ? '' : $links;
}
echo json_encode($output);
exit();
}
}