Ismael
Ismael

Reputation: 16720

How to use secondary indexes for a "contains" query

Rethinkdb docs has this example to improve getAll/contains queries with a secondary index:

// Create the index
r.table("users").indexCreate("userEquipment", function(user) {
    return user("equipment").map(function(equipment) {
        return [ user("id"), equipment ];
    });
}, {multi: true}).run(conn, callback);

// Query equivalent to:
// r.table("users").getAll(1).filter(function (user) {
//     return user("equipment").contains("tent");
// });
r.table("users").getAll([1, "tent"], {index: "userEquipment"}).distinct().run(conn, callback);

My questions is if there's a way to do the same but for querying with multiple tags. What would be the equivalent to make this query possible with a secondary index?

r.table("users").getAll(1).filter(function (user) {
    return user("equipment").contains("tent", "tent2");
});

Upvotes: 1

Views: 248

Answers (1)

kureikain
kureikain

Reputation: 2314

Probably we can do this

r.table("users").getAll([1, "tent"]).filter(function (user) {
    return user("equipment").contains("tent2");
});

So build a multi index as you did, and try to getAll first, so that part is efficient with index, then filter to continue ensure that equipment contains array we want.

Upvotes: 1

Related Questions