Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redisearch

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redisearch - npm Package Compare versions

Comparing version 0.0.11 to 1.0.0

97

lib/redis-search.js
'use strict';
var redis = require('redis');
var async = require('async');
var natural = require('natural');
var metaphone = natural.Metaphone.process;
var stem = natural.PorterStemmer.stem;
var stopwords = natural.stopwords;
var redisClient;
const redis = require('ioredis');
const natural = require('natural');
const metaphone = natural.Metaphone.process;
const stem = natural.PorterStemmer.stem;
const stopwords = natural.stopwords;
let redisClient;
function noop() {}
var commands = {
const commands = {
and: 'zinterstore',

@@ -30,19 +27,12 @@ or: 'zunionstore'

Search.prototype.index = function(data, id, callback) {
callback = callback || noop;
Search.prototype.index = async function(data, id) {
var namespace = this.namespace;
if (!namespace) {
return callback(new Error('index needs a namespace'));
throw new Error('index needs a namespace');
}
indexData(namespace, data, id, callback);
await indexData(namespace, data, id);
};
Search.prototype.query = function(data, start, stop, callback) {
if (typeof start === 'function') {
callback = start;
start = 0;
}
Search.prototype.query = async function (data, start, stop) {
var namespace = this.namespace;

@@ -79,3 +69,3 @@ start = start || 0;

if (!tmpKeys.length) {
return callback(null, []);
return [];
}

@@ -100,8 +90,8 @@

redisClient.multi(cmds).exec(function(err, ids) {
if (err) {
return callback(err);
}
callback(null, ids[resultIndex]);
});
const ids = await redisClient.multi(cmds).exec();
const errRes = ids[resultIndex];
if (errRes[0]) {
throw new Error(String(errRes[0]));
}
return errRes[1];
};

@@ -141,47 +131,28 @@

Search.prototype.remove = function(id, callback) {
callback = callback || noop;
Search.prototype.remove = async function(id) {
var namespace = this.namespace;
async.waterfall([
function(next) {
getIndices(namespace, id, next);
},
function(indices, next) {
async.each(indices, function(index, next) {
removeIndex(namespace, id, index, next);
}, next);
}
], function(err) {
if (err) {
return callback(err);
}
redisClient.del(namespace + ':id:' + id + ':indices', callback);
});
const indices = await getIndices(namespace, id);
await Promise.all(indices.map(index => removeIndex(namespace, id, index)));
await redisClient.del(namespace + ':id:' + id + ':indices');
return this;
};
function removeIndex(namespace, id, index, callback) {
redisClient.smembers(namespace + ':id:' + id + ':' + index, function(err, indexValues) {
if (err) {
return callback(err);
}
async function removeIndex(namespace, id, index) {
const indexValues = await redisClient.smembers(namespace + ':id:' + id + ':' + index);
var multi = redisClient.multi();
multi.del(namespace + ':id:' + id + ':' + index);
var multi = redisClient.multi();
multi.del(namespace + ':id:' + id + ':' + index);
indexValues.forEach(function(indexValue) {
multi.zrem(namespace + ':' + index + ':' + indexValue + ':id', id);
});
indexValues.forEach(function(indexValue) {
multi.zrem(namespace + ':' + index + ':' + indexValue + ':id', id);
});
multi.exec(callback);
});
return await multi.exec();
}
function getIndices(namespace, id, callback) {
redisClient.smembers(namespace + ':id:' + id + ':indices', callback);
async function getIndices(namespace, id) {
return await redisClient.smembers(namespace + ':id:' + id + ':indices');
}
function indexData(namespace, data, id, callback) {
async function indexData(namespace, data, id) {
var cmds = [];

@@ -193,3 +164,3 @@ for(var index in data) {

}
redisClient.multi(cmds).exec(callback);
return await redisClient.multi(cmds).exec();
}

@@ -196,0 +167,0 @@

@@ -5,3 +5,3 @@ {

"description": "A plugin to search data with redis",
"version": "0.0.11",
"version": "1.0.0",
"repository": {

@@ -16,8 +16,7 @@ "type": "git",

"dependencies": {
"redis": "^0.12.1",
"async": "^1.4.2",
"natural": "^0.2.0"
"ioredis": "^4.27.2",
"natural": "^5.0.3"
},
"devDependencies": {
"mocha": "~2.1.0"
"mocha": "~8.4.0"
},

@@ -24,0 +23,0 @@ "bugs": {

@@ -6,26 +6,15 @@

var redisSearch = require('../index');
var async = require('async');
var assert = require('assert');
const redisSearch = require('../index');
const assert = require('assert');
var postSearch = redisSearch.createSearch('mypostsearch');
const postSearch = redisSearch.createSearch('mypostsearch');
before(function(done) {
async.parallel([
function(next) {
postSearch.index({content: 'ruby emerald', uid: 5, cid: 1}, 1, next);
},
function(next) {
postSearch.index({content: 'emerald orange emerald', uid: 5, cid: 2}, 2, next);
},
function(next) {
postSearch.index({content: 'cucumber apple orange', uid: 4, cid: 2}, 3, next);
},
function(next) {
postSearch.index({content: 'ORANGE apple pear', uid: 5, cid: 4}, 4, next);
},
function(next) {
postSearch.index({content: 'dog cat', uid: 6, cid: 4}, 5, next);
}
], done);
before(async function() {
await Promise.all([
postSearch.index({content: 'ruby emerald', uid: 5, cid: 1}, 1),
postSearch.index({content: 'emerald orange emerald', uid: 5, cid: 2}, 2),
postSearch.index({content: 'cucumber apple orange', uid: 4, cid: 2}, 3),
postSearch.index({content: 'ORANGE apple pear', uid: 5, cid: 4}, 4),
postSearch.index({content: 'dog cat', uid: 6, cid: 4}, 5),
]);
});

@@ -35,112 +24,82 @@

describe('query', function() {
it('should find the correct ids', function(done) {
postSearch.query({content: 'apple orange', uid: 5}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 2);
assert.notEqual(ids.indexOf('2'), -1);
assert.notEqual(ids.indexOf('4'), -1);
done();
it('should find the correct ids', async function() {
const ids = await postSearch.query({
matchWords: 'any',
query: { content: 'apple orange', uid: 5 },
});
assert.strictEqual(ids.length, 2);
assert(ids.includes('2'));
assert(ids.includes('4'));
});
it('should return empty', function(done) {
postSearch.query({uid: 5, content: 'apple orange', cid: 123}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 0);
done();
});
it('should return empty', async function () {
const ids = await postSearch.query({ query: {uid: 5, content: 'apple orange', cid: 123 } });
assert.strictEqual(ids.length, 0);
});
it('should return 1', function(done) {
postSearch.query({uid: 5, content: 'emerald', cid: 1}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 1);
assert.equal(ids[0], 1);
done();
});
it('should return 1', async function () {
const ids = await postSearch.query({ query: { uid: 5, content: 'emerald', cid: 1 } } );
assert.strictEqual(ids.length, 1);
assert.strictEqual(ids[0], '1');
});
it('should return all the ids with uid=5', function(done) {
postSearch.query({uid: 5}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 3);
assert.notEqual(ids.indexOf('1'), -1);
assert.notEqual(ids.indexOf('2'), -1);
assert.notEqual(ids.indexOf('4'), -1);
done();
});
it('should return all the ids with uid=5', async function () {
const ids = await postSearch.query({ query: { uid: 5 }});
assert.strictEqual(ids.length, 3);
assert(ids.includes('1'));
assert(ids.includes('2'));
assert(ids.includes('4'));
});
it('should return return 4', function(done) {
postSearch.query({content: 'ruby pear', cid: 4}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 1);
assert.notEqual(ids.indexOf('4'), -1);
done();
it('should only return 4', async function () {
const ids = await postSearch.query({
matchWords: 'any',
query: { content: 'ruby pear', cid: 4 }
});
assert.strictEqual(ids.length, 1);
assert(ids.includes('4'));
});
it('should limit the results to 2', function(done) {
postSearch.query({uid: 5}, 0, 1, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 2);
done();
});
it('should limit the results to 2', async function () {
const ids = await postSearch.query({ query: { uid: 5 } }, 0, 1);
assert.strictEqual(ids.length, 2);
});
it('should return the correct ids when an array is passed in', function(done) {
postSearch.query({cid: [2, 4]}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 4);
assert.notEqual(ids.indexOf('2'), -1);
assert.notEqual(ids.indexOf('3'), -1);
assert.notEqual(ids.indexOf('4'), -1);
assert.notEqual(ids.indexOf('5'), -1);
done();
});
it('should return the correct ids when an array is passed in', async function () {
const ids = await postSearch.query({ query: { cid: [2, 4] } });
assert.strictEqual(ids.length, 4);
assert(ids.includes('2'));
assert(ids.includes('3'));
assert(ids.includes('4'));
assert(ids.includes('5'));
});
it('should return the correct ids when an array is passed in', function(done) {
postSearch.query({cid: [2, 4], content: 'orange', uid: 4}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 1);
assert.notEqual(ids.indexOf('3'), -1);
done();
it('should return the correct ids when an array is passed in', async function () {
const ids = await postSearch.query({
query: {
cid: [2, 4], content: 'orange', uid: 4
}
});
assert.strictEqual(ids.length, 1);
assert(ids.includes('3'));
});
it('should find the match when query is upper case', function(done) {
postSearch.query({content: 'CUCUMBER'}, function(err, ids) {
assert.ifError(err);
assert.equal(ids.length, 1);
assert.notEqual(ids.indexOf('3'), -1);
done();
});
})
it('should find the match when query is upper case', async function () {
const ids = await postSearch.query({ query: { content: 'CUCUMBER' } })
assert.strictEqual(ids.length, 1);
assert(ids.includes('3'));
});
});
after(function(done) {
async.parallel([
function(next) {
postSearch.remove(1, next);
},
function(next) {
postSearch.remove(2, next);
},
function(next) {
postSearch.remove(3, next);
},
function(next) {
postSearch.remove(4, next);
},
function(next) {
postSearch.remove(5, next);
}
], done);
after(async function () {
await Promise.all([
postSearch.remove(1),
postSearch.remove(2),
postSearch.remove(3),
postSearch.remove(4),
postSearch.remove(5),
]);
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc