|
@@ -4,7 +4,7 @@ namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\DhammaTerm;
|
|
use App\Models\DhammaTerm;
|
|
|
use App\Services\OpenSearchService;
|
|
use App\Services\OpenSearchService;
|
|
|
-use App\Services\TermService;
|
|
|
|
|
|
|
+use App\Services\TermIndexService;
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -25,7 +25,7 @@ class IndexTerm extends Command
|
|
|
|
|
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
protected OpenSearchService $openSearchService,
|
|
protected OpenSearchService $openSearchService,
|
|
|
- protected TermService $termService,
|
|
|
|
|
|
|
+ protected TermIndexService $termIndexService,
|
|
|
) {
|
|
) {
|
|
|
parent::__construct();
|
|
parent::__construct();
|
|
|
}
|
|
}
|
|
@@ -82,7 +82,12 @@ class IndexTerm extends Command
|
|
|
Cache::put(self::CACHE_KEY, $term->id, now()->addHours(48));
|
|
Cache::put(self::CACHE_KEY, $term->id, now()->addHours(48));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $this->indexTerm($term->guid);
|
|
|
|
|
|
|
+ if ($this->isTest) {
|
|
|
|
|
+ $document = $this->termIndexService->buildDocument($term->guid);
|
|
|
|
|
+ $this->info($document['title']['text']['pali']);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->termIndexService->index($term->guid);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 全部完成,清除断点缓存
|
|
// 全部完成,清除断点缓存
|
|
@@ -97,105 +102,4 @@ class IndexTerm extends Command
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 构建单条词条文档并写入 OpenSearch
|
|
|
|
|
- *
|
|
|
|
|
- * 文档结构遵循新版 mapping:
|
|
|
|
|
- * title.text.pali / title.text.zh → 全文检索
|
|
|
|
|
- * title.suggest.pali / title.suggest.zh → 自动建议
|
|
|
|
|
- * content.text.pali / content.text.zh → 正文内容
|
|
|
|
|
- *
|
|
|
|
|
- * @param string $id DhammaTerm 的 guid
|
|
|
|
|
- */
|
|
|
|
|
- protected function indexTerm(string $id): void
|
|
|
|
|
- {
|
|
|
|
|
- $termData = $this->termService->find($id, 'text');
|
|
|
|
|
- $channelName = $termData['channel']['name'] ?? '';
|
|
|
|
|
- $isCommunity = $this->termService->isCommunity($termData['channel_id']);
|
|
|
|
|
- $content = $termData['html'] ?? $termData['meaning'];
|
|
|
|
|
-
|
|
|
|
|
- $categories = $this->extractCategories($termData['note'] ?? '');
|
|
|
|
|
- $quality = $this->extractFirstQuality($termData['note'] ?? '');
|
|
|
|
|
- $tags = [];
|
|
|
|
|
- foreach ($categories as $key => $category) {
|
|
|
|
|
- $tags[] = "category:{$category}";
|
|
|
|
|
- }
|
|
|
|
|
- if (! empty($quality)) {
|
|
|
|
|
- $tags[] = "quality:{$quality}";
|
|
|
|
|
- }
|
|
|
|
|
- $document = [
|
|
|
|
|
- 'id' => "term_{$id}",
|
|
|
|
|
- 'resource_id' => $id,
|
|
|
|
|
- 'resource_type' => 'term',
|
|
|
|
|
- 'title' => [
|
|
|
|
|
- 'text' => [
|
|
|
|
|
- 'pali' => $termData['word'],
|
|
|
|
|
- 'zh' => $termData['meaning'],
|
|
|
|
|
- ],
|
|
|
|
|
- 'suggest' => [
|
|
|
|
|
- 'pali' => [$termData['word']],
|
|
|
|
|
- 'zh' => [$termData['meaning']],
|
|
|
|
|
- ],
|
|
|
|
|
- ],
|
|
|
|
|
- 'summary' => [
|
|
|
|
|
- 'text' => $termData['summary'] ?? '',
|
|
|
|
|
- ],
|
|
|
|
|
- 'content' => [],
|
|
|
|
|
- 'bold_single' => [$termData['meaning'], $termData['word']],
|
|
|
|
|
- 'related_id' => $termData['word'],
|
|
|
|
|
- 'category' => null,
|
|
|
|
|
- 'tags' => $tags,
|
|
|
|
|
- 'language' => $termData['language'],
|
|
|
|
|
- 'updated_at' => now()->toIso8601String(),
|
|
|
|
|
- 'path' => $termData['studio']['realName']."/{$channelName}",
|
|
|
|
|
- 'metadata' => ['channel' => $termData['channel_id']],
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- // TODO: 补充语言判断,将内容放入对应的 text.pali 或 text.zh 字段
|
|
|
|
|
- $plainText = strip_tags($content);
|
|
|
|
|
- if (str_contains($termData['language'], 'zh')) {
|
|
|
|
|
- $document['content']['text']['zh'] = $plainText;
|
|
|
|
|
- } else {
|
|
|
|
|
- $document['content']['text']['zh'] = $plainText;
|
|
|
|
|
- }
|
|
|
|
|
- $document['content']['display'] = $content; // 展示
|
|
|
|
|
-
|
|
|
|
|
- if ($this->isTest) {
|
|
|
|
|
- $this->info($document['title']['text']['pali']);
|
|
|
|
|
- // $this->info($document['summary']['text']);
|
|
|
|
|
- } else {
|
|
|
|
|
- $this->openSearchService->create($document['id'], $document);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 提取 Markdown 中的 {{category|...}} 分类标签
|
|
|
|
|
- */
|
|
|
|
|
- private function extractCategories(string $content): array
|
|
|
|
|
- {
|
|
|
|
|
- if (empty($content)) {
|
|
|
|
|
- return [];
|
|
|
|
|
- }
|
|
|
|
|
- preg_match_all('/\{\{category\|([^}]+)\}\}/u', $content, $matches);
|
|
|
|
|
-
|
|
|
|
|
- return array_values(array_filter(array_map(
|
|
|
|
|
- fn ($item) => trim($item),
|
|
|
|
|
- $matches[1] ?? []
|
|
|
|
|
- )));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 提取 Markdown 中第一个 {{quality|...}} 标签内的内容
|
|
|
|
|
- */
|
|
|
|
|
- private function extractFirstQuality(string $content): string
|
|
|
|
|
- {
|
|
|
|
|
- if (empty($content)) {
|
|
|
|
|
- return '';
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- preg_match('/\{\{quality\|([^}]+)\}\}/u', $content, $matches);
|
|
|
|
|
-
|
|
|
|
|
- return isset($matches[1]) ? trim($matches[1]) : '';
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|