loopbackio / loopback-connector-redis

EXPERIMENTAL: Redis connector for LoopBack.
http://loopback.io/doc/en/lb2/Redis-connector.html
Other
36 stars 46 forks source link

No indexes found for <fieldname> impossible to sort and filter using redis connector #20

Open pierreclr opened 8 years ago

pierreclr commented 8 years ago

Hi,

I discovered loopback few weeks ago and I find it amazing ! However I need your help to solve my problem on redis connector: The 'where' filter does not work despite indexing the fields I want to query on .. indeed, when I'm trying to call model.find({where: {field: true}, limit: 10}, function ...) it does not consider my query at all and give me an array of the ten first model insertions ...

However, the same query in the loopback explorer is working well ... even if i'm just a JavaScript beginner I think there's something wrong in the nodeAPI redis adapter

Could someone light my mind about how using redis-adapter to avoid problem ? I know that this project is under development and I accept any thug solution ;)

Thank you to strongloop / loopback team and all contributors for this awesome product !

alexbeer2048 commented 8 years ago

I'm getting a similar error when using the destroyAll method.

Code snippet:

var ONE_HOUR = 1000 * 60 * 60;
var oneHourAgo = new Date(Date.now() - ONE_HOUR);
ServiceUpdateStats.destroyAll({
    or: [
        { lastRead: { lt: oneHourAgo } },
        { lastRead: null }
    ]
} , function (err, obj) {
    if (err) {
        console.log(err);
        return;
    }
    var destroyed = obj.count;
    if (destroyed) {
        console.log('ServiceUpdateStats: destroyed ' + destroyed + ' objects');
    }
});

This works fine with the mongodb connector (deletes entries with date field 'lastRead' set to date value older than an hour or empt).

When i chang the datasource for this model to loopback-connector-redis i get this error:

/path/to/project/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^ Error: ServiceUpdateStats: no indexes found for or impossible to sort and filter using redis connector at BridgeToRedis.all (/path/to/project/node_modules/loopback-connector-redis/lib/redis.js:504:19) at BridgeToRedis.destroyAll (/path/to/project/node_modules/loopback-connector-redis/lib/redis.js:620:14) at doDelete (/path/to/project/node_modules/loopback-datasource-juggler/lib/dao.js:1926:19) at /path/to/project/node_modules/loopback-datasource-juggler/lib/dao.js:1900:11 at doNotify (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:98:49) at doNotify (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:98:49) at doNotify (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:98:49) at doNotify (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:98:49) at Function.ObserverMixin._notifyBaseObservers (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:121:5) at Function.ObserverMixin.notifyObserversOf (/path/to/project/node_modules/loopback-datasource-juggler/lib/observer.js:96:8) npm ERR! Linux 4.4.0-0.bpo.1-amd64 npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "start" npm ERR! node v4.4.3 npm ERR! npm v3.8.7 npm ERR! code ELIFECYCLE

I wonder if there are restrictions using filters with this connctor or is it a bug that can be fixed.

Edit: Redis server v=3.0.7 running on debian

pierreclr commented 8 years ago

Is your "lastRead" field indexed in your redis database ? (Or in your JSON model definition) Built in loopback Redis ORM could not sort or more globally query on field which is not indexed ...

alexbeer2048 commented 8 years ago

At my first attempt I didn't set it indexed, but I also tried setting ALL the properties index parameters to true as well and it did not make a difference. Same error.

model JSON:

...
    "lastRead": {
      "type": "date",
      "required": true,
      "index": true
    },
...

Tbh, I don't know if i can set the index parameter in the redis db directly (I know little about redis yet), or if it would make a difference to setting it in the loopback model files. I'm hoping the connector would do that form me.

pierreclr commented 8 years ago

Since the error is coming from the redis database itself I imagine you can set index in the redis db directly. However, you can also define it by adding "id : true" in your model.json files for the fields you need to query on !

alexbeer2048 commented 8 years ago

I found out by reviewing the code that 'and' or 'or' operators are not supported in where queries yet. Its right there in the error, but not so obvious:

... no indexes found for OR impossible to sort and filter ...

The error is shown because there is no indexed field 'or' on my model. But of course I meant 'or' as an operator, not as a field.

I could sort my problem for most cases by simplifying the query bit and adding some extra code to process the result.

Did you mean "index: true"? I set it for all fields used in queries and it works.

I made an attempt to rewrite the code of the prototype.all() method (in lib/redis.js) to support those operators but it was not a bit of a hack and only worked properly for 'and' so i dropped it. But I will give it another try if I have some time to learn more about redis and some of the methods it provides.

penguinpowernz commented 8 years ago

Once you get to setting up auth you will have problems, as the common code in the loopback repo does not explicitly set index: true on the ACL models etc, I guess other connectors implicitly determine the indexes?

penguinpowernz commented 8 years ago

I created a PR on the loopback repos to fix this: strongloop/loopback#2368

superkhau commented 8 years ago

Waiting on strongloop/loopback#2368 to close this.

bajtos commented 7 years ago

Let's move the discussion about missing indexes in built-in Auth models to https://github.com/strongloop/loopback/issues/3011 and keep this issue focused on how to configure the Redis connector and/or the models to support the kind of queries described in the issue description.

WendySanarwanto commented 7 years ago

Hello All,

Any updates about this issue ?