Skip to content

Commit

Permalink
Merge pull request #4 from Jeroen-G/nested
Browse files Browse the repository at this point in the history
Add period of cartographer as nested
  • Loading branch information
Jeroen-G authored Apr 1, 2022
2 parents 92b558e + 1d5b77e commit 825dd78
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
15 changes: 15 additions & 0 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use App\Http\Requests\SearchFormRequest;
use App\Models\Cartographer;
use JeroenG\Explorer\Domain\Syntax\Compound\BoolQuery;
use JeroenG\Explorer\Domain\Syntax\Matching;
use JeroenG\Explorer\Domain\Syntax\Nested;
use JeroenG\Explorer\Domain\Syntax\Terms;
use JeroenG\Explorer\Infrastructure\Scout\ElasticEngine;

Expand All @@ -16,6 +19,7 @@ public function show()
return view('search', [
'people' => $people,
'countries' => $people->pluck('place')->unique(),
'periods' => $people->pluck('period_name')->unique()->whereNotNull(),
]);
}

Expand All @@ -27,11 +31,22 @@ public function search(SearchFormRequest $request)
$search->must(new Terms('place', $request->get('countries')));
}

if ($request->get('periods')) {
$periodQuery = new BoolQuery();

foreach ($request->get('periods') as $period) {
$periodQuery->must(new Matching('period.name', $period));
}

$search->must(new Nested('period', $periodQuery));
}

$people = $search->take(100)->get();

return view('search', [
'people' => $people,
'countries' => $people->pluck('place')->unique(),
'periods' => $people->pluck('period_name')->unique()->whereNotNull(),
'query' => ElasticEngine::debug()->json(),
]);
}
Expand Down
16 changes: 15 additions & 1 deletion app/Models/Cartographer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Cartographer extends Model implements Explored, BePrepared, IndexSettings,
use HasFactory;
use Searchable;

protected $fillable = ['name', 'place', 'lifespan'];
protected $fillable = ['name', 'place', 'lifespan', 'period_name'];
protected $perPage = 5;

public function mappableAs(): array
Expand All @@ -31,6 +31,20 @@ public function mappableAs(): array
],
'place' => 'keyword',
'lifespan' => 'text',
'period' => 'nested',
];
}

public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'place' => $this->place,
'lifespan' => $this->lifespan,
'period' => [
'name' => $this->period_name,
],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

class AddPeriodNameToCartographersTable extends Migration
{
public function up(): void
{
Schema::table('cartographers', function (Blueprint $table) {
$table->string('period_name')->nullable();
});
}

public function down(): void
{
Schema::table('cartographers', function (Blueprint $table) {
$table->dropColumn('period_name');
});
}
}
30 changes: 13 additions & 17 deletions database/seeders/CartographerSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,35 @@

class CartographerSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
public function run(): void
{
$this->create('Leonardo da Vinci', 'Italy', '1452–1519');
$this->create('Willem Janszoon Blaeu', 'Netherlands', '1571–1638');
$this->create('Leonardo da Vinci', 'Italy', '1452–1519', 'Renaissance');
$this->create('Willem Janszoon Blaeu', 'Netherlands', '1571–1638', 'Renaissance');
$this->create('Johannes Blaeu', 'Netherlands', '1596–1673');
$this->create('Gerardus Mercator', 'Netherlands', '1512–1594');
$this->create('Gerardus Mercator', 'Netherlands', '1512–1594', 'Renaissance');
$this->create('Waldo R. Tobler', 'United States', '1930–2018');
$this->create('al-Idrisi', 'Sicily', '1100–1166');
$this->create('Shen Kuo', 'China', '1031–1095');
$this->create('Anaximander', 'Greek Anatolia', '610BC–546BC');
$this->create('al-Khwārazmī', 'Persia', '780-850');
$this->create('Sebastian Münster', 'Germany', '1488–1552');
$this->create('Abraham Ortelius', 'France', '1527–1598');
$this->create('Sebastian Münster', 'Germany', '1488–1552', 'Renaissance');
$this->create('Abraham Ortelius', 'France', '1527–1598', 'Renaissance');
$this->create('Barbara Petchenik', 'United States', '1939–1992');
$this->create('Johannes Janssonius', 'Netherlands', '1588–1664');
$this->create('Johannes van Keulen', 'Netherlands', '1654–1715');
$this->create('Johann Homann', 'Germany', '1664–1724');
$this->create('Johannes Ruysch', 'Netherlands', '1466–1530');
$this->create('Johannes Werner', 'Germany', '1466–1528');
$this->create('Johannes Honterus', 'Transylvania', '1498–1549');
$this->create('Johannes van Keulen', 'Netherlands', '1654–1715', 'Age of Enlightenment');
$this->create('Johann Homann', 'Germany', '1664–1724', 'Age of Enlightenment');
$this->create('Johannes Ruysch', 'Netherlands', '1466–1530', 'Renaissance');
$this->create('Johannes Werner', 'Germany', '1466–1528', 'Renaissance');
$this->create('Johannes Honterus', 'Transylvania', '1498–1549', 'Renaissance');
}

private function create(string $name, string $place, string $lifespan): Cartographer
private function create(string $name, string $place, string $lifespan, ?string $periodName = null): Cartographer
{
return Cartographer::factory()->create([
'name' => $name,
'place' => $place,
'lifespan' => $lifespan,
'period_name' => $periodName,
]);
}
}
10 changes: 10 additions & 0 deletions resources/views/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
@endforeach
</fieldset>

<fieldset class="my-5 flex flex-col">
<div class="flex flex-col text-gray-500">Periods</div>
@foreach($periods as $period)
<label>
<input type="checkbox" name="periods[]" value="{{ $period }}" />
{{ $period }}
</label>
@endforeach
</fieldset>

<fieldset class="my-5">
<button class="bg-blue-300 font-medium px-10 py-2 rounded mt-2" type="submit">Search</button>
</fieldset>
Expand Down

0 comments on commit 825dd78

Please sign in to comment.