Remove genkey.js and push2typesense.js from repository; add to .gitignore
This commit is contained in:
27
genkey.js
27
genkey.js
@@ -1,27 +0,0 @@
|
||||
const http = require('https');
|
||||
|
||||
const data = JSON.stringify({
|
||||
"description": "Public search only key",
|
||||
"actions": ["documents:search"],
|
||||
"collections": ["blogs"]
|
||||
});
|
||||
|
||||
const options = {
|
||||
hostname: 'typesense.biss.click', // 不要带 https://
|
||||
port: 443,
|
||||
path: '/keys',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-TYPESENSE-API-KEY': 'tDPfzkghH3Zy55DHyWOnYkQiijOqN8bx',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': data.length
|
||||
}
|
||||
};
|
||||
|
||||
const req = http.request(options, res => {
|
||||
res.on('data', d => { process.stdout.write(d); });
|
||||
});
|
||||
|
||||
req.on('error', error => { console.error(error); });
|
||||
req.write(data);
|
||||
req.end();
|
||||
@@ -1,69 +0,0 @@
|
||||
const Typesense = require('typesense');
|
||||
const fs = require('fs');
|
||||
const xml2js = require('xml2js');
|
||||
|
||||
// --- 配置区域 ---
|
||||
const CONFIG = {
|
||||
apiKey: 'tDPfzkghH3Zy55DHyWOnYkQiijOqN8bx', // 必须是 Admin Key
|
||||
host: 'typesense.biss.click',
|
||||
port: 443,
|
||||
protocol: 'https',
|
||||
collectionName: 'blogs'
|
||||
};
|
||||
|
||||
const client = new Typesense.Client({
|
||||
'nodes': [{ 'host': CONFIG.host, 'port': CONFIG.port, 'protocol': CONFIG.protocol }],
|
||||
'apiKey': CONFIG.apiKey,
|
||||
'connectionTimeoutSeconds': 5
|
||||
});
|
||||
|
||||
async function sync() {
|
||||
try {
|
||||
// 1. 读取并解析 XML
|
||||
const xml = fs.readFileSync('./public/search.xml', 'utf8');
|
||||
const parser = new xml2js.Parser({ explicitArray: false });
|
||||
const result = await parser.parseStringPromise(xml);
|
||||
|
||||
// 提取文章列表 (处理单篇文章和多篇文章的情况)
|
||||
let entries = result.search.entry;
|
||||
if (!Array.isArray(entries)) entries = [entries];
|
||||
|
||||
// 格式化数据以适配 Typesense
|
||||
const documents = entries.map(post => ({
|
||||
title: post.title,
|
||||
url: post.url,
|
||||
content: post.content,
|
||||
categories: post.categories ? (Array.isArray(post.categories.category) ? post.categories.category : [post.categories.category]) : [],
|
||||
tags: post.tags ? (Array.isArray(post.tags.tag) ? post.tags.tag : [post.tags.tag]) : [],
|
||||
}));
|
||||
|
||||
// 2. 检查或创建 Collection (Schema)
|
||||
try {
|
||||
await client.collections(CONFIG.collectionName).retrieve();
|
||||
} catch (err) {
|
||||
const schema = {
|
||||
name: CONFIG.collectionName,
|
||||
fields: [
|
||||
// 关键点:指定 locale 为 'zh' 开启中文分词
|
||||
{ name: 'title', type: 'string', locale: 'zh' },
|
||||
{ name: 'content', type: 'string', locale: 'zh' },
|
||||
{ name: 'url', type: 'string' },
|
||||
{ name: 'categories', type: 'string[]', facet: true },
|
||||
{ name: 'tags', type: 'string[]', facet: true }
|
||||
]
|
||||
};
|
||||
await client.collections().create(schema);
|
||||
console.log('Collection created with Chinese support!');
|
||||
}
|
||||
|
||||
// 3. 导入数据 (使用 upsert 模式:存在则更新,不存在则创建)
|
||||
console.log(`Syncing ${documents.length} posts to Typesense...`);
|
||||
await client.collections(CONFIG.collectionName).documents().import(documents, { action: 'upsert' });
|
||||
console.log('Sync complete!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Sync failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
sync();
|
||||
Reference in New Issue
Block a user