Spaces:
Paused
Paused
Update commons/queryBuilder.js
Browse files1234456789101111121415161718192021222313141516172728293031323334181920212223242526272829373031const { convert } = require('lucene-query-generator');
/** * Builds and formats the Lucene query for the catalog search. * Builds a complex Lucene query formatted for Archive.org based on arguments. * Ensures all query terms are properly formatted as strings. */module.exports = (args) => { const query = {};
if (args.extra) { // Process `extra` parameters, converting values to strings if necessary Object.keys(args.extra).forEach((key) => { let value = args.extra[key];
// Convert non-string values to strings if (typeof value !== 'string') { value = String(value); }
// Add non-empty strings to the query if (value && value.trim().length > 0) { query[key] = value; } if (typeof value !== 'string') value = String(value); if (value && value.trim().length > 0) query[key] = value; }); }
// Generate the Lucene query string only if the query object is not empty if (Object.keys(query).length > 0) { try { return convert(query); // Corrected usage of `convert` } catch (error) { console.error("Error generating Lucene query:", error); return ''; // Return an empty query if generation fails } // Generate the Lucene query using a field-focused structure const formattedQuery = convert({ title: query.genre, subject: query.genre, description: query.genre, collection: query.genre, });
if (formattedQuery) { return `(${formattedQuery}) AND !collection:(podcasts OR radio OR uspto)`; }
// Return an empty string if no query was generated return '';};commons/queryBuilder.js+13-21