Skip to content

Commit

Permalink
Merge pull request #4 from indic-archive/results
Browse files Browse the repository at this point in the history
Add search results page
  • Loading branch information
joeirimpan authored Feb 25, 2023
2 parents 60dac25 + 35d2991 commit 0431902
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public function updateSearchIndex(Event $event): void

if ($request->getOperation() == 'create' || $request->getOperation() == 'update') {
$updated = $response->getContent();

// skip private items
if (!$updated->isPublic()) {
return;
}
$document = [
'resource_id' => strval($updated->getId()),
];
Expand Down
7 changes: 5 additions & 2 deletions asset/js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,12 @@
}
self._words[self._idx].className += " ac__word--selected";
} else if (key == 13) {
if (self._words.length && self._idx > -1)
if (self._words.length && self._idx > -1) {
self._onSelectedCB(_getStringFromWordElement(self._words[self._idx]));
else self._onSelectedCB(self._input.value);
} else {
var url = document.querySelector("#search-form").dataset.resultsUrl;
self._onSelectedCB(url + "?query=" + self._input.value);
}
} else if (self._input.value || key === 8) {
self._idx = -1;
let prefix = encodeURIComponent(self._input.value);
Expand Down
1 change: 0 additions & 1 deletion asset/public/bundle-08009383e5.js

This file was deleted.

1 change: 1 addition & 0 deletions asset/public/bundle-1141ea3798.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
'action' => 'search',
],
],
],
'results' => [
'type' => Literal::class,
'options' => [
'route' => '/search-results',
'defaults' => [
'__NAMESPACE__' => 'TypesenseSearch\Controller',
'controller' => Controller\SearchController::class,
'action' => 'results',
],
],
]
]
],
Expand Down
2 changes: 1 addition & 1 deletion config/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ author_link = "https://github.com/indic-archive"
module_link = "https://github.com/indic-archive/omeka-typesense-search"
support_link = "https://github.com/indic-archive/omeka-typesense-search/issues"
configurable = true
version = "0.0.2"
version = "0.0.3"
omeka_version_constraint = "^3.1.0"
2 changes: 1 addition & 1 deletion rev-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"bundle.js": "bundle-08009383e5.js",
"bundle.js": "bundle-1141ea3798.js",
"stylesheet.css": "stylesheet-c83bed661b.css"
}
55 changes: 55 additions & 0 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Omeka\Stdlib\Message;
use Laminas\Log\Logger;
use Laminas\View\Model\JsonModel;
use Laminas\View\Model\ViewModel;

/**
* Controller for search action that returns a json response.
Expand Down Expand Up @@ -115,6 +116,60 @@ public function searchAction()
]);
}

public function resultsAction()
{
$searchQ = $this->params()->fromQuery('query');

if ($searchQ == "*") {
return new ViewModel([
'items' => [],
]);
}

// convert [dcterms:title,dcterms:alternative] => 'dcterms_title,dcterms_alternative'
// get weights by the order of property
$result = array();
$weights = array();
$propertyCt = count($this->indexProperties);
foreach (array_values($this->indexProperties) as $i => $property) {
$result[] = str_replace(':', '_', $property);
$weights[] = strval($propertyCt - $i);
}
$queryBy = implode(',', $result);
$queryByWeights = implode(',', $weights);

try {
$results = $this->client->collections[$this->indexName]->documents->search(
[
'q' => $searchQ,
'query_by' => $queryBy,
'query_by_weights' => $queryByWeights,
'highlight_full_fields' => 'dcterms_title',
'page' => 1,
'per_page' => 15,
'infix' => 'fallback',
//'sort_by' => 'dcterms_issued:desc',
],
);
} catch (\Exception $e) {
//$this->logger->err(new Message('Error searching index #%s, err: %s', $this->indexName, $e->getMessage()));
return new ViewModel([
'items' => [],
]);
}

// collect ids from search results and read the resource
$ids = array();
foreach ($results['hits'] as $hit) {
array_push($ids, (int)$hit['document']['resource_id']);
}
$results = $this->api()->search('items', ['id' => $ids]);

return new ViewModel([
'items' => $results->getContent(),
]);
}

public function dropIndexAction()
{
$jobArgs = [];
Expand Down
3 changes: 2 additions & 1 deletion view/common/search-form.phtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
$siteSlug = $this->params()->fromRoute('site-slug');
$searchAction = $this->url('site/search', ['site-slug' => $siteSlug, 'controller' => 'SearchController', 'action' => 'search'], false);
$resultsAction = $this->url('site/results', ['site-slug' => $siteSlug, 'controller' => 'SearchController', 'action' => 'results'], false);
?>

<!-- this overrides the default search -->
<div>
<form id="search-form" data-url="<?php echo $this->escapeHtml($searchAction); ?>" onsubmit="event.preventDefault();">
<form id="search-form" data-url="<?php echo $this->escapeHtml($searchAction); ?>" data-results-url="<?php echo $this->escapeHtml($resultsAction); ?>" onsubmit="event.preventDefault();">
<input class="search-text" type="text" autofocus>
<button type="submit"><?php echo 'Search'; ?></button>
</form>
Expand Down
34 changes: 34 additions & 0 deletions view/typesense-search/search/results.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
$siteSlug = $this->params()->fromRoute('site-slug');

$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$hyperlink = $this->plugin('hyperlink');
$assetUrl = $this->plugin('assetUrl');
$thumbnail = $this->plugin('thumbnail');
$filterLocale = (bool) $this->siteSetting('filter_locale_values');
$lang = $this->lang();

$defaultThumbnail = '<img src="' . $assetUrl('thumbnails/default.png', 'Omeka', true) . '" title="' . $translate('No media') . '"/>';
?>
<div class="item-set-items">
<ul class="resource-list">
<?php
$headingTerm = $this->siteSetting('browse_heading_property_term');
$bodyTerm = $this->siteSetting('browse_body_property_term');
foreach ($items as $item):
$heading = $headingTerm ? $item->value($headingTerm, ['default' => $translate('[Untitled]'), 'lang' => ($filterLocale ? [$lang, ''] : null)]) : $item->displayTitle(null, ($filterLocale ? [$lang, ''] : null));
$body = $bodyTerm ? $item->value($bodyTerm, ['lang' => ($filterLocale ? [$lang, ''] : null)]) : $item->displayDescription(null, ($filterLocale ? [$lang, ''] : null));
$resourceUrl = $item->siteUrl($siteSlug);
$resourceThumbnail = $thumbnail($item, 'medium') ?: $defaultThumbnail;
?>
<li class="item resource">
<?= $hyperlink->raw($resourceThumbnail, $resourceUrl, ['class' => 'resource-link']) ?>
<h4><?php echo $item->link($heading); ?></h4>
<?php if ($body): ?>
<div class="description"><?php echo $escape($body); ?></div>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>

0 comments on commit 0431902

Please sign in to comment.