This commit is contained in:
Daniël Klabbers 2021-10-31 02:53:16 +02:00
parent 6c045ab595
commit 61e2f90835
7 changed files with 58 additions and 1280 deletions

View File

@ -12,6 +12,8 @@ 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\Routes('api')) (new Flarum\Routes('api'))
->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class), ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class),

1257
js/dist/forum.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -14,26 +14,22 @@ app.initializers.add('blomstra-search', () => {
.for('blomstra-search') .for('blomstra-search')
.registerSetting({ .registerSetting({
setting: 'blomstra-search.elastic-endpoint', setting: 'blomstra-search.elastic-endpoint',
label: app.translator.trans('blomstra-search.admin.elastic-endpoint.label'), label: app.translator.trans('blomstra-search.admin.elastic-endpoint'),
help: app.translator.trans('blomstra-search.admin.elastic-endpoint.help'),
type: 'input' type: 'input'
}) })
.registerSetting({ .registerSetting({
setting: 'blomstra-search.elastic-username', setting: 'blomstra-search.elastic-username',
label: app.translator.trans('blomstra-search.admin.elastic-username.label'), label: app.translator.trans('blomstra-search.admin.elastic-username'),
help: app.translator.trans('blomstra-search.admin.elastic-username.help'),
type: 'input' type: 'input'
}) })
.registerSetting({ .registerSetting({
setting: 'blomstra-search.elastic-password', setting: 'blomstra-search.elastic-password',
label: app.translator.trans('blomstra-search.admin.elastic-password.label'), label: app.translator.trans('blomstra-search.admin.elastic-password'),
help: app.translator.trans('blomstra-search.admin.elastic-password.help'),
type: 'password' type: 'password'
}) })
.registerSetting({ .registerSetting({
setting: 'blomstra-search.elastic-index', setting: 'blomstra-search.elastic-index',
label: app.translator.trans('blomstra-search.admin.elastic-index.label'), label: app.translator.trans('blomstra-search.admin.elastic-index'),
help: app.translator.trans('blomstra-search.admin.elastic-index.help'),
default: 'flarum', default: 'flarum',
type: 'input' type: 'input'
}) })

11
resources/locale/en.yml Normal file
View File

@ -0,0 +1,11 @@
blomstra-search:
admin:
elastic-endpoint: Elastic Endpoint
elastic-username: => core.ref.username
elastic-password: => core.ref.password
elastic-index: Index
analyzer:
label: Analyzer language
help: |
The analyzer makes search understand stop words and undertakes language
specific improvements for indexing.

View File

@ -90,35 +90,45 @@ class SearchController extends ListDiscussionsController
} }
} }
// we need to retrieve all discussion ids and when the results are posts,
// their ids as most relevant post id
$results = Collection::make(Arr::get($result, 'hits.hits')) $results = Collection::make(Arr::get($result, 'hits.hits'))
->map(function ($hit) { ->map(function ($hit) {
$id = Str::after($hit['_source']['id'], ':'); $id = Str::after($hit['_source']['id'], ':');
$type = $hit['_source']['type']; $type = $hit['_source']['type'];
if ($type === 'posts') { if ($type === 'posts') {
/** @var Discussion $discussion */ return [
$discussion = Discussion::whereHas('posts', function ($query) use ($id) { 'most_relevant_post_id' => $id,
$query->where('id', $id); ];
})->first(); } else {
return [
$discussion->most_relevant_post_id = $id; 'discussion_id' => $id
];
} }
if ($type === 'discussions') { });
/** @var Discussion $discussion */
$discussion = Discussion::find($id);
$discussions = Discussion::query()
->whereIn('id', $results->pluck('discussion_id'))
->orWhereHas('posts', function ($query) use ($results) {
$query->whereIn('id', $results->pluck('most_relevant_post_id'));
})
->get()
->map(function (Discussion $discussion) use ($results) {
if (in_array($discussion->id, $results->pluck('discussion_id'))) {
$discussion->most_relevant_post_id = $discussion->first_post_id; $discussion->most_relevant_post_id = $discussion->first_post_id;
} else {
$post = $discussion->posts()->whereIn('id', $results->pluck('most_relevant_post_id'))->first();
$discussion->most_relevant_post_id = $post?->id ?? $discussion->first_post_id;
} }
return $discussion;
}) })
->keyBy('id') ->keyBy('id')
->unique(); ->unique();
$this->loadRelations($results, $include); $this->loadRelations($discussions, $include);
if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) { if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) {
foreach ($results as $discussion) { foreach ($discussions as $discussion) {
foreach ($relations as $relation) { foreach ($relations as $relation) {
if ($discussion->$relation) { if ($discussion->$relation) {
$discussion->$relation->discussion = $discussion; $discussion->$relation->discussion = $discussion;
@ -127,7 +137,7 @@ class SearchController extends ListDiscussionsController
} }
} }
return $results; return $discussions;
} }
protected function getDocument(string $type): ?ElasticDocument protected function getDocument(string $type): ?ElasticDocument

View File

@ -5,6 +5,7 @@ namespace Blomstra\Search\Commands;
use Blomstra\Search\Observe\SavingJob; use Blomstra\Search\Observe\SavingJob;
use Blomstra\Search\Seeders\Seeder; use Blomstra\Search\Seeders\Seeder;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Queue; use Illuminate\Contracts\Queue\Queue;
@ -29,6 +30,11 @@ class RebuildDocumentsCommand extends Command
/** @var Client $client */ /** @var Client $client */
$client = $container->make('blomstra.search.elastic'); $client = $container->make('blomstra.search.elastic');
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
$analyzer = $settings->get('blomstra-search.analyzer-language', 'english');
// Flush the index. // Flush the index.
if ($this->option('flush')) { if ($this->option('flush')) {
$client->indices()->delete([ $client->indices()->delete([
@ -36,7 +42,15 @@ class RebuildDocumentsCommand extends Command
'ignore_unavailable' => true 'ignore_unavailable' => true
]); ]);
$client->indices()->create([ $client->indices()->create([
'index' => $container->make('blomstra.search.elastic_index') 'index' => $container->make('blomstra.search.elastic_index'),
// 'settings' => [
// 'analysis' => [
// 'analyzer' => [
// 'default' => $analyzer,
// 'default_search' => $analyzer
// ]
// ]
// ]
]); ]);
} }