liyugang
3 天以前 8c24730e9a52dc2c8933e8d41d2f9651de48a231
src/book-list-to-es.mjs
@@ -62,20 +62,27 @@
}
async function searchBook() {
  const books = [];
  const workSheets = xlsx.parse("./fictionnoisbn.xlsx");
  let books = [];
  const workSheets = xlsx.parse("./archive-books.xlsx");
  for (const sheet of workSheets) {
    books.push(...sheet.data.map(row => ({ id: row[0], title: row[1], author: row[2] })));
    books = books.concat(sheet.data.slice(1).map(row => ({ id: row[0], title: row[1], author: row[2] })));
  }
  const bookMap = new Map();
  let cnt = 0;
  let bookCnt = 0;
  const promiseList = [];
  for (const book of books) {
    cnt++;
    if (cnt % 1000 == 0) {
      console.log('当前%d', cnt);
    }
    if (!book.title) { continue; }
    const resp = await client.search({
    if (bookMap.has(book.title + book.author)) {
      continue;
    } else {
      bookMap.set(book.title + book.author, book);
    }
    const promise = client.search({
      index: 'books',
      size: 1,
      query: {
@@ -97,17 +104,23 @@
          'title': book.title,
        }
      }
    }).then(resp => {
      if ((resp.hits.max_score ?? 0) < 25) { return; }
      const isbn = resp.hits.hits[0]?._source?.isbn;
      if (isbn) {
        book.isbn = isbn;
        book.title2 = resp.hits.hits[0]?._source?.title;
        book.author2 = resp.hits.hits[0]?._source?.author;
      }
      bookCnt++;
      if (bookCnt % 1000 == 0) {
        console.log('已匹配:%s', bookCnt);
      }
    });
    if ((resp.hits.max_score ?? 0) < 25) { continue; }
    const isbn = resp.hits.hits[0]?._source?.isbn;
    if (isbn) {
      book.isbn = isbn;
      book.title2 = resp.hits.hits[0]?._source?.title;
      book.author2 = resp.hits.hits[0]?._source?.author;
    }
    bookCnt++;
    if (bookCnt % 1000 == 0) {
      console.log('已匹配:%s', bookCnt);
    promiseList.push(promise);
    if (promiseList.length >= 20) {
      await Promise.all(promiseList);
      promiseList.length = 0;
    }
  }
  console.log(bookCnt);
@@ -115,7 +128,7 @@
}
async function saveToDb(books) {
  const db = new sqlite3.Database("./book-list-result.db");
  const db = new sqlite3.Database("./book-list-result2.db");
  db.serialize(function () {
    db.run("CREATE TABLE IF NOT EXISTS t_books (id TEXT PRIMARY KEY, Title TEXT, Author TEXT, ISBN TEXT, Title2 TEXT, Author2 TEXT)");
    db.run("BEGIN TRANSACTION");