Apply fixes from StyleCI

This commit is contained in:
StyleCI Bot 2023-10-18 12:26:10 +00:00
parent 54148ab1ad
commit 69003373a9
No known key found for this signature in database
GPG Key ID: E4A5316DFBB23575
14 changed files with 148 additions and 38 deletions

View File

@ -22,7 +22,7 @@ return [
(new Flarum\Frontend('admin')) (new Flarum\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'), ->js(__DIR__.'/js/dist/admin.js'),
(new Flarum\Locales(__DIR__.'/resources/locale')), new Flarum\Locales(__DIR__.'/resources/locale'),
(new Flarum\ServiceProvider()) (new Flarum\ServiceProvider())
->register(Provider::class), ->register(Provider::class),

View File

@ -12,6 +12,7 @@
namespace Blomstra\Search\Api\Controllers; namespace Blomstra\Search\Api\Controllers;
use Blomstra\Search\Elasticsearch\Builder;
use Blomstra\Search\Elasticsearch\MatchPhraseQuery; use Blomstra\Search\Elasticsearch\MatchPhraseQuery;
use Blomstra\Search\Elasticsearch\MatchQuery; use Blomstra\Search\Elasticsearch\MatchQuery;
use Blomstra\Search\Elasticsearch\TermsQuery; use Blomstra\Search\Elasticsearch\TermsQuery;
@ -32,7 +33,6 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Blomstra\Search\Elasticsearch\Builder;
use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery;
use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\Query;
use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery; use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery;

View File

@ -13,6 +13,7 @@
namespace Blomstra\Search\Commands; namespace Blomstra\Search\Commands;
use Blomstra\Search\Discussion\DiscussionIndexer; use Blomstra\Search\Discussion\DiscussionIndexer;
use Blomstra\Search\Elasticsearch\Builder;
use Blomstra\Search\Post\CommentPostIndexer; use Blomstra\Search\Post\CommentPostIndexer;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Discussion\Discussion; use Flarum\Discussion\Discussion;
@ -24,7 +25,6 @@ use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Queue; use Illuminate\Contracts\Queue\Queue;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Blomstra\Search\Elasticsearch\Builder;
use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery;
use Spatie\ElasticsearchQueryBuilder\Queries\RangeQuery; use Spatie\ElasticsearchQueryBuilder\Queries\RangeQuery;
@ -45,11 +45,11 @@ class BuildCommand extends Command
{ {
$indexers = [ $indexers = [
'discussions' => DiscussionIndexer::class, 'discussions' => DiscussionIndexer::class,
'posts' => CommentPostIndexer::class, 'posts' => CommentPostIndexer::class,
]; ];
$models = [ $models = [
'discussions' => Discussion::class, 'discussions' => Discussion::class,
'posts' => Post::class, 'posts' => Post::class,
]; ];
$only = $this->option('only'); $only = $this->option('only');
@ -72,7 +72,7 @@ class BuildCommand extends Command
} }
// Create the index. // Create the index.
if (! $this->option('recreate') && $this->option('mapping')) { if (!$this->option('recreate') && $this->option('mapping')) {
$client->indices()->putMapping([ $client->indices()->putMapping([
'index' => $indexer::index(), 'index' => $indexer::index(),
'body' => $properties, 'body' => $properties,

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Discussion; namespace Blomstra\Search\Discussion;
use Blomstra\Search\Save\Document; use Blomstra\Search\Save\Document;
@ -34,12 +44,12 @@ class DiscussionIndexer implements IndexerInterface
$this->elastic->delete(self::index(), $models); $this->elastic->delete(self::index(), $models);
} }
function build(): void public function build(): void
{ {
$this->elastic->build(self::index(), $this->properties()); $this->elastic->build(self::index(), $this->properties());
} }
function flush(): void public function flush(): void
{ {
$this->elastic->flush(self::index()); $this->elastic->flush(self::index());
} }

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Discussion; namespace Blomstra\Search\Discussion;
use Blomstra\Search\Post\CommentPostIndexer; use Blomstra\Search\Post\CommentPostIndexer;
@ -12,10 +22,10 @@ class DiscussionSearcher extends Searcher
{ {
public function index(): string public function index(): string
{ {
return DiscussionIndexer::index() . ',' . CommentPostIndexer::index(); return DiscussionIndexer::index().','.CommentPostIndexer::index();
} }
function getQuery(User $actor): Builder public function getQuery(User $actor): Builder
{ {
return Discussion::whereVisibleTo($actor)->select('discussions.*'); return Discussion::whereVisibleTo($actor)->select('discussions.*');
} }

View File

@ -1,7 +1,18 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Discussion; namespace Blomstra\Search\Discussion;
use Blomstra\Search\Elasticsearch\Builder;
use Blomstra\Search\Elasticsearch\MatchPhraseQuery; use Blomstra\Search\Elasticsearch\MatchPhraseQuery;
use Blomstra\Search\Elasticsearch\MatchQuery; use Blomstra\Search\Elasticsearch\MatchQuery;
use Blomstra\Search\Elasticsearch\TermsQuery; use Blomstra\Search\Elasticsearch\TermsQuery;
@ -19,7 +30,6 @@ use Illuminate\Support\Collection;
use Spatie\ElasticsearchQueryBuilder\Aggregations\FilterAggregation; use Spatie\ElasticsearchQueryBuilder\Aggregations\FilterAggregation;
use Spatie\ElasticsearchQueryBuilder\Aggregations\TermsAggregation; use Spatie\ElasticsearchQueryBuilder\Aggregations\TermsAggregation;
use Spatie\ElasticsearchQueryBuilder\Aggregations\TopHitsAggregation; use Spatie\ElasticsearchQueryBuilder\Aggregations\TopHitsAggregation;
use Blomstra\Search\Elasticsearch\Builder;
use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery;
use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\Query;
use Spatie\ElasticsearchQueryBuilder\Sorts\Sort; use Spatie\ElasticsearchQueryBuilder\Sorts\Sort;
@ -81,17 +91,17 @@ class FulltextFilter extends AbstractFulltextFilter
$state->retrieveDatabaseRecordsUsing(function (array $response, SearchCriteria $criteria): Collection { $state->retrieveDatabaseRecordsUsing(function (array $response, SearchCriteria $criteria): Collection {
$buckets = Collection::make(Arr::get($response, 'aggregations.posts.per_discussion.buckets')) $buckets = Collection::make(Arr::get($response, 'aggregations.posts.per_discussion.buckets'))
->map(fn (array $hit) => [ ->map(fn (array $hit) => [
'discussion_id' => $hit['key'], 'discussion_id' => $hit['key'],
'most_relevant_post_id' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._id'), 'most_relevant_post_id' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._id'),
'most_relevant_post_score' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._score'), 'most_relevant_post_score' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._score'),
])->keyBy('discussion_id'); ])->keyBy('discussion_id');
$results = Collection::make(Arr::get($response, 'hits.hits')) $results = Collection::make(Arr::get($response, 'hits.hits'))
->map(fn (array $hit) => [ ->map(fn (array $hit) => [
'discussion_id' => $hit['_source']['discussion_id'], 'discussion_id' => $hit['_source']['discussion_id'],
'most_relevant_post_id' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_id'), 'most_relevant_post_id' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_id'),
'most_relevant_post_score' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_score'), 'most_relevant_post_score' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_score'),
'title_score' => $hit['_score'], 'title_score' => $hit['_score'],
])->keyBy('discussion_id'); ])->keyBy('discussion_id');
// We have $hits and $buckets, both are sorted by score. // We have $hits and $buckets, both are sorted by score.
@ -122,17 +132,17 @@ class FulltextFilter extends AbstractFulltextFilter
->select('discussions.*') ->select('discussions.*')
->selectRaw( ->selectRaw(
$connection->raw('COALESCE(('.$connection->getQueryGrammar()->compileSelect( $connection->raw('COALESCE(('.$connection->getQueryGrammar()->compileSelect(
$postsQuery = Post::query() $postsQuery = Post::query()
->select('posts.id') ->select('posts.id')
->whereIn('posts.id', $results->pluck('most_relevant_post_id')->filter()) ->whereIn('posts.id', $results->pluck('most_relevant_post_id')->filter())
->whereColumn('discussions.id', '=', 'posts.discussion_id') ->whereColumn('discussions.id', '=', 'posts.discussion_id')
->limit(1) ->limit(1)
->toBase() ->toBase()
).'), first_post_id) as most_relevant_post_id')->getValue($connection->getQueryGrammar()) ).'), first_post_id) as most_relevant_post_id')->getValue($connection->getQueryGrammar())
) )
->mergeBindings($postsQuery) ->mergeBindings($postsQuery)
->whereIn('discussions.id', $results->pluck('discussion_id')) ->whereIn('discussions.id', $results->pluck('discussion_id'))
->orderByRaw('FIELD(`discussions`.`id`, ' . implode(',', $results->pluck('discussion_id')->all()) . ')') ->orderByRaw('FIELD(`discussions`.`id`, '.implode(',', $results->pluck('discussion_id')->all()).')')
->get(); ->get();
}); });
} }

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Discussion; namespace Blomstra\Search\Discussion;
use Blomstra\Search\Elasticsearch\TermsQuery; use Blomstra\Search\Elasticsearch\TermsQuery;
@ -33,7 +43,7 @@ class PrivateFilterMutator implements FilterInterface
{ {
$actor = $state->getActor(); $actor = $state->getActor();
if (! $this->extensions->isEnabled('fof-byobu') || $actor->isGuest()) { if (!$this->extensions->isEnabled('fof-byobu') || $actor->isGuest()) {
return; return;
} }

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
class Builder extends \Spatie\ElasticsearchQueryBuilder\Builder class Builder extends \Spatie\ElasticsearchQueryBuilder\Builder

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Post; namespace Blomstra\Search\Post;
use Blomstra\Search\Save\Document; use Blomstra\Search\Save\Document;
@ -25,7 +35,7 @@ class CommentPostIndexer implements IndexerInterface
return 'posts'; return 'posts';
} }
function save(array $models): void public function save(array $models): void
{ {
$this->elastic->save( $this->elastic->save(
self::index(), self::index(),
@ -34,7 +44,7 @@ class CommentPostIndexer implements IndexerInterface
); );
} }
function delete(array $models): void public function delete(array $models): void
{ {
$this->elastic->delete( $this->elastic->delete(
self::index(), self::index(),

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Search\Concerns; namespace Blomstra\Search\Search\Concerns;
use Flarum\Discussion\Discussion; use Flarum\Discussion\Discussion;

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Search; namespace Blomstra\Search\Search;
use Blomstra\Search\Exceptions\IndexingException; use Blomstra\Search\Exceptions\IndexingException;
@ -29,8 +39,8 @@ class ElasticIndex
[ [
'index' => [ 'index' => [
'_index' => $index, '_index' => $index,
'_id' => $document->id '_id' => $document->id,
] ],
], ],
$document->toArray(), $document->toArray(),
]; ];
@ -55,8 +65,8 @@ class ElasticIndex
[ [
'delete' => [ 'delete' => [
'_index' => $index(), '_index' => $index(),
'_id' => $model->id '_id' => $model->id,
] ],
], ],
]; ];
})->flatten(1); })->flatten(1);

View File

@ -1,5 +1,15 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Search; namespace Blomstra\Search\Search;
use Flarum\Search\AbstractDriver; use Flarum\Search\AbstractDriver;

View File

@ -1,10 +1,20 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Search; namespace Blomstra\Search\Search;
use Blomstra\Search\Elasticsearch\Builder;
use Closure; use Closure;
use Flarum\Search\SearchState; use Flarum\Search\SearchState;
use Blomstra\Search\Elasticsearch\Builder;
class ElasticSearchState extends SearchState class ElasticSearchState extends SearchState
{ {

View File

@ -1,7 +1,18 @@
<?php <?php
/*
* This file is part of blomstra/search.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Search; namespace Blomstra\Search\Search;
use Blomstra\Search\Elasticsearch\Builder;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Search\Filter\FilterManager; use Flarum\Search\Filter\FilterManager;
use Flarum\Search\SearchCriteria; use Flarum\Search\SearchCriteria;
@ -10,7 +21,6 @@ use Flarum\Search\SearchResults;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Blomstra\Search\Elasticsearch\Builder;
use Spatie\ElasticsearchQueryBuilder\Sorts\Sort; use Spatie\ElasticsearchQueryBuilder\Sorts\Sort;
abstract class Searcher implements SearcherInterface abstract class Searcher implements SearcherInterface
@ -43,8 +53,8 @@ abstract class Searcher implements SearcherInterface
$id = Str::after($hit['_source']['id'], "$type:"); $id = Str::after($hit['_source']['id'], "$type:");
return [ return [
'id' => $id, 'id' => $id,
'score' => Arr::get($hit, '_score'), 'score' => Arr::get($hit, '_score'),
'weight' => Arr::get($hit, 'sort.0'), 'weight' => Arr::get($hit, 'sort.0'),
]; ];
})->sortByDesc('weight'); })->sortByDesc('weight');
@ -56,7 +66,7 @@ abstract class Searcher implements SearcherInterface
return $this->getQuery($criteria->actor) return $this->getQuery($criteria->actor)
->whereIn('id', $ids) ->whereIn('id', $ids)
->orderByRaw('FIELD(id, ' . implode(',', $ids) . ')') ->orderByRaw('FIELD(id, '.implode(',', $ids).')')
->get(); ->get();
}); });
@ -81,7 +91,7 @@ abstract class Searcher implements SearcherInterface
$callback = $state->getRetrieveDatabaseRecordsUsing(); $callback = $state->getRetrieveDatabaseRecordsUsing();
if (! $callback) { if (!$callback) {
throw new \RuntimeException('No callback set to retrieve database records'); throw new \RuntimeException('No callback set to retrieve database records');
} }
@ -94,7 +104,7 @@ abstract class Searcher implements SearcherInterface
{ {
$sort = $criteria->sort; $sort = $criteria->sort;
if ($criteria->sortIsDefault && ! empty($state->getDefaultSort())) { if ($criteria->sortIsDefault && !empty($state->getDefaultSort())) {
$sort = $state->getDefaultSort(); $sort = $state->getDefaultSort();
} }