wip
This commit is contained in:
parent
6c045ab595
commit
61e2f90835
|
|
@ -12,6 +12,8 @@ return [
|
|||
(new Flarum\Frontend('admin'))
|
||||
->js(__DIR__ . '/js/dist/admin.js'),
|
||||
|
||||
(new Flarum\Locales(__DIR__ . '/resources/locale')),
|
||||
|
||||
(new Flarum\Routes('api'))
|
||||
->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class),
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -14,26 +14,22 @@ app.initializers.add('blomstra-search', () => {
|
|||
.for('blomstra-search')
|
||||
.registerSetting({
|
||||
setting: 'blomstra-search.elastic-endpoint',
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-endpoint.label'),
|
||||
help: app.translator.trans('blomstra-search.admin.elastic-endpoint.help'),
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-endpoint'),
|
||||
type: 'input'
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'blomstra-search.elastic-username',
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-username.label'),
|
||||
help: app.translator.trans('blomstra-search.admin.elastic-username.help'),
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-username'),
|
||||
type: 'input'
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'blomstra-search.elastic-password',
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-password.label'),
|
||||
help: app.translator.trans('blomstra-search.admin.elastic-password.help'),
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-password'),
|
||||
type: 'password'
|
||||
})
|
||||
.registerSetting({
|
||||
setting: 'blomstra-search.elastic-index',
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-index.label'),
|
||||
help: app.translator.trans('blomstra-search.admin.elastic-index.help'),
|
||||
label: app.translator.trans('blomstra-search.admin.elastic-index'),
|
||||
default: 'flarum',
|
||||
type: 'input'
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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'))
|
||||
->map(function ($hit) {
|
||||
$id = Str::after($hit['_source']['id'], ':');
|
||||
$type = $hit['_source']['type'];
|
||||
|
||||
if ($type === 'posts') {
|
||||
/** @var Discussion $discussion */
|
||||
$discussion = Discussion::whereHas('posts', function ($query) use ($id) {
|
||||
$query->where('id', $id);
|
||||
})->first();
|
||||
|
||||
$discussion->most_relevant_post_id = $id;
|
||||
return [
|
||||
'most_relevant_post_id' => $id,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'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;
|
||||
} 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')
|
||||
->unique();
|
||||
|
||||
$this->loadRelations($results, $include);
|
||||
$this->loadRelations($discussions, $include);
|
||||
|
||||
if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) {
|
||||
foreach ($results as $discussion) {
|
||||
foreach ($discussions as $discussion) {
|
||||
foreach ($relations as $relation) {
|
||||
if ($discussion->$relation) {
|
||||
$discussion->$relation->discussion = $discussion;
|
||||
|
|
@ -127,7 +137,7 @@ class SearchController extends ListDiscussionsController
|
|||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
return $discussions;
|
||||
}
|
||||
|
||||
protected function getDocument(string $type): ?ElasticDocument
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace Blomstra\Search\Commands;
|
|||
use Blomstra\Search\Observe\SavingJob;
|
||||
use Blomstra\Search\Seeders\Seeder;
|
||||
use Elasticsearch\Client;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Queue\Queue;
|
||||
|
|
@ -29,6 +30,11 @@ class RebuildDocumentsCommand extends Command
|
|||
/** @var Client $client */
|
||||
$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.
|
||||
if ($this->option('flush')) {
|
||||
$client->indices()->delete([
|
||||
|
|
@ -36,7 +42,15 @@ class RebuildDocumentsCommand extends Command
|
|||
'ignore_unavailable' => true
|
||||
]);
|
||||
$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
|
||||
// ]
|
||||
// ]
|
||||
// ]
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue