Files
openserp/examples/content/js-search-with-extract/index.js
2026-06-09 04:14:35 +03:00

30 lines
1009 B
JavaScript

import { OpenSERP } from "@openserp/sdk";
// Hosted API instead? Get a key at https://openserp.org/dashboard/keys:
// const client = new OpenSERP({ apiKey: "<YOUR_API_TOKEN>", timeoutMs: 60_000 });
const client = new OpenSERP({ baseUrl: "http://localhost:7000", timeoutMs: 60_000 });
// `extract: true` fetches the top pages and returns their cleaned content
// alongside each result, so you get the page text in a single request.
// `extractTop` (max 5) controls how many results are enriched.
const { results } = await client.search({
engine: "ecosia",
text: "what is a serp api",
extract: true,
extractTop: 2,
extractMode: "auto",
});
for (const item of results) {
console.log(`${item.rank}. ${item.title}`);
console.log(` ${item.url}`);
// When extracted, `item.extracted.content` holds the page body. Trim it to
// a short preview here.
const content = item.extracted?.content;
if (content) {
console.log(` ${content.slice(0, 300).trim()}`);
}
console.log();
}