1

If I have a table with multiple secondary indexes, how do I generate an intersection? For example, if I have a users table with secondary indexes on "firstName" and "lastName", and want all users named 'Bob Smith':

r.table('users').getAll('Bob', {index: 'firstName'}).XXXX('Smith', {index: 'lastName'})

I can use a filter, but my understanding is that would be slower:

r.table('users').getAll('Bob', {index: 'firstName'}).filter({'lastName': 'Smith'})

Alternatively, can I do intersections with a compound index?

bjnortier
  • 2,008
  • 18
  • 18

2 Answers2

1

The correct way to do this is with a compound index. For example:

table.index_create("fullName", lambda doc: [doc["firstName"], doc["lastName"])
table.getAll(["Bob", "Smith"])

You can't currently use more than one index in a single query. It's something that Rethink might support in the future but right now it doesn't.

Joe Doliner
  • 2,058
  • 2
  • 15
  • 19
0

The correct updated way to do this is creating a compound secondary index based on the first_name and last_name attributes. Example updated in JS: r.table("users").indexCreate( "full_name", [r.row("last_name"), r.row("first_name")] ).run(conn, callback)

Then your query will be something like Get all users whose last name is Smith and the first name name is John. r.table("users").getAll(["Smith", "John"], {index: "full_name"}).run(conn, callback)

Thomas Modeneis
  • 687
  • 8
  • 11