mikmc commited on
Commit
6996b39
·
verified ·
1 Parent(s): 6caeb01

Update commons/queryBuilder.js

Browse files
Files changed (1) hide show
  1. commons/queryBuilder.js +13 -21
commons/queryBuilder.js CHANGED
@@ -1,39 +1,31 @@
1
  const { convert } = require('lucene-query-generator');
2
 
3
  /**
4
- * Builds and formats the Lucene query for the catalog search.
5
  * Ensures all query terms are properly formatted as strings.
6
  */
7
  module.exports = (args) => {
8
  const query = {};
9
 
10
  if (args.extra) {
11
- // Process `extra` parameters, converting values to strings if necessary
12
  Object.keys(args.extra).forEach((key) => {
13
  let value = args.extra[key];
14
-
15
- // Convert non-string values to strings
16
- if (typeof value !== 'string') {
17
- value = String(value);
18
- }
19
-
20
- // Add non-empty strings to the query
21
- if (value && value.trim().length > 0) {
22
- query[key] = value;
23
- }
24
  });
25
  }
26
 
27
- // Generate the Lucene query string only if the query object is not empty
28
- if (Object.keys(query).length > 0) {
29
- try {
30
- return convert(query); // Corrected usage of `convert`
31
- } catch (error) {
32
- console.error("Error generating Lucene query:", error);
33
- return ''; // Return an empty query if generation fails
34
- }
 
 
35
  }
36
 
37
- // Return an empty string if no query was generated
38
  return '';
39
  };
 
1
  const { convert } = require('lucene-query-generator');
2
 
3
  /**
4
+ * Builds a complex Lucene query formatted for Archive.org based on arguments.
5
  * Ensures all query terms are properly formatted as strings.
6
  */
7
  module.exports = (args) => {
8
  const query = {};
9
 
10
  if (args.extra) {
 
11
  Object.keys(args.extra).forEach((key) => {
12
  let value = args.extra[key];
13
+ if (typeof value !== 'string') value = String(value);
14
+ if (value && value.trim().length > 0) query[key] = value;
 
 
 
 
 
 
 
 
15
  });
16
  }
17
 
18
+ // Generate the Lucene query using a field-focused structure
19
+ const formattedQuery = convert({
20
+ title: query.genre,
21
+ subject: query.genre,
22
+ description: query.genre,
23
+ collection: query.genre,
24
+ });
25
+
26
+ if (formattedQuery) {
27
+ return `(${formattedQuery}) AND !collection:(podcasts OR radio OR uspto)`;
28
  }
29
 
 
30
  return '';
31
  };