diff --git a/app/app/Services/Bot/KnowledgeSearch.php b/app/app/Services/Bot/KnowledgeSearch.php new file mode 100644 index 00000000..a26e5be4 --- /dev/null +++ b/app/app/Services/Bot/KnowledgeSearch.php @@ -0,0 +1,40 @@ + + */ +class KnowledgeSearch +{ + public function search(string $question, int $limit = 3): array + { + $question = trim($question); + if ($question === '') { + return []; + } + + /** @var Collection $hits */ + $hits = KnowledgeChunk::query() + ->selectRaw( + "knowledge_chunks.*, ts_rank(search_tsv, websearch_to_tsquery('russian', ?)) AS rank", + [$question] + ) + ->whereRaw("search_tsv @@ websearch_to_tsquery('russian', ?)", [$question]) + ->orderByDesc('rank') + ->limit($limit) + ->get(); + + return $hits->all(); + } +} diff --git a/app/tests/Feature/Bot/KnowledgeSearchTest.php b/app/tests/Feature/Bot/KnowledgeSearchTest.php new file mode 100644 index 00000000..ab21cad1 --- /dev/null +++ b/app/tests/Feature/Bot/KnowledgeSearchTest.php @@ -0,0 +1,44 @@ + 'help/p.md', 'title' => 'Что такое проект', 'tour' => 'create-project', + 'topics' => 'создать проект, заявка на лиды', 'chunk_index' => 0, + 'content' => 'Проект — это заявка на поток клиентов с выбранного источника.', + ]); + KnowledgeChunk::create([ + 'source_path' => 'help/b.md', 'title' => 'Как пополнить баланс', 'tour' => 'top-up-balance', + 'topics' => 'пополнить, закинуть деньги, оплата', 'chunk_index' => 0, + 'content' => 'Пополнить баланс: раздел Биллинг, кнопка Пополнить.', + ]); +}); + +it('находит релевантный чанк и ранжирует его первым', function () { + $hits = app(KnowledgeSearch::class)->search('а что такое проект?', 3); + + expect($hits)->not->toBeEmpty() + ->and($hits[0]->title)->toBe('Что такое проект'); +}); + +it('находит по синонимам из topics («закинуть деньги»)', function () { + $hits = app(KnowledgeSearch::class)->search('как закинуть деньги', 3); + + expect($hits)->not->toBeEmpty() + ->and($hits[0]->title)->toBe('Как пополнить баланс'); +}); + +it('на вопрос не по теме возвращает пусто', function () { + expect(app(KnowledgeSearch::class)->search('какая погода в москве', 3))->toBeEmpty(); +}); + +it('не падает на спецсимволах в вопросе', function () { + expect(app(KnowledgeSearch::class)->search('проект & | ! ( )', 3))->toBeArray(); +});