indexing now works using language processing

This commit is contained in:
Daniel Klabbers 2021-11-02 12:07:27 +01:00
parent da04d0abfd
commit 254c58101b
4 changed files with 37 additions and 30 deletions

2
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

@ -75,7 +75,7 @@ class SearchController extends ListDiscussionsController
$builder->addSort(new Sort($field, $direction)); $builder->addSort(new Sort($field, $direction));
} }
$result = $builder->search(); $response = $builder->search();
Discussion::setStateUser($actor); Discussion::setStateUser($actor);
@ -92,7 +92,7 @@ class SearchController extends ListDiscussionsController
// we need to retrieve all discussion ids and when the results are posts, // we need to retrieve all discussion ids and when the results are posts,
// their ids as most relevant post id // their ids as most relevant post id
$results = Collection::make(Arr::get($result, 'hits.hits')) $results = Collection::make(Arr::get($response, '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'];
@ -114,8 +114,8 @@ class SearchController extends ListDiscussionsController
$query->whereIn('id', $results->pluck('most_relevant_post_id')); $query->whereIn('id', $results->pluck('most_relevant_post_id'));
}) })
->get() ->get()
->map(function (Discussion $discussion) use ($results) { ->each(function (Discussion $discussion) use ($results) {
if (in_array($discussion->id, $results->pluck('discussion_id'))) { if (in_array($discussion->id, $results->pluck('discussion_id')->toArray())) {
$discussion->most_relevant_post_id = $discussion->first_post_id; $discussion->most_relevant_post_id = $discussion->first_post_id;
} else { } else {
$post = $discussion->posts()->whereIn('id', $results->pluck('most_relevant_post_id'))->first(); $post = $discussion->posts()->whereIn('id', $results->pluck('most_relevant_post_id'))->first();

View File

@ -21,6 +21,8 @@ class RebuildDocumentsCommand extends Command
public function handle(Container $container) public function handle(Container $container)
{ {
$index = $container->make('blomstra.search.elastic_index');
/** @var array $seeders */ /** @var array $seeders */
$seeders = $container->tagged('blomstra.search.seeders'); $seeders = $container->tagged('blomstra.search.seeders');
@ -33,33 +35,9 @@ class RebuildDocumentsCommand extends Command
/** @var SettingsRepositoryInterface $settings */ /** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class); $settings = $container->make(SettingsRepositoryInterface::class);
$analyzer = $settings->get('blomstra-search.analyzer-language', 'english'); $properties = [
// Flush the index.
if ($this->option('flush')) {
$client->indices()->delete([
'index' => $container->make('blomstra.search.elastic_index'),
'ignore_unavailable' => true
]);
$client->indices()->create([
'index' => $container->make('blomstra.search.elastic_index'),
// 'settings' => [
// 'analysis' => [
// 'analyzer' => [
// 'default' => $analyzer,
// 'default_search' => $analyzer
// ]
// ]
// ]
]);
}
if ($this->option('mapping')) {
$client->indices()->putMapping([
'index' => $container->make('blomstra.search.elastic_index'),
'body' => [
'properties' => [ 'properties' => [
'content' => ['type' => 'text'], 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer'],
'created_at' => ['type' => 'date'], 'created_at' => ['type' => 'date'],
'updated_at' => ['type' => 'date'], 'updated_at' => ['type' => 'date'],
'is_private' => ['type' => 'boolean'], 'is_private' => ['type' => 'boolean'],
@ -68,7 +46,36 @@ class RebuildDocumentsCommand extends Command
'recipient_groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'],
'recipient_users' => ['type' => 'integer'], 'recipient_users' => ['type' => 'integer'],
] ]
];
// Flush the index.
if ($this->option('flush')) {
$client->indices()->delete([
'index' => $index,
'ignore_unavailable' => true
]);
$client->indices()->create([
'index' => $index,
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [
'flarum_analyzer' => [
'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english'
] ]
]
]
]
]
]);
}
if ($this->option('mapping')) {
$client->indices()->putMapping([
'index' => $index,
'body' => $properties
]); ]);
} }