Просмотр исходного кода

🐛 (search): 章节索引漏掉每本书的最后一章

IndexTipitaka::indexChapter 求最后一章的结束段时用了 ->first()->value('paragraph'):
模型实例上的 value() 会新开一个不带条件的查询,拿到全表第一行的段号(book 162 得到 858,
真实最后一段是 4669),最后一章的范围为空、从不被索引。改成 max('paragraph'),补回归测试。
线上要重建章节索引才会生效。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 5 дней назад
Родитель
Сommit
2f89734d57

+ 3 - 3
api-v13/app/Console/Commands/IndexTipitaka.php

@@ -289,9 +289,9 @@ class IndexTipitaka extends Command
              */
              */
             $start = $chapter->paragraph;
             $start = $chapter->paragraph;
             if ($key === count($chapters) - 1) {
             if ($key === count($chapters) - 1) {
-                $end = PaliText::where('book', $book)
-                    ->orderBy('paragraph', 'desc')->first()
-                    ->value('paragraph');
+                // 不能写 ->first()->value('paragraph'):模型实例上的 value() 会新开一个不带条件的查询,
+                // 拿到全表第一行的段号,最后一章的范围变成空的、从不被索引
+                $end = PaliText::where('book', $book)->max('paragraph');
             } else {
             } else {
                 $end = $chapters[$key + 1]->paragraph - 1;
                 $end = $chapters[$key + 1]->paragraph - 1;
             }
             }

+ 76 - 0
api-v13/tests/Feature/IndexTipitakaChapterTest.php

@@ -0,0 +1,76 @@
+<?php
+
+use App\Models\PaliText;
+use App\Services\OpenSearchService;
+use App\Services\PaliContentService;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Str;
+
+uses(RefreshDatabase::class);
+
+/*
+ * opensearch:index-tipitaka --granularity=chapter:每个「章节 × channel」建一份文档。
+ *
+ * 回归:求一本书最后一章的结束段时曾用 `->first()->value('paragraph')`,模型实例上的 value()
+ * 新开不带条件的查询、拿到全表第一行的段号,最后一章的范围为空,从不被索引。
+ * OpenSearch 与取文都换成假的,只看建了哪些文档。
+ */
+
+function itcParagraph(int $book, int $para, int $level, string $toc = ''): void
+{
+    (new PaliText)->forceFill([
+        'book' => $book,
+        'paragraph' => $para,
+        'level' => $level,
+        'class' => '',
+        'toc' => $toc,
+        'text' => $toc,
+        'html' => '',
+        'pcd_book_id' => 0,
+        'chapter_len' => 1,
+        'chapter_strlen' => 10,
+        'uid' => (string) Str::uuid(),
+    ])->save();
+}
+
+it('indexes every chapter of a book, including the last one', function () {
+    // 先放一本段号更小的书:修复前 value() 拿到的「全表第一行」就是它,最后一章的范围变空
+    itcParagraph(1, 1, 1, 'other book');
+
+    itcParagraph(9300, 3, 1, 'Book');
+    itcParagraph(9300, 4, 2, '1. First');
+    itcParagraph(9300, 5, 100);
+    itcParagraph(9300, 6, 2, '2. Last');
+    itcParagraph(9300, 7, 100);
+    itcParagraph(9300, 8, 100);
+
+    $channel = makeChannel((string) Str::uuid(), 'pali');
+    foreach ([4, 5, 6, 7, 8] as $para) {
+        makeSentence($channel, 9300, $para, 1, "para {$para}");
+    }
+
+    $this->mock(PaliContentService::class, function ($mock) {
+        $mock->shouldReceive('readParagraph')->andReturnUsing(fn ($book, $para) => [
+            'display' => "<p>{$para}</p>",
+            'sentences' => [['html' => "title {$para}"]],
+        ]);
+    });
+    $created = [];
+    $this->mock(OpenSearchService::class, function ($mock) use (&$created) {
+        $mock->shouldReceive('testConnection')->andReturn([true, 'ok']);
+        $mock->shouldReceive('create')->andReturnUsing(function (string $id, array $body) use (&$created) {
+            $created[$id] = $body;
+
+            return [];
+        });
+    });
+
+    $this->artisan('opensearch:index-tipitaka', ['book' => 9300, '--granularity' => 'chapter', '--summary' => 'off'])
+        ->assertExitCode(0);
+
+    // 书名那一段(9300:3)到下一个标题之间没有句子,不建文档
+    expect(array_keys($created))->toBe([
+        "tipitaka_chapter_9300-4_{$channel}",
+        "tipitaka_chapter_9300-6_{$channel}",
+    ])->and($created["tipitaka_chapter_9300-6_{$channel}"]['content']['text']['zh'])->toBe('678');
+});