Commit c4275c3e authored by Emma's avatar Emma 🦉

highlight words in search results

parent c943936a
Pipeline #54422970 passed with stage
in 1 minute and 44 seconds
......@@ -17,6 +17,25 @@ final class SearchRepository {
Submission::class => 'submission',
];
private const ENTITY_HEADLINES = [
Comment::class => [
'body_excerpt' => [
'document' => 'e.body',
'config' => 'MaxFragments=3',
],
],
Submission::class => [
'title_highlighted' => [
'document' => 'e.title',
'config' => 'HighlightAll=TRUE',
],
'body_excerpt' => [
'document' => 'e.body',
'config' => 'MaxFragments=3',
],
],
];
/**
* @var EntityManagerInterface
*/
......@@ -81,14 +100,27 @@ final class SearchRepository {
$rsm->addScalarResult('entity', 'entity');
$rsm->addScalarResult('search_rank', 'search_rank');
$qb = $this->em->getConnection()->createQueryBuilder();
foreach (self::ENTITY_HEADLINES[$entityClass] as $name => $headline) {
$rsm->addScalarResult($name, $name);
$qb->addSelect(\sprintf(
"ts_headline(%s, search_query, :{$name}_config) AS %s",
$headline['document'],
$name
))->setParameter("{$name}_config", $headline['config'] ?? '');
}
$table = $this->em->getClassMetadata($entityClass)->getTableName();
$qb = $this->em->getConnection()->createQueryBuilder()
->select($rsm->generateSelectClause())
$qb
->addSelect($rsm->generateSelectClause())
->addSelect(":entity::TEXT AS entity")
->addSelect("ts_rank(search_doc, plainto_tsquery(:query::TEXT)) AS search_rank")
->addSelect("ts_rank(search_doc, search_query) AS search_rank")
->from($table, 'e')
->where('search_doc @@ plainto_tsquery(:query)')
->from('plainto_tsquery(:query::TEXT)', 'search_query')
->where('search_doc @@ search_query')
->setParameter('entity', self::ENTITY_TYPES[$entityClass], Type::TEXT)
->setParameter('query', $options['query'], Type::TEXT)
->orderBy('search_rank', 'DESC')
......
......@@ -32,7 +32,11 @@
</h1>
<div class="comment__body break-text">
{{ comment.body|cached_markdown({ context: 'comment', comment: comment })|raw }}
{%- if raw_body is not null -%}
{{ raw_body|raw }}
{%- elseif comment.body is not null -%}
{{ comment.body|cached_markdown({ context: 'comment', comment: comment })|raw }}
{%- endif -%}
</div>
<nav class="comment__nav">
......
......@@ -53,7 +53,11 @@
<header class="submission__header">
<h1 class="submission__title break-text">
<a href="{{ block('submission_url') }}" class="submission__link" {% if submission.url %}rel="nofollow noreferrer noopener" {% if open_external %}target="{{ open_external ? '_blank' }}"{% endif %}{% endif %}>
{{- submission.title -}}
{%- if raw_title is not null -%}
{{ raw_title|raw }}
{%- else -%}
{{ submission.title }}
{%- endif -%}
</a>
{% if submission.sticky %}
......@@ -104,9 +108,13 @@
</header>
{%- if show_body -%}
{% if submission.body is not null %}
{% if raw_body is not null or submission.body is not null %}
<div class="submission__body break-text">
{{- submission.body|cached_markdown({ context: 'submission', submission: submission })|raw -}}
{%- if raw_body is not null -%}
{{ raw_body|raw }}
{%- elseif submission.body is not null -%}
{{ submission.body|cached_markdown({ context: 'submission', submission: submission })|raw }}
{%- endif -%}
</div>
{% endif %}
{%- endif -%}
......
......@@ -4,6 +4,7 @@
{%- with {
comment: comment,
level: 1,
raw_body: options.raw_body ?? null,
recurse: options.recurse ?? false,
show_context: options.show_context ?? false,
show_form: options.show_form ?? false,
......
......@@ -29,9 +29,16 @@
{% for result in results %}
{% if result.entity == 'comment' %}
{{ comment(result[0], { show_context: true }) }}
{{ comment(result[0], {
show_context: true,
raw_body: '<p>%s</p>'|format(result.body_excerpt)
}) }}
{% elseif result.entity == 'submission' %}
{{ submission(result[0], { show_body: true }) }}
{{ submission(result[0], {
show_body: true,
raw_title: result.title_highlighted,
raw_body: result.body_excerpt is not empty ? '<p>%s</p>'|format(result.body_excerpt) : null
}) }}
{% endif %}
{% else %}
{% if query is not empty %}
......
......@@ -3,6 +3,8 @@
{%- macro submission(submission, options) -%}
{%- with {
open_external_links_in_new_tab: options.open_external_links_in_new_tab ?? app.user.openExternalLinksInNewTab ?? false,
raw_body: options.raw_body ?? null,
raw_title: options.raw_title ?? null,
show_body: options.show_body ?? false,
show_forum_name: options.show_forum_name ?? true,
show_thumbnails: options.show_thumbnails ?? app.user.showThumbnails ?? true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment