|
|
|
@ -36,7 +36,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
|
|
|
|
|
* @return string |
|
|
|
|
* A JSON string |
|
|
|
|
*/ |
|
|
|
|
function getResults($queryString = NULL, $type = 'General') { |
|
|
|
|
function getResults($queryString = NULL, $solr_type = 'General') { |
|
|
|
|
global $base_url; |
|
|
|
|
$config = \Drupal::config('roblib_search_solr_site.settings'); |
|
|
|
|
// Url parameters. |
|
|
|
@ -49,7 +49,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
$queryString = urlencode($queryString); |
|
|
|
|
|
|
|
|
|
// TODO make the index configurable. |
|
|
|
|
$index = \Drupal\search_api\Entity\Index::load('default_solr_index'); |
|
|
|
|
$query = $index->query(); |
|
|
|
|
|
|
|
|
@ -60,10 +60,14 @@ class RoblibSearchSolrsiteController extends ControllerBase {
|
|
|
|
|
$query->setParseMode($parse_mode); |
|
|
|
|
|
|
|
|
|
// Restrict the search to specific languages. |
|
|
|
|
$query->setLanguages(['en']); |
|
|
|
|
//$query->setLanguages(['en']); |
|
|
|
|
|
|
|
|
|
// Set fulltext search keywords and fields. |
|
|
|
|
|
|
|
|
|
$query->keys($queryString); |
|
|
|
|
if ($solr_type == 'BestBet') { |
|
|
|
|
$query->addCondition('type', 'bestbet'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// $query->setFulltextFields(['rendered_item', 'title', 'body']); |
|
|
|
|
$query->range(0, 5); |
|
|
|
|
$query->sort('search_api_relevance', 'DESC'); |
|
|
|
@ -78,24 +82,23 @@ class RoblibSearchSolrsiteController extends ControllerBase {
|
|
|
|
|
$titles = $result->getField('title')->getValues(); |
|
|
|
|
$output['results'][$id]['title'] = $titles[0]->getText(); |
|
|
|
|
$body = $result->getField('body')->getValues(); |
|
|
|
|
$bodyOutput = strip_tags($body[0]->getText()); |
|
|
|
|
if (strlen($bodyOutput) > 500) { |
|
|
|
|
$bodyOutput = substr($bodyOutput, 0, 500) . '...'; |
|
|
|
|
if (!empty($body)) { |
|
|
|
|
$bodyOutput = strip_tags($body[0]->getText()); |
|
|
|
|
if (strlen($bodyOutput) > 500) { |
|
|
|
|
$bodyOutput = substr($bodyOutput, 0, 500) . '...'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
$output['results'][$id]['body'] = $bodyOutput; |
|
|
|
|
$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'] = reset($summarys); |
|
|
|
|
$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); |
|
|
|
|
//list(, $path, $langcode) = explode(':', $result->getId()); |
|
|
|
|
//list($entity_type, $id) = explode('/', $path); |
|
|
|
|
//$entity = \Drupal::service('entity_type.manager')->getStorage($entity_type)->load($id); |
|
|
|
|
//teaserOutput = \Drupal::service('entity_type.manager')->getViewBuilder($entity_type)->view($entity, 'teaser'); |
|
|
|
|
//$output['teaser'] = $teaserOutput; |
|
|
|
|
$links = $result->getField('field_link')->getValues(); |
|
|
|
|
$output['results'][$id]['field_link'] = empty($links) ? '' : $links; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
echo json_encode($output); |
|
|
|
|