Skip to content

Commit 34c9f1e

Browse files
author
Simon Prickett
committed
Various tidy up.
1 parent 2815a0d commit 34c9f1e

File tree

2 files changed

+149
-153
lines changed

2 files changed

+149
-153
lines changed
Lines changed: 118 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,140 @@
1-
const redis = require('redis');
2-
const redisearch = require('redis-redisearch');
1+
const redis = require('redis');
2+
const redisearch = require('redis-redisearch');
33

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';
66

7-
console.log("Configuration Index: "+indexName+" - redisUrl: "+redisUrl);
7+
console.log(`Configuration Index: ${indexName} - redisUrl: ${redisUrl}`);
88

99
redisearch(redis);
10-
let client = redis.createClient(redisUrl);
10+
const client = redis.createClient(redisUrl);
1111

1212

13-
let SearchService = function() {
13+
const SearchService = function () {
1414

15-
let _search = function(queryString, options, callback) {
15+
const _search = function (queryString, options, callback) {
1616

17-
let offset = 0; // default values
18-
let limit = 10; // default value
17+
let offset = 0; // default values
18+
let limit = 10; // default value
1919

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-
];
2820

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
4360
}
4461

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];
8879
}
89-
);
80+
}
81+
result.docs.push(doc);
82+
}
9083

91-
}
84+
callback(err, result);
85+
}
86+
);
9287

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+
}
13489

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+
};
13696

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+
});
137131

138-
return {
139-
search: _search,
140-
getMovieGroupBy: _getMovieGroupBy,
141-
};
132+
}
142133

134+
return {
135+
search: _search,
136+
getMovieGroupBy: _getMovieGroupBy
137+
};
143138
}
144139

145140
module.exports = SearchService;
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
1-
const express = require('express')
2-
const cors = require('cors')
3-
const app = express()
1+
const express = require('express');
2+
const cors = require('cors');
3+
const app = express();
44
const serverPort = process.env.SERVER_PORT || 8086;
55

66

77

88
const SearchService = require('./NodeSearchService');
9-
const searchService = new SearchService()
9+
const searchService = new SearchService();
1010

11-
app.use(cors())
11+
app.use(cors());
1212

1313

1414
app.get('/api/1.0/movies/search', (req, res) => {
15-
let queryString = req.query["q"];
16-
let offset = Number((req.query["offset"])?req.query["offset"]:"0");
17-
let limit = Number((req.query["limit"])?req.query["limit"]:"10");
18-
let sortBy = req.query["sortby"];
19-
let ascending = req.query["ascending"];
20-
21-
let options = {
22-
offset : offset,
23-
limit : limit
24-
}
15+
const queryString = req.query.q;
16+
const offset = Number((req.query.offset)?req.query.offset:'0');
17+
const limit = Number((req.query.limit)?req.query.limit:'10');
18+
const sortBy = req.query.sortby;
19+
const ascending = req.query.ascending;
20+
21+
const options = {
22+
offset,
23+
limit
24+
};
2525

2626
if (sortBy) {
2727
options.sortBy = sortBy;
28-
options.ascending = true; // if sorted by default it is asceending
28+
options.ascending = true; // if sorted by default it is ascending
2929
}
3030

3131
if (ascending) {
32-
options.ascending = (ascending==1 || ascending.toLocaleLowerCase()==="true");
32+
options.ascending = (ascending==1 || ascending.toLocaleLowerCase()==='true');
3333
}
3434

3535
searchService.search(
36-
queryString, // query string
37-
options, // options
38-
function(err, result){ // callbacl
39-
res.json(result);
40-
});
36+
queryString, // query string
37+
options, // options
38+
function(err, result){ // callback
39+
res.json(result);
40+
}
41+
);
4142
})
4243

4344
app.get('/api/1.0/movies/group_by/:field', (req, res) =>{
44-
searchService.getMovieGroupBy(req.params['field'], function(err, result){
45+
searchService.getMovieGroupBy(req.params.field, function(err, result){
4546
res.json(result);
4647
});
47-
})
48+
});
4849

4950
app.get('/api/1.0/', (req, res) => {
50-
res.json({"status" : "started"});
51-
})
51+
res.json({status: 'started'});
52+
});
5253

5354

5455
app.get('/', (req, res) => {
55-
res.send('RediSearch Node REST Server Started')
56-
})
56+
res.send('RediSearch Node REST Server Started');
57+
});
5758

5859
app.listen(serverPort, () => {
59-
console.log(`RediSearch Node listening at http://localhost:${serverPort}`)
60-
})
60+
console.log(`RediSearch Node listening at http://localhost:${serverPort}`);
61+
});

0 commit comments

Comments
 (0)