Skip to content

Commit c47d7d0

Browse files
committed
Merge branch 'release/v2.3.6'
2 parents d201de1 + 8814f33 commit c47d7d0

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

app/Models/Page.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function toSearchableArray(): array
7070
$contentSections = [];
7171
foreach ($this->content as $sectionLabel => $sectionContent) {
7272
$content = [];
73-
foreach ($sectionContent['content'] as $i => $contentBlock) {
73+
foreach ($sectionContent['content'] ?? [] as $i => $contentBlock) {
7474
switch ($contentBlock['type']) {
7575
case 'copy':
7676
$content[] = $this->makeSearchable($contentBlock['value']);

app/Search/ElasticSearch/CollectionCategoryQueryBuilder.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types=1);
3+
declare (strict_types=1);
44

55
namespace App\Search\ElasticSearch;
66

@@ -25,10 +25,10 @@ public function __construct()
2525
],
2626
'functions' => [
2727
[
28-
'field_value_factor' => [
29-
'field' => 'score',
30-
'missing' => 1,
31-
'modifier' => 'ln1p',
28+
'script_score' => [
29+
'script' => [
30+
'source' => "doc['score'].size() == 0 ? 1 : ((doc['score'].value + 1) * 0.1) + 1",
31+
],
3232
],
3333
],
3434
],

app/Search/ElasticSearch/CollectionPersonaQueryBuilder.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types=1);
3+
declare (strict_types=1);
44

55
namespace App\Search\ElasticSearch;
66

@@ -25,10 +25,10 @@ public function __construct()
2525
],
2626
'functions' => [
2727
[
28-
'field_value_factor' => [
29-
'field' => 'score',
30-
'missing' => 1,
31-
'modifier' => 'ln1p',
28+
'script_score' => [
29+
'script' => [
30+
'source' => "doc['score'].size() == 0 ? 1 : ((doc['score'].value + 1) * 0.1) + 1",
31+
],
3232
],
3333
],
3434
],

app/Search/ElasticSearch/ServiceQueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct()
2929
[
3030
'script_score' => [
3131
'script' => [
32-
'source' => "((doc['score'].value + 1) * 0.1) + 1",
32+
'source' => "doc['score'].size() == 0 ? 1 : ((doc['score'].value + 1) * 0.1) + 1",
3333
],
3434
],
3535
],

database/factories/ServiceFactory.php

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function definition(): array
2828
'status' => Service::STATUS_ACTIVE,
2929
'intro' => mb_substr($this->faker->paragraph(2), 0, 149),
3030
'description' => $this->faker->paragraph(5),
31-
// 'intro' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
32-
// 'description' => 'Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.',
3331
'is_free' => true,
3432
'url' => $this->faker->url(),
3533
'contact_name' => $this->faker->name(),

tests/Feature/Search/ServiceTest.php

+65
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature\Search;
44

5+
use Carbon\Carbon;
56
use Tests\TestCase;
67
use App\Models\Service;
78
use App\Models\Taxonomy;
@@ -1452,4 +1453,68 @@ public function test_service_scores_are_secondary_to_distance(): void
14521453
$this->assertEquals($service0->id, $data[0]['id']);
14531454
$this->assertEquals($service5->id, $data[1]['id']);
14541455
}
1456+
1457+
/**
1458+
* @test
1459+
*/
1460+
public function freshnessOfServiceDoesNotAffectSearchResult()
1461+
{
1462+
$organisation = \App\Models\Organisation::factory()->create();
1463+
$serviceParams = [
1464+
'organisation_id' => $organisation->id,
1465+
'name' => 'Royal Organs Hike',
1466+
];
1467+
$service5 = Service::factory()->create(array_merge($serviceParams, [
1468+
'score' => 5,
1469+
'created_at' => Carbon::now()->subMonths(24),
1470+
'updated_at' => Carbon::now()->subMonths(24),
1471+
'last_modified_at' => Carbon::now()->subMonths(24),
1472+
]));
1473+
$service4 = Service::factory()->create(array_merge($serviceParams, [
1474+
'score' => 4,
1475+
'created_at' => Carbon::now()->subMonths(12),
1476+
'updated_at' => Carbon::now()->subMonths(12),
1477+
'last_modified_at' => Carbon::now()->subMonths(12),
1478+
]));
1479+
$service3 = Service::factory()->create(array_merge($serviceParams, [
1480+
'score' => 3,
1481+
'created_at' => Carbon::now()->subMonths(6),
1482+
'updated_at' => Carbon::now()->subMonths(6),
1483+
'last_modified_at' => Carbon::now()->subMonths(6),
1484+
]));
1485+
$service2 = Service::factory()->create(array_merge($serviceParams, [
1486+
'score' => 2,
1487+
'created_at' => Carbon::now()->subMonths(3),
1488+
'updated_at' => Carbon::now()->subMonths(3),
1489+
'last_modified_at' => Carbon::now()->subMonths(3),
1490+
]));
1491+
$service1 = Service::factory()->create(array_merge($serviceParams, [
1492+
'score' => 1,
1493+
'created_at' => Carbon::now()->subMonths(1),
1494+
'updated_at' => Carbon::now()->subMonths(1),
1495+
'last_modified_at' => Carbon::now()->subMonths(1),
1496+
]));
1497+
$service0 = Service::factory()->create(array_merge($serviceParams, [
1498+
'score' => 0,
1499+
'created_at' => Carbon::now(),
1500+
'updated_at' => Carbon::now(),
1501+
'last_modified_at' => Carbon::now(),
1502+
]));
1503+
1504+
sleep(1);
1505+
1506+
$response = $this->json('POST', '/core/v1/search', [
1507+
'query' => 'royal organs hike',
1508+
]);
1509+
1510+
$response->assertStatus(Response::HTTP_OK);
1511+
$data = $this->getResponseContent($response)['data'];
1512+
1513+
$this->assertEquals($service5->id, $data[0]['id']);
1514+
$this->assertEquals($service4->id, $data[1]['id']);
1515+
$this->assertEquals($service3->id, $data[2]['id']);
1516+
$this->assertEquals($service2->id, $data[3]['id']);
1517+
$this->assertEquals($service1->id, $data[4]['id']);
1518+
$this->assertEquals($service0->id, $data[5]['id']);
1519+
}
14551520
}

0 commit comments

Comments
 (0)