Created
January 7, 2023 22:06
-
-
Save danny-avila/05ab0e6ca3cc2d18d7a565eb87482d52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import weaviate from 'weaviate-client'; | |
import fs from 'fs'; | |
// if you use Docker-compose | |
const client = weaviate.client({ | |
scheme: 'http', | |
host: 'localhost:8080' | |
}); | |
async function importDocument(err, file) { | |
const data = JSON.parse(file); | |
// Prepare a batcher | |
let batcher = client.batch.objectsBatcher(); | |
let counter = 0; | |
// Construct an object with a class, id, properties | |
const documentObj = { | |
class: 'Document', | |
id: data.id, | |
properties: { | |
title: 'Secrets of a Javascript Ninja', | |
added: Date.now(), | |
description: 'Javascript secrets book by John Resig' | |
} | |
}; | |
// add the object to the batch queue | |
batcher = batcher.withObject(documentObj); | |
data.pages.forEach((page) => { | |
// Construct an object with a class, id, properties and vector | |
const obj = { | |
class: 'Page', | |
id: page.id, | |
properties: { | |
Number: page.pageNumber | |
} | |
}; | |
// add the object to the batch queue | |
batcher = batcher.withObject(obj); | |
// When the batch counter reaches 20, push the objects to Weaviate | |
if (counter++ == 20) { | |
// flush the batch queue | |
batcher | |
.do() | |
.then((res) => { | |
console.log(res); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}); | |
// restart the batch queue | |
counter = 0; | |
batcher = client.batch.objectsBatcher(); | |
} | |
}); | |
data.paragraphs.forEach((paragraph) => { | |
// Construct an object with a class, id, properties and vector | |
const obj = { | |
class: 'Paragraph', | |
id: paragraph.id, | |
properties: { | |
Number: paragraph.order, | |
text: paragraph.text | |
} | |
}; | |
// add the object to the batch queue | |
batcher = batcher.withObject(obj); | |
// When the batch counter reaches 20, push the objects to Weaviate | |
if (counter++ == 20) { | |
// flush the batch queue | |
batcher | |
.do() | |
.then((res) => { | |
console.log(res); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}); | |
// restart the batch queue | |
counter = 0; | |
batcher = client.batch.objectsBatcher(); | |
} | |
}); | |
// Flush the remaining objects | |
batcher | |
.do() | |
.then((res) => { | |
console.log(res); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}); | |
} | |
fs.readFile('./document.json', 'utf8', async (err, file) => await importDocument(err, file)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment