Browse Source

bento solr site now returns results to the block

d9
ppound 3 years ago
parent
commit
f94c4fbc1e
  1. 82
      modules/solr_site/js/solr_site_results.js
  2. 1
      modules/solr_site/roblib_search_solr_site.libraries.yml
  3. 4
      modules/solr_site/roblib_search_solr_site.module
  4. 81
      modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php
  5. 26
      modules/solr_site/src/Form/SolrsiteSettingsForm.php
  6. 12
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php
  7. 26
      modules/solr_site/templates/roblib-search-solr-site-results.html.twig
  8. 43
      modules/solr_site/templates/theme.inc

82
modules/solr_site/js/solr_site_results.js

@ -1,47 +1,43 @@
Drupal.behaviors.roblib_search_solr_site_results = {
attach: function (context, settings) {
url = settings.roblib_search_solr_site_results.search_url;
query = settings.roblib_search_solr_site_results.search_query;
baseUrl = settings.roblib_search_solr_site_results.base_url;
default_site_type = settings.roblib_search_solr_site_results.default_site_type + '/';
jQuery.getJSON(url, function (data) {
var items = [];
total_found = data.response.numFound;
var numberOfDocs = 0;
try {
numberOfDocs = data.response.docs.length;
} catch (err) {
// do nothing leave docLength at 0
}
if (numberOfDocs < 1) {
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append('No Results');
jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-results').hide();
} else {
jQuery('#' + 'roblib-search-content-solr-site-results').empty();
var counter = 0;
var divs = new Array();
var content = new Array();
jQuery.each(data.response.docs, function (key, val) {
id = 'roblib_search_solr_results_' + counter;
content[counter] = val.teaser;
divs[counter++] = id;
items.push('<div class ="roblib-search-row" id="' + id + '">\n\
attach: function (context, settings) {
if (context == document) {
query = $current_path.replace('roblib_search/', '');
solrUrl = settings.solr_site_search_url + query;
baseUrl = settings.solr_site_base_url + query;
jQuery.getJSON(baseUrl, function (data) {
var items = [];
total_found = data.resultCount;
if (total_found < 1) {
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append('No Results');
jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-results').hide();
} else {
jQuery('#' + 'roblib-search-content-solr-site-results').empty();
var counter = 0;
var divs = new Array();
var content = new Array();
jQuery.each(data.results, function (key, val) {
id = 'roblib_search_solr_results_' + counter;
content[counter] = val.teaser;
divs[counter++] = id;
items.push('<div class ="roblib-search-row" id="' + id + '">\n\
<span class="roblib-title results">\n\
<a href="' + val.url + '">' + val.label + '</a></span>' +
'\n\<span class="roblib-solr-bundle">(' + val.bundle_name + ')</span> <span class="roblib-solr-teaser"> ' + val.teaser + '</span></div>');
});
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append(items.join(''));
}
if (numberOfDocs > 0) {
results_url = '<a href="' + baseUrl + '/search/' + default_site_type + query + '?f[0]=(-bundle:bestbet)"' + '>see all results ('
+ total_found + ') </a>';
move_me_div_url = '<a href="' + baseUrl + '/search/' + default_site_type + query + '?f[0]=(-bundle:bestbet)"' + '>Library Site ('
+ total_found + ') </a>';
jQuery('#' + 'roblib-search-solr-site-results-more').empty().append(results_url);
jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url);
jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url);
}
});
}
<a href="' + val.url + '">' + val.title + '</a></span>' +
'\n\<span class="roblib-solr-bundle">(' + val.type + ')</span> <span class="roblib-solr-teaser"> ' + val.body + '</span></div>');
});
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append(items.join(''));
}
if (total_found > 0) {
results_url = '<a href="' + solrUrl + '">see all results ('
+ total_found + ') </a>';
move_me_div_url = '<a href="' + solrUrl + '">Library Site ('
+ total_found + ') </a>';
jQuery('#' + 'roblib-search-solr-site-results-more').empty().append(results_url);
jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url);
jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url);
}
});
}
}
}

1
modules/solr_site/roblib_search_solr_site.libraries.yml

@ -5,6 +5,5 @@ solr-site:
css/roblib_search_solr_site.css: {}
js:
js/solr_site_results.js: {}
js/solr_site_bestbet.js: {}
dependencies:
- core/drupal

4
modules/solr_site/roblib_search_solr_site.module

@ -15,8 +15,8 @@ function roblib_search_solr_site_theme($existing, $type, $theme, $path) {
->getPath();
$spinner_path = '/' . $modulePath . "/img/spinner.gif";
return [
'roblib_search_solr_site' => [
'variables' => ['query' => NULL, 'spinner_path' => $spinner_path],
'roblib_search_solr_site_results' => [
'variables' => ['solr_type' => NULL, 'spinner_path' => $spinner_path],
],
];
}

81
modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php

@ -29,42 +29,77 @@ class RoblibSearchSolrsiteController extends ControllerBase {
/**
* Query Solr for site results.
* @param string $query
* @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($query = NULL, $type = 'General') {
function getResults($queryString = NULL, $type = 'General') {
global $base_url;
$config = \Drupal::config('roblib_search_solr_site.settings');
$config = \Drupal::config('roblib_search_solr_site.settings');
// Url parameters.
if (!isset($query)) {
if (!isset($queryString)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
$queryString = $_GET['roblib_query'];
}
else {
return '';
}
}
$query = urlencode($query);
/* $num_results = variable_get('roblib_search_solr_site_num_results', '5');
$query = apachesolr_drupal_query("apachesolr");
$encoded_query = htmlentities($query_string, ENT_QUOTES);
$query->addParam('q', $encoded_query); // keyword to be searched
$query->addParam('rows', $num_results);
$query->addParam('fq', $type);
$query->addParam('mm', '1');
apachesolr_search_add_boost_params($query);
$response = $query->search();
$json_results = drupal_json_encode($response);
return $json_results;*/
$search_url = $base_url . '/roblib_search/solr_site/' . $type . '/' . $query;
return json_encode($query);
$queryString = urlencode($queryString);
$index = \Drupal\search_api\Entity\Index::load('default_solr_index');
$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']);
// Set fulltext search keywords and fields.
$query->keys($queryString);
// $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();
$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) . '...';
}
$output['results'][$id]['body'] = $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);
$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;
}
echo json_encode($output);
exit();
}
}

26
modules/solr_site/src/Form/SolrsiteSettingsForm.php

@ -16,38 +16,20 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function getFormId() {
return 'roblib_search_eds_settings_form';
return 'roblib_search_solr_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search_eds.settings');
/*
*
* $form['roblib_search_solr_site_url'] = array(
'#type' => 'textfield',
'#title' => t('Solr url'),
'#default_value' => variable_get('roblib_search_solr_site_url', 'http://localhost:8983/solr'),
'#description' => t('The base Solr URL, for example http://localhost:8983/solr'),
'#required' => TRUE,
);
$form['roblib_search_solr_site_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#default_value' => variable_get('roblib_search_solr_site_num_results', '5'),
'#description' => t('The number of results to display in the Bento box'),
'#required' => TRUE,
);
$config = \Drupal::config('roblib_search_solr.settings');
return system_settings_form($form);
*/
$form['solr_site_url'] = array(
'#type' => 'textfield',
'#title' => t('Solr URL'),
'#default_value' => $config->get('solr_site_url'),
'#description' => t('The base Solr URL, for example http://localhost:8983/solr'),
'#description' => t('The base Solr URL, for example /search/node?keys='),
'#required' => TRUE,
);
@ -77,7 +59,7 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search_eds.settings');
$config = \Drupal::configFactory()->getEditable('roblib_search_solr.settings');
$config->set('solr_site_url', $form_state->getValue('solr_site_url'))->save();
$config->set('solr_site_num_results', $form_state->getValue('solr_site_num_results'))->save();
}

12
modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php

@ -19,18 +19,20 @@ class RoblibSearchSolrsiteGeneral extends BlockBase {
* {@inheritdoc}
*/
public function build() {
// $host = \Drupal::request()->getSchemeAndHttpHost();
$search_url = \Drupal::config('roblib_search_solr_site.settings')
$host = \Drupal::request()->getSchemeAndHttpHost();
$search_url = \Drupal::config('roblib_search_solr.settings')
->get('solr_site_url');
$settings = [
'solr_site_search_url' => $search_url,
'solr_site_base_url' => $host . '/roblib_search/solr_site/general/',
];
return [
'#theme' => 'roblib_search_solr_site',
'query' => $query,
'#theme' => 'roblib_search_solr_site_results',
//'query' => $query,
'#solr_type' => 'results',
'#attached' => [
'library' => [
'roblib_search_solr_site/solr_site'
'roblib_search_solr_site/solr-site'
],
'drupalSettings' => $settings,
],

26
modules/solr_site/templates/roblib-search-solr-site-results.html.twig

@ -1,24 +1,8 @@
<?php
/**
* @file roblib-search-solr-site-results.tpl.php
*
*
* Variables available:
* - $variables: all array elements of $variables can be used as a variable. e.g. $base_url equals $variables['base_url']
*
*/
?>
<?
// Get rid of warnings in admin interface
if (!isset($type)){
$type = 'results';
}
?>
<div class ="roblib-search-more" id="roblib-search-solr-site-<?php echo $type;?>-more"></div>
<div class ="roblib-search-content solr-site-<?php echo $type;?>" id="roblib-search-content-solr-site-<?php echo $type;?>">
<img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
<div class ="roblib-search-more" id="roblib-search-solr-site-{{ solr_type }}-more"></div>
<div class ="roblib-search-content solr-site-{{ solr_type }}" id="roblib-search-content-solr-site-{{ solr_type }}">
<img src="{{ spinner_path }}"/>
</div>
<div id="roblib-solr-more-results-<?php print($type)?>" class="roblib-solr-more-button "></div>
<div id="roblib-solr-search-toc-<?php print($type)?>" class="roblib-bento-toc moveme-please"></div>
<div id="roblib-solr-more-results-{{ solr_type }}" class="roblib-solr-more-button "></div>
<div id="roblib-solr-search-toc-{{ solr_type }}" class="roblib-bento-toc moveme-please"></div>

43
modules/solr_site/templates/theme.inc

@ -1,43 +0,0 @@
<?php
/*
*
*/
/**
* @param array $variables
* @param string $type
*
* @return string
*/
function roblib_search_solr_site_preprocess_roblib_search_solr_site(&$variables){
//function roblib_search_solr_site_preprocessor(&$variables, $type) {
global $base_url;
$type = $variables['type'];
if (!isset($query)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
return '';
}
}
$spinner_path = $base_url . '/' . drupal_get_path('module', 'roblib_search') . '/img/' . 'spinner.gif';
$variables['spinner_path'] = $spinner_path;
$variables['type'] = $type;
$query = urlencode($query);
$search_url = $base_url . '/roblib_search/solr_site/' . $type . '/' . $query;
drupal_add_js(drupal_get_path('module', 'roblib_search_solr_site') . '/js/solr_site_' . $type . '.js');
$key = 'roblib_search_solr_site_' . $type;
$default_module = variable_get('search_default_module', 'site') == 'apachesolr_search'
? 'site' : variable_get('search_default_module', 'site');
drupal_add_js(array(
$key => array(
'search_url' => $search_url,
'search_query' => $query,
'base_url' => $base_url,
'default_site_type' => $default_module
)
), array('type' => 'setting'));
}
Loading…
Cancel
Save