|
1 |
| -const redis = require('redis'); |
2 |
| -const redisearch = require('redis-redisearch'); |
| 1 | +const redis = require('redis'); |
| 2 | +const redisearch = require('redis-redisearch'); |
3 | 3 |
|
4 |
| -let redisUrl = process.env.REDIS_URL || "redis://localhost:6379"; |
5 |
| -let indexName = process.env.REDIS_INDEX || "idx:movie"; |
| 4 | +const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379'; |
| 5 | +const indexName = process.env.REDIS_INDEX || 'idx:movie'; |
6 | 6 |
|
7 |
| -console.log("Configuration Index: "+indexName+" - redisUrl: "+redisUrl); |
| 7 | +console.log(`Configuration Index: ${indexName} - redisUrl: ${redisUrl}`); |
8 | 8 |
|
9 | 9 | redisearch(redis);
|
10 |
| -let client = redis.createClient(redisUrl); |
| 10 | +const client = redis.createClient(redisUrl); |
11 | 11 |
|
12 | 12 |
|
13 |
| -let SearchService = function() { |
| 13 | +const SearchService = function () { |
14 | 14 |
|
15 |
| - let _search = function(queryString, options, callback) { |
| 15 | + const _search = function (queryString, options, callback) { |
16 | 16 |
|
17 |
| - let offset = 0; // default values |
18 |
| - let limit = 10; // default value |
| 17 | + let offset = 0; // default values |
| 18 | + let limit = 10; // default value |
19 | 19 |
|
20 |
| - |
21 |
| - // prepare the "native" FT.SEARCK call |
22 |
| - // FT.SEARCH IDX_NAME queryString [options] |
23 |
| - let searchParams = [ |
24 |
| - indexName, // name of thje inde |
25 |
| - queryString, // query string, |
26 |
| - "WITHSCORES" // return the score |
27 |
| - ]; |
28 | 20 |
|
29 |
| - // if limit add the parameters |
30 |
| - if (options.offset || options.limit ) { |
31 |
| - offset = options.offset||0; |
32 |
| - limit = options.limit||10 |
33 |
| - searchParams.push("LIMIT"); |
34 |
| - searchParams.push(offset); |
35 |
| - searchParams.push(limit); |
36 |
| - } |
37 |
| - // if sortby add the parameters |
38 |
| - if (options.sortBy) { |
39 |
| - searchParams.push("SORTBY"); |
40 |
| - searchParams.push(options.sortBy); |
41 |
| - searchParams.push( (options.ascending)?"ASC":"DESC" ); |
42 |
| - |
| 21 | + // prepare the "native" FT.SEARCH call |
| 22 | + // FT.SEARCH IDX_NAME queryString [options] |
| 23 | + const searchParams = [ |
| 24 | + indexName, // name of the index |
| 25 | + queryString, // query string |
| 26 | + 'WITHSCORES' // return the score |
| 27 | + ]; |
| 28 | + |
| 29 | + // if limit add the parameters |
| 30 | + if (options.offset || options.limit) { |
| 31 | + offset = options.offset || 0; |
| 32 | + limit = options.limit || 10 |
| 33 | + searchParams.push('LIMIT'); |
| 34 | + searchParams.push(offset); |
| 35 | + searchParams.push(limit); |
| 36 | + } |
| 37 | + // if sortby add the parameters |
| 38 | + if (options.sortBy) { |
| 39 | + searchParams.push('SORTBY'); |
| 40 | + searchParams.push(options.sortBy); |
| 41 | + searchParams.push((options.ascending) ? 'ASC' : 'DESC'); |
| 42 | + } |
| 43 | + |
| 44 | + console.log(searchParams); |
| 45 | + |
| 46 | + client.ft_search( |
| 47 | + searchParams, |
| 48 | + function (err, searchResult) { |
| 49 | + |
| 50 | + const totalNumberOfDocs = searchResult[0]; |
| 51 | + const result = { |
| 52 | + meta: { |
| 53 | + totalResults: totalNumberOfDocs, |
| 54 | + offset, |
| 55 | + limit, |
| 56 | + queryString, |
| 57 | + }, |
| 58 | + docs: [], |
| 59 | + raw_docs: searchResult |
43 | 60 | }
|
44 | 61 |
|
45 |
| - console.log( searchParams ); |
46 |
| - |
47 |
| - client.ft_search( |
48 |
| - searchParams, |
49 |
| - function(err, searchResult) { |
50 |
| - |
51 |
| - let totalNumberOfDocs = searchResult[0]; |
52 |
| - let result = { |
53 |
| - "meta" : { |
54 |
| - "totalResults" : totalNumberOfDocs, |
55 |
| - "offset" : offset, |
56 |
| - "limit" : limit, |
57 |
| - "queryString" : queryString, |
58 |
| - }, |
59 |
| - "docs" : [], |
60 |
| - "raw_docs" : searchResult |
61 |
| - } |
62 |
| - |
63 |
| - // create JSON document from n/v paires |
64 |
| - for (var i = 1; i <= searchResult.length-1 ; i++){ |
65 |
| - let doc = { |
66 |
| - meta : { |
67 |
| - score : Number(searchResult[i+1]), |
68 |
| - id : searchResult[i], |
69 |
| - } |
70 |
| - }; |
71 |
| - i = i+2; |
72 |
| - doc.fields = {}; |
73 |
| - let fields = searchResult[i] |
74 |
| - if (fields) { |
75 |
| - for (var j=0, len=fields.length; j < len; j++) { |
76 |
| - let idxKey = j; |
77 |
| - let idxValue =idxKey+1; |
78 |
| - j++; |
79 |
| - doc.fields[fields[idxKey]] = fields[idxValue]; |
80 |
| - } |
81 |
| - } |
82 |
| - result.docs.push(doc) |
83 |
| - } |
84 |
| - |
85 |
| - |
86 |
| - |
87 |
| - callback(err,result); |
| 62 | + // create JSON document from n/v pairs |
| 63 | + for (let i = 1; i <= searchResult.length - 1; i++) { |
| 64 | + const doc = { |
| 65 | + meta: { |
| 66 | + score: Number(searchResult[i + 1]), |
| 67 | + id: searchResult[i] |
| 68 | + } |
| 69 | + }; |
| 70 | + i = i + 2; |
| 71 | + doc.fields = {}; |
| 72 | + const fields = searchResult[i] |
| 73 | + if (fields) { |
| 74 | + for (let j = 0, len = fields.length; j < len; j++) { |
| 75 | + const idxKey = j; |
| 76 | + const idxValue = idxKey + 1; |
| 77 | + j++; |
| 78 | + doc.fields[fields[idxKey]] = fields[idxValue]; |
88 | 79 | }
|
89 |
| - ); |
| 80 | + } |
| 81 | + result.docs.push(doc); |
| 82 | + } |
90 | 83 |
|
91 |
| - } |
| 84 | + callback(err, result); |
| 85 | + } |
| 86 | + ); |
92 | 87 |
|
93 |
| - let _getMovieGroupBy = function(field, callback) { |
94 |
| - let retValue = { |
95 |
| - totalResults : 0, |
96 |
| - rows : [], |
97 |
| - raw : [] // get the data as returned by the API |
98 |
| - }; |
99 |
| - |
100 |
| - // prepare the "native" FT.AGGREGATE call |
101 |
| - // FT.AGGREGATE IDX_NAME queryString [options] |
102 |
| - let pipeline = [ |
103 |
| - indexName, // name of thje inde |
104 |
| - "*", // query string, |
105 |
| - "GROUPBY", "1", `@${field}`, // group by |
106 |
| - "REDUCE", "COUNT", "0", "AS", "nb_of_movies", //count the numbe rof movie by group |
107 |
| - "SORTBY", "2", `@${field}`, "ASC", // sorted by the genre |
108 |
| - "LIMIT", "0", "1000", // get all genre expecting less than 100 genres |
109 |
| - ]; |
110 |
| - |
111 |
| - client.ft_aggregate( |
112 |
| - pipeline, |
113 |
| - function(err, aggrResult) { |
114 |
| - |
115 |
| - // transform array into document |
116 |
| - // this should be added to a generic function |
117 |
| - // ideally into the library itself |
118 |
| - retValue.totalResults = aggrResult[0]; |
119 |
| - |
120 |
| - // loop on the results starting at element 1 |
121 |
| - for (var i = 1; i <= aggrResult.length-1 ; i++){ |
122 |
| - const item = aggrResult[i]; |
123 |
| - let doc = {}; |
124 |
| - for (var j=0, len=item.length; j < len; j++) { |
125 |
| - doc[ item[j] ] = item[j+1] |
126 |
| - doc[ item[j+2] ] = item[j+3] |
127 |
| - j=j+3; |
128 |
| - } |
129 |
| - retValue.rows.push(doc); |
130 |
| - } |
131 |
| - retValue.raw = aggrResult; |
132 |
| - callback(err,retValue); |
133 |
| - }); |
| 88 | + } |
134 | 89 |
|
135 |
| - } |
| 90 | + const _getMovieGroupBy = function (field, callback) { |
| 91 | + const retValue = { |
| 92 | + totalResults: 0, |
| 93 | + rows: [], |
| 94 | + raw: [] // get the data as returned by the API |
| 95 | + }; |
136 | 96 |
|
| 97 | + // prepare the "native" FT.AGGREGATE call |
| 98 | + // FT.AGGREGATE IDX_NAME queryString [options] |
| 99 | + const pipeline = [ |
| 100 | + indexName, // name of the index |
| 101 | + '*', // query string, |
| 102 | + 'GROUPBY', '1', `@${field}`, // group by |
| 103 | + 'REDUCE', 'COUNT', '0', 'AS', 'nb_of_movies', //count the number of movies by group |
| 104 | + 'SORTBY', '2', `@${field}`, 'ASC', // sorted by the genre |
| 105 | + 'LIMIT', '0', '1000' // get all genre expecting less than 100 genres |
| 106 | + ]; |
| 107 | + |
| 108 | + client.ft_aggregate( |
| 109 | + pipeline, |
| 110 | + function (err, aggrResult) { |
| 111 | + |
| 112 | + // transform array into document |
| 113 | + // this should be added to a generic function |
| 114 | + // ideally into the library itself |
| 115 | + retValue.totalResults = aggrResult[0]; |
| 116 | + |
| 117 | + // loop on the results starting at element 1 |
| 118 | + for (let i = 1; i <= aggrResult.length - 1; i++) { |
| 119 | + const item = aggrResult[i]; |
| 120 | + const doc = {}; |
| 121 | + for (let j = 0, len = item.length; j < len; j++) { |
| 122 | + doc[item[j]] = item[j + 1]; |
| 123 | + doc[item[j + 2]] = item[j + 3]; |
| 124 | + j = j + 3; |
| 125 | + } |
| 126 | + retValue.rows.push(doc); |
| 127 | + } |
| 128 | + retValue.raw = aggrResult; |
| 129 | + callback(err, retValue); |
| 130 | + }); |
137 | 131 |
|
138 |
| - return { |
139 |
| - search: _search, |
140 |
| - getMovieGroupBy: _getMovieGroupBy, |
141 |
| - }; |
| 132 | + } |
142 | 133 |
|
| 134 | + return { |
| 135 | + search: _search, |
| 136 | + getMovieGroupBy: _getMovieGroupBy |
| 137 | + }; |
143 | 138 | }
|
144 | 139 |
|
145 | 140 | module.exports = SearchService;
|
0 commit comments