Skip to content

Commit

Permalink
[#21] Legger til felt for emneord
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Oct 13, 2019
1 parent c24b61a commit 5404580
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/LitteraturkritikkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ public function autocomplete(Request $request, LitteraturkritikkSchema $schema)
break;

case 'kritikktype':
case 'tags':
# Ref: https://stackoverflow.com/a/31757242/489916
# for the #>> '{}' magick
$results = \DB::select("
select
distinct jd.value #>> '{}' as value
from
litteraturkritikk_records,
jsonb_array_elements(litteraturkritikk_records.kritikktype) as jd
jsonb_array_elements(litteraturkritikk_records.${fieldName}) as jd
order by value
");
foreach ($results as $row) {
Expand Down
18 changes: 17 additions & 1 deletion app/Litteraturkritikk/LitteraturkritikkSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class LitteraturkritikkSchema extends Schema

'searchOptions' => [
'type' => 'autocomplete',
'placeholder' => 'F.eks. teaterkritikk, forfatterportrett, ...',
'placeholder' => '',
'index' => [
'type' => 'array',
'column' => 'kritikktype',
Expand Down Expand Up @@ -320,6 +320,22 @@ class LitteraturkritikkSchema extends Schema

'searchable' => 'advanced',
],

// Emneord
[
'key' => 'tags',
'type' => 'tags',
'defaultValue' => [],

'searchOptions' => [
'type' => 'autocomplete',
'placeholder' => 'F.eks. teaterkritikk, forfatterportrett, ...',
'index' => [
'type' => 'array',
'column' => 'tags',
],
],
],
],
],

Expand Down
1 change: 1 addition & 0 deletions app/Litteraturkritikk/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Record extends \App\Record
*/
protected $casts = [
'kritikktype' => 'array',
'tags' => 'array',
];

public function createdBy()
Expand Down
144 changes: 144 additions & 0 deletions database/migrations/2019_10_13_181954_add_tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddTags extends Migration
{
protected function dropView()
{
DB::unprepared('DROP MATERIALIZED VIEW litteraturkritikk_records_search');

}

protected function recreateView($withTags)
{
DB::unprepared("
CREATE MATERIALIZED VIEW litteraturkritikk_records_search AS
SELECT
r.*,
SUBSTR(TRIM(dato),1,4) AS dato_numeric,
-- Flat representasjon for tabellvisning
STRING_AGG(DISTINCT forfatter.etternavn_fornavn, '; ') AS verk_forfatter,
STRING_AGG(DISTINCT kritiker.etternavn_fornavn, '; ') AS kritiker,
STRING_AGG(DISTINCT forfatter.kjonn, '; ') AS forfatter_kjonn,
STRING_AGG(DISTINCT kritiker.kjonn, '; ') AS kritiker_kjonn,
-- Søkeindeks 'any_field_ts'
TO_TSVECTOR('simple', r.id::text)
|| TO_TSVECTOR('simple', COALESCE(r.tittel, ''))
|| TO_TSVECTOR('simple', COALESCE(r.publikasjon, ''))
|| TO_TSVECTOR('simple', COALESCE(r.dato, ''))
|| TO_TSVECTOR('simple', COALESCE(r.bind, ''))
|| TO_TSVECTOR('simple', COALESCE(r.hefte, ''))
|| TO_TSVECTOR('simple', COALESCE(r.sidetall, ''))
|| TO_TSVECTOR('simple', COALESCE(r.kommentar, ''))
|| TO_TSVECTOR('simple', COALESCE(r.utgivelseskommentar, ''))
|| TO_TSVECTOR('simple', r.kritikktype::text)
" . ($withTags ? "|| TO_TSVECTOR('simple', r.tags::text)" : "") . "
|| TO_TSVECTOR('simple', COALESCE(r.verk_tittel, ''))
|| TO_TSVECTOR('simple', COALESCE(r.verk_dato, ''))
|| TO_TSVECTOR('simple', COALESCE(r.verk_kommentar, ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(person_pivot.kommentar, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(person_pivot.pseudonym, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(person.etternavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(person.fornavn, ' '), ''))
AS any_field_ts,
-- Søkeindeks 'verk_tittel_ts'
TO_TSVECTOR('simple', COALESCE(r.verk_tittel, ''))
AS verk_tittel_ts,
-- Søkeindeks 'forfatter_ts'.
TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT forfatter.etternavn_fornavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT forfatter.fornavn_etternavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT forfatter_pivot.pseudonym, ' '), ''))
AS forfatter_ts,
-- Søkeindeks 'kritiker_ts'
TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT kritiker.etternavn_fornavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT kritiker.fornavn_etternavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT kritiker_pivot.pseudonym, ' '), ''))
AS kritiker_ts,
-- Søkeindeks 'person_ts'
TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT person.etternavn_fornavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT person.fornavn_etternavn, ' '), ''))
|| TO_TSVECTOR('simple', COALESCE(STRING_AGG(DISTINCT person_pivot.pseudonym, ' '), ''))
AS person_ts
FROM litteraturkritikk_records AS r
-- person
LEFT JOIN litteraturkritikk_record_person AS person_pivot
ON r.id = person_pivot.record_id
LEFT JOIN litteraturkritikk_personer_view AS person
ON person.id = person_pivot.person_id
-- kritiker
LEFT JOIN litteraturkritikk_record_person AS kritiker_pivot
ON r.id = kritiker_pivot.record_id
AND kritiker_pivot.person_role = 'kritiker'
LEFT JOIN litteraturkritikk_personer_view AS kritiker
ON kritiker.id = kritiker_pivot.person_id
-- forfatter
LEFT JOIN litteraturkritikk_record_person AS forfatter_pivot
ON forfatter_pivot.record_id = r.id
AND forfatter_pivot.person_role != 'kritiker'
LEFT JOIN litteraturkritikk_personer_view AS forfatter
ON forfatter.id = forfatter_pivot.person_id
GROUP BY r.id
");

DB::unprepared('CREATE UNIQUE INDEX litteraturkritikk_records_search_id ON litteraturkritikk_records_search (id)');
DB::unprepared('CREATE INDEX litteraturkritikk_any_field_ts_idx ON litteraturkritikk_records_search USING gin(any_field_ts)');
DB::unprepared('CREATE INDEX litteraturkritikk_forfatter_ts_idx ON litteraturkritikk_records_search USING gin(forfatter_ts)');
DB::unprepared('CREATE INDEX litteraturkritikk_kritiker_ts_idx ON litteraturkritikk_records_search USING gin(kritiker_ts)');
DB::unprepared('CREATE INDEX litteraturkritikk_person_ts_idx ON litteraturkritikk_records_search USING gin(person_ts)');

Schema::table('litteraturkritikk_records_search', function ($view) {
$view->index('publikasjon');
$view->index('verk_sjanger');
$view->index('kritikktype');
$view->index('spraak');
});
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->dropView();
Schema::table('litteraturkritikk_records', function (Blueprint $table) {
$table->jsonb('tags')->default('[]');
});
$this->recreateView(true);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->dropView();
Schema::table('litteraturkritikk_records', function (Blueprint $table) {
$table->dropColumn('tags');
});
$this->recreateView(false);
}
}
1 change: 1 addition & 0 deletions resources/lang/nb/litteraturkritikk.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'person' => 'Person (forfatter, kritiker e.a.)',

'kritikktype' => 'Type',
'tags' => 'Emneord',
'utgivelsessted' => 'Utgivelsessted',
'spraak' => 'Språk',

Expand Down
4 changes: 3 additions & 1 deletion resources/views/litteraturkritikk/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@

@elseif (is_array($record->{$field->key}))

{{ implode(', ', $record->{$field->key}) }}
@foreach ($record->{$field->key} as $value)
<a class="badge badge-primary" href="{{ action('LitteraturkritikkController@index', ['f0' => 'tags', 'v0' => $value]) }}">{{ $value }}</a>
@endforeach

@elseif ($field->key == 'created_at')

Expand Down

0 comments on commit 5404580

Please sign in to comment.