Skip to content

Commit

Permalink
Merge pull request #89 from Connected-Places/bug/sc-3659/improve-sutt…
Browse files Browse the repository at this point in the history
…on-search-results

Bug/sc 3659/improve sutton search results
  • Loading branch information
appsol committed Dec 11, 2023
2 parents a89688d + 829b1b1 commit de4a331
Show file tree
Hide file tree
Showing 16 changed files with 4,901 additions and 4,522 deletions.
9 changes: 7 additions & 2 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ public function hasAppend(string $name): bool
*
* @author
*/
public function onlyAlphaNumeric($string)
public function makeSearchable($string)
{
return preg_replace('/[^\w\d\s]+/i', '', $string);
$string = mb_strtolower($string);
preg_match_all('/(?:\b([A-Z,a-z]+)\b)/i', $string, $words);

return implode(' ', array_filter($words[1], function ($word) {
return mb_strlen($word) > 2;
}));
}
}
11 changes: 7 additions & 4 deletions app/Models/OrganisationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public function toSearchableArray(): array
{
$organisationEvent = [
'id' => $this->id,
'title' => $this->onlyAlphaNumeric($this->title),
'intro' => $this->onlyAlphaNumeric($this->intro),
'description' => $this->onlyAlphaNumeric($this->description),
'title' => $this->makeSearchable($this->title),
'intro' => $this->makeSearchable($this->intro),
'description' => $this->makeSearchable($this->description),
'start_date' => $this->start_date->setTimeFromTimeString($this->start_time)->toDateTimeLocalString(),
'end_date' => $this->end_date->setTimeFromTimeString($this->end_time)->toDateTimeLocalString(),
'is_free' => $this->is_free,
'is_virtual' => $this->is_virtual,
'organisation_name' => $this->onlyAlphaNumeric($this->organisation->name),
'organisation_name' => $this->makeSearchable($this->organisation->name),
'taxonomy_categories' => $this->taxonomies()->pluck('name')->toArray(),
'collection_categories' => $this->collections()->pluck('name')->toArray(),
'event_location' => null,
Expand Down Expand Up @@ -171,6 +171,9 @@ public function applyUpdateRequest(UpdateRequest $updateRequest): UpdateRequest
$this->syncTaxonomyRelationships($taxonomies);
}

// Update the search index
$this->save();

// Ensure conditional fields are reset if needed.
$this->resetConditionalFields();

Expand Down
34 changes: 11 additions & 23 deletions app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public function toSearchableArray(): array
foreach ($sectionContent['content'] as $i => $contentBlock) {
switch ($contentBlock['type']) {
case 'copy':
$content[] = $this->onlyAlphaNumeric($contentBlock['value']);
$content[] = $this->makeSearchable($contentBlock['value']);
break;
case 'cta':
$content[] = $this->onlyAlphaNumeric($contentBlock['title'] . ' ' . $contentBlock['description']);
$content[] = $this->makeSearchable($contentBlock['title'] . ' ' . $contentBlock['description']);
break;
default:
break;
Expand All @@ -92,7 +92,7 @@ public function toSearchableArray(): array
return [
'id' => $this->id,
'enabled' => $this->enabled,
'title' => $this->onlyAlphaNumeric($this->title),
'title' => $this->makeSearchable($this->title),
'content' => $contentSections,
'collection_categories' => $this->collections()->where('type', Collection::TYPE_CATEGORY)->pluck('name')->all(),
'collection_personas' => $this->collections()->where('type', Collection::TYPE_PERSONA)->pluck('name')->all(),
Expand Down Expand Up @@ -158,7 +158,7 @@ public function updateStatus($status): self
{
if ($this->parent && $this->parent->enabled === self::DISABLED) {
$this->enabled = self::DISABLED;
} elseif (!is_null($status)) {
} elseif (!is_null($status) && $status !== $this->enabled) {
$this->enabled = $status;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public function updateParent(?string $parentId = null): self
*/
public function updateOrder(?int $order): self
{
if (!is_null($order)) {
if (!is_null($order) && $order !== $this->order) {
$siblingAtIndex = $this->siblingAtIndex($order)->first();
$this->beforeOrAfterNode($siblingAtIndex, $siblingAtIndex->getLft() > $this->getLft());
}
Expand Down Expand Up @@ -284,7 +284,7 @@ public function applyUpdateRequest(UpdateRequest $updateRequest): UpdateRequest
$slug = $slugGenerator->generate($slug, 'pages');
}

// Update the organisation event record.
// Update the page record.
$this->update([
'title' => Arr::get($data, 'title', $this->title),
'slug' => $slug,
Expand All @@ -293,28 +293,16 @@ public function applyUpdateRequest(UpdateRequest $updateRequest): UpdateRequest
'page_type' => Arr::get($data, 'page_type', $this->page_type),
]);

if (Arr::has($data, 'parent_id')) {
$this->updateParent(Arr::get($data, 'parent_id'));
$this->updateStatus(Arr::get($data, 'enabled', $this->enabled));
}

if (Arr::has($data, 'enabled')) {
$this->updateStatus(Arr::get($data, 'enabled'));
}

if (Arr::has($data, 'order')) {
$this->updateOrder(Arr::get($data, 'order'));
}

if (Arr::has($data, 'image_file_id')) {
$this->updateImage(Arr::get($data, 'image_file_id'));
}
$this->updateParent(Arr::get($data, 'parent_id', $this->parent_uuid))
->updateStatus(Arr::get($data, 'enabled', $this->enabled))
->updateOrder(Arr::get($data, 'order', $this->order))
->updateImage(Arr::get($data, 'image_file_id', $this->image_file_id));

if (Arr::has($data, 'collections')) {
$this->updateCollections(Arr::get($data, 'collections'));
}

// Update model so far
// Update the search index
$this->save();

return $updateRequest;
Expand Down
11 changes: 7 additions & 4 deletions app/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public function toSearchableArray(): array

return [
'id' => $this->id,
'name' => $this->onlyAlphaNumeric($this->name),
'intro' => $this->onlyAlphaNumeric($this->intro),
'description' => $this->onlyAlphaNumeric($this->description),
'name' => $this->makeSearchable($this->name),
'intro' => $this->makeSearchable($this->intro),
'description' => $this->makeSearchable($this->description),
'wait_time' => $this->wait_time,
'is_free' => $this->is_free,
'status' => $this->status,
'score' => $this->score,
'organisation_name' => $this->onlyAlphaNumeric($this->organisation->name),
'organisation_name' => $this->makeSearchable($this->organisation->name),
'taxonomy_categories' => $this->taxonomies()->pluck('name')->toArray(),
'collection_categories' => static::collections($this)->where('type', Collection::TYPE_CATEGORY)->pluck('name')->toArray(),
'collection_personas' => static::collections($this)->where('type', Collection::TYPE_PERSONA)->pluck('name')->toArray(),
Expand Down Expand Up @@ -312,6 +312,9 @@ public function applyUpdateRequest(UpdateRequest $updateRequest): UpdateRequest
}
}

// Update the search index
$this->save();

// Ensure conditional fields are reset if needed.
$this->resetConditionalFields();

Expand Down
26 changes: 13 additions & 13 deletions app/Search/ElasticSearch/EventQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare (strict_types=1);

namespace App\Search\ElasticSearch;

Expand Down Expand Up @@ -110,21 +110,21 @@ public function build(SearchCriteriaQuery $query, int $page = null, int $perPage

protected function applyQuery(string $query): void
{
$this->addMatch('title', $query, $this->shouldPath, 1.5);
$this->addMatch('title', $query, $this->shouldPath, 2.5, 'AUTO', 'AND');
$this->addMatchPhrase('title', $query, $this->shouldPath, 4);
$this->addMatch('organisation_name', $query, $this->shouldPath);
$this->addMatch('organisation_name', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('organisation_name', $query, $this->shouldPath, 3);
$this->addMatch('title', $query, $this->shouldPath, 3);
$this->addMatch('title', $query, $this->shouldPath, 4, 'AUTO', 'AND');
$this->addMatchPhrase('title', $query, $this->shouldPath, 6);
$this->addMatch('organisation_name', $query, $this->shouldPath, 3);
$this->addMatch('organisation_name', $query, $this->shouldPath, 4, 'AUTO', 'AND');
$this->addMatchPhrase('organisation_name', $query, $this->shouldPath, 6);
$this->addMatch('intro', $query, $this->shouldPath);
$this->addMatch('intro', $query, $this->shouldPath, 1.5, 'AUTO', 'AND');
$this->addMatchPhrase('intro', $query, $this->shouldPath, 2.5);
$this->addMatch('description', $query, $this->shouldPath, 0.5);
$this->addMatch('intro', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('intro', $query, $this->shouldPath, 3);
$this->addMatch('description', $query, $this->shouldPath);
$this->addMatch('description', $query, $this->shouldPath, 1.5, 'AUTO', 'AND');
$this->addMatchPhrase('description', $query, $this->shouldPath, 2);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath, 0.5);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath, 1, 'AUTO', 'AND');
$this->addMatchPhrase('taxonomy_categories', $query, $this->shouldPath, 1.5);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('taxonomy_categories', $query, $this->shouldPath, 3);

$this->addMinimumShouldMatch();
}
Expand Down
26 changes: 7 additions & 19 deletions app/Search/ElasticSearch/PageQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare (strict_types=1);

namespace App\Search\ElasticSearch;

Expand Down Expand Up @@ -50,24 +50,12 @@ protected function applyStatus(bool $enabled): void

protected function applyQuery(string $query): void
{
// $this->addMatch('title', $query, $this->shouldPath, 3);
// $this->addMatch('content.introduction.title', $query, $this->shouldPath, 2);
// $this->addMatch('content.introduction.content', $query, $this->shouldPath);
// $this->addMatch('content.about.title', $query, $this->shouldPath, 2);
// $this->addMatch('content.about.content', $query, $this->shouldPath);
// $this->addMatch('content.info_pages.title', $query, $this->shouldPath, 2);
// $this->addMatch('content.info_pages.content', $query, $this->shouldPath);
// $this->addMatch('content.collections.title', $query, $this->shouldPath, 2);
// $this->addMatch('content.collections.content', $query, $this->shouldPath);
// $this->addMatch('collection_categories', $query, $this->shouldPath);
// $this->addMatch('collection_personas', $query, $this->shouldPath);

$this->addMatch('title', $query, $this->shouldPath, 2);
$this->addMatch('title', $query, $this->shouldPath, 3, 'AUTO', 'AND');
$this->addMatchPhrase('title', $query, $this->shouldPath, 5);
$this->addMatch('content.introduction.title', $query, $this->shouldPath, 1.5);
$this->addMatch('content.introduction.title', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('content.introduction.title', $query, $this->shouldPath, 2.5);
$this->addMatch('title', $query, $this->shouldPath, 3);
$this->addMatch('title', $query, $this->shouldPath, 4, 'AUTO', 'AND');
$this->addMatchPhrase('title', $query, $this->shouldPath, 6);
$this->addMatch('content.introduction.title', $query, $this->shouldPath, 2);
$this->addMatch('content.introduction.title', $query, $this->shouldPath, 3, 'AUTO', 'AND');
$this->addMatchPhrase('content.introduction.title', $query, $this->shouldPath, 4);
$this->addMatch('content.introduction.content', $query, $this->shouldPath);
$this->addMatch('content.introduction.content', $query, $this->shouldPath, 1.5, 'AUTO', 'AND');
$this->addMatchPhrase('content.introduction.content', $query, $this->shouldPath, 2);
Expand Down
2 changes: 1 addition & 1 deletion app/Search/ElasticSearch/ServiceEloquentMapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare (strict_types=1);

namespace App\Search\ElasticSearch;

Expand Down
28 changes: 14 additions & 14 deletions app/Search/ElasticSearch/ServiceQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function __construct()
],
'functions' => [
[
'field_value_factor' => [
'field' => 'score',
'missing' => 1,
'modifier' => 'ln1p',
'script_score' => [
'script' => [
'source' => "((doc['score'].value + 1) * 0.1) + 1",
],
],
],
],
Expand Down Expand Up @@ -95,21 +95,21 @@ protected function applyStatus(string $status): void

protected function applyQuery(string $query): void
{
$this->addMatch('name', $query, $this->shouldPath, 2);
$this->addMatch('name', $query, $this->shouldPath, 2.5, 'AUTO', 'AND');
$this->addMatchPhrase('name', $query, $this->shouldPath, 3);
$this->addMatch('organisation_name', $query, $this->shouldPath, 2);
$this->addMatch('organisation_name', $query, $this->shouldPath, 2.5, 'AUTO', 'AND');
$this->addMatchPhrase('organisation_name', $query, $this->shouldPath, 3);
$this->addMatch('name', $query, $this->shouldPath, 3);
$this->addMatch('name', $query, $this->shouldPath, 4, 'AUTO', 'AND');
$this->addMatchPhrase('name', $query, $this->shouldPath, 6);
$this->addMatch('organisation_name', $query, $this->shouldPath, 3);
$this->addMatch('organisation_name', $query, $this->shouldPath, 4, 'AUTO', 'AND');
$this->addMatchPhrase('organisation_name', $query, $this->shouldPath, 6);
$this->addMatch('intro', $query, $this->shouldPath);
$this->addMatch('intro', $query, $this->shouldPath, 1.5, 'AUTO', 'AND');
$this->addMatchPhrase('intro', $query, $this->shouldPath, 2);
$this->addMatch('intro', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('intro', $query, $this->shouldPath, 3);
$this->addMatch('description', $query, $this->shouldPath);
$this->addMatch('description', $query, $this->shouldPath, 1.5, 'AUTO', 'AND');
$this->addMatchPhrase('description', $query, $this->shouldPath, 2);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath, 1, 'AUTO', 'AND');
$this->addMatchPhrase('taxonomy_categories', $query, $this->shouldPath, 1.5);
$this->addMatch('taxonomy_categories', $query, $this->shouldPath, 2, 'AUTO', 'AND');
$this->addMatchPhrase('taxonomy_categories', $query, $this->shouldPath, 3);

$this->addMinimumShouldMatch();
}
Expand Down
4 changes: 2 additions & 2 deletions app/Search/SearchCriteriaQuery.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare (strict_types=1);

namespace App\Search;

Expand Down Expand Up @@ -100,7 +100,7 @@ public function getQuery(): ?string

public function setQuery(?string $query): void
{
$this->query = $query;
$this->query = mb_strtolower($query);
}

public function hasType(): bool
Expand Down
6 changes: 4 additions & 2 deletions database/factories/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function definition(): array
'name' => $name,
'type' => Service::TYPE_SERVICE,
'status' => Service::STATUS_ACTIVE,
'intro' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
'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.',
'intro' => mb_substr($this->faker->paragraph(2), 0, 149),
'description' => $this->faker->paragraph(5),
// 'intro' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
// '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.',
'is_free' => true,
'url' => $this->faker->url(),
'contact_name' => $this->faker->name(),
Expand Down
9 changes: 8 additions & 1 deletion docker/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Set base image.
FROM node:16.13
FROM node:16.20

# Set maintainer to Ayup Digital.
LABEL maintainer="Ayup Digital"

# Install git for faster package downloads.
RUN apt-get install -y git

# Create new cache directory for npm
# See https://stackoverflow.com/questions/77626859/npm-cannot-run-dev-in-docker-node-container-due-to-root-owned-files
RUN mkdir -p /home/node/app/.npm \
&& chown -R node:node /home/node/app/.npm

ENV npm_config_cache /home/node/app/.npm

# Set the working directory to the project root.
WORKDIR /var/www/html
Loading

0 comments on commit de4a331

Please sign in to comment.