Skip to content

Clarify multi index semantics w.r.t. duplicates #580

Closed

Description

@danielmewes

@nviennot noted rethinkdb/rethinkdb#3345 that our docs currently do not explain the semantics of multi indexes with respect to duplicate results.

Queries on multi indexes through getAll or between can generally produce the same result multiple times. Here's a simple example:

r.tableCreate('t1')
r.table('t1').indexCreate('a', {multi: true})
r.table('t1').insert({a: [1, 2, 1]})
r.table('t1').getAll(1, {index: "a"})
// Returns the document twice

The one exception are geo multi indexes, which can be queried through getIntersecting and getNearest. Those operations never return duplicates.

@nviennot further noticed that this example in http://www.rethinkdb.com/docs/secondary-indexes/ruby/ requires a distinct() in order to make the mentioned queries really equivalent:

# Create the index
r.table("users").index_create("user_equipment", {:multi => true}) { |user|
    user['equipment'].map { |equipment| [user['id'], equipment] }
}.run(conn)

# Query equivalent to:
# r.table("users").get(1).filter { |user|
#     user['equipment'].contains('tent')
# }.run(conn)
r.table("users").get_all([1, "tent"], {:index =>"user_equipment"}).run(conn)

Activity

  1. nviennot commented on Nov 17, 2014

    @nviennot
    Contributor

    also get(1) should be get_all(1) right?

  2. danielmewes commented on Nov 17, 2014

    @danielmewes
    MemberAuthor

    Oh yeah, it has to be get_all(1) indeed or the filter won't work on it.

  3. mlucy commented on Nov 18, 2014

    @mlucy
    Member

    @danielmewes: it seems confusing to me that geo multi-indexes remove duplicates when normal multi-indexes don't. Are we sure we want to do it that way?

  4. nviennot commented on Nov 18, 2014

    @nviennot
    Contributor

    I find the introduced doc fix confusing.

    Simple and compound indexes are unique: values (or combinations of values, for compound indexes) can only appear once, and a single document can have only one index. A multi index can index a single document multiple times with different values. For instance, a blog post might have multiple tags, and each tag might refer to multiple blog posts.

    If simple and compound indexes are unique, does that mean that arbitrary expressions indexes are not?
    What does it mean for a value to appear only once. Does that mean that if I have two documents with the same indexed value is a problem?
    The following is confusing in two ways: "A single document can only have one index" 1) documents don't have indexes 2) does it mean that if a document belong to an simple index, it cannot belong at the same time to a compound index?

    Also, the beginning of the page says:

    RethinkDB supports different types of secondary indexes:

    • Simple indexes based on the value of a single field.
    • Compound indexes based on multiple fields.
    • Multi indexes based on arrays of values.
    • Indexes based on arbitrary expressions.

    This is confusing, because "multi" in an attribute that can be applied to any of the 3 possible types of indexes (simple, compound, arbitrary). However, the list suggests that multi indexes are a different index type.

    To me, the multi operator on an index is like having r.args() applied to the value to be indexed.


    Also, why bother with the different index types, since the simple and compound indexes can be just special cases of the arbitrary expression type?

  5. chipotle commented on Nov 18, 2014

    @chipotle
    Contributor

    We're still working on the phrasing of that one paragraph, actually; my current take is

    With simple and compound indexes, a single document can be indexed by only one index value (or one specific set of values, for compound indexes). A multi index can index a single document multiple times with different values. For instance, a blog post might have multiple tags, and each tag might refer to multiple blog posts.

    Which might address some of your concerns. @danielmewes pointed out that it's probably best to avoid "unique" because they're not unique in the "unique constraints" sense; it doesn't say that documents have indexes; etc.

    As for the way multi indexes are kind of "meta" and thus not like the others, while that might be a discussion worth having, we use the phrase multi index a lot throughout the documentation and to date, at least, I haven't gotten reports of users being too confused about this in practice -- the examples and more complete discussion past the introduction here (hopefully) make this fairly clear. Taking that bullet point out and finding a way to say "in addition, there's another thing we call an index which isn't technically an index but changes the way other indexes work, although in practice you'll use it just like an index" will be more accurate, but I think it'll be very hard to do that in a way which actually improves clarity -- the introduction block is supposed to be a pretty high-level overview. Maybe it's something we can make more explicit in the more detailed section on multi indexes here.

  6. danielmewes commented on Nov 18, 2014

    @danielmewes
    MemberAuthor

    @mlucy Geo indexes generally have to filter duplicates, because even in a regular index a single document can appear in multiple places in the index tree.
    I think we could theoretically make geo indexes emit duplicates in a multi index. This would work by comparing the tags that we generate for multi index index keys (specifically we could filter duplicates in the geo index based not just on their primary keys, but on the pair (primary key, multi index tag) ). It would require some additional code specifically for this in the geospatial index traversal code.
    I don't think it's too confusing to have getIntersecting() and getNearest() never return duplicates, even if between() and getAll() do for multi indexes. But that's up for discussion. I opened rethinkdb/rethinkdb#3351 to discuss that aspect further.

  7. added a commit that references this issue on Nov 21, 2014
    b0f34ef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions