How do I intersect multiple secondary index queries

↘锁芯ラ 提交于 2020-01-16 00:48:10

问题


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?


回答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.




回答2:


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)



来源:https://stackoverflow.com/questions/19656785/how-do-i-intersect-multiple-secondary-index-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!