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));
}
$result = $builder->search();
$response = $builder->search();
Discussion::setStateUser($actor);
@ -92,7 +92,7 @@ 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($response, 'hits.hits'))
->map(function ($hit) {
$id = Str::after($hit['_source']['id'], ':');
$type = $hit['_source']['type'];
@ -114,8 +114,8 @@ class SearchController extends ListDiscussionsController
$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'))) {
->each(function (Discussion $discussion) use ($results) {
if (in_array($discussion->id, $results->pluck('discussion_id')->toArray())) {
$discussion->most_relevant_post_id = $discussion->first_post_id;
} else {
$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)
{
$index = $container->make('blomstra.search.elastic_index');
/** @var array $seeders */
$seeders = $container->tagged('blomstra.search.seeders');
@ -33,42 +35,47 @@ class RebuildDocumentsCommand extends Command
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
$analyzer = $settings->get('blomstra-search.analyzer-language', 'english');
$properties = [
'properties' => [
'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer'],
'created_at' => ['type' => 'date'],
'updated_at' => ['type' => 'date'],
'is_private' => ['type' => 'boolean'],
'is_sticky' => ['type' => 'boolean'],
'groups' => ['type' => 'integer'],
'recipient_groups' => ['type' => 'integer'],
'recipient_users' => ['type' => 'integer'],
]
];
// Flush the index.
if ($this->option('flush')) {
$client->indices()->delete([
'index' => $container->make('blomstra.search.elastic_index'),
'index' => $index,
'ignore_unavailable' => true
]);
$client->indices()->create([
'index' => $container->make('blomstra.search.elastic_index'),
// 'settings' => [
// 'analysis' => [
// 'analyzer' => [
// 'default' => $analyzer,
// 'default_search' => $analyzer
// ]
// ]
// ]
'index' => $index,
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [
'flarum_analyzer' => [
'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english'
]
]
]
]
]
]);
}
if ($this->option('mapping')) {
$client->indices()->putMapping([
'index' => $container->make('blomstra.search.elastic_index'),
'body' => [
'properties' => [
'content' => ['type' => 'text'],
'created_at' => ['type' => 'date'],
'updated_at' => ['type' => 'date'],
'is_private' => ['type' => 'boolean'],
'is_sticky' => ['type' => 'boolean'],
'groups' => ['type' => 'integer'],
'recipient_groups' => ['type' => 'integer'],
'recipient_users' => ['type' => 'integer'],
]
]
'index' => $index,
'body' => $properties
]);
}