Skip to content

multi index semantics #3345

Closed
Closed

Description

@nviennot

Querying a multi index does not return distinct documents. Is this behavior expected or undefined?
Can the documentation specify the behavior?

How to reproduce:

#!/usr/bin/env ruby
require 'rethinkdb'
include RethinkDB::Shortcuts

r.connect(:host => 'localhost', :port => 28015, :db => :test).repl
r.db_drop(:test).run rescue nil
r.db_create(:test).run
r.table_create(:table).run

r.table(:table).index_create(:field1, :multi => true).run
r.table(:table).insert(:field1 => [1,2,3]).run # insert 1 document
r.table(:table).get_all(1,2,3, :index => :field1).run.to_a # retrieve 3 documents

Activity

  1. nviennot commented on Nov 17, 2014

    @nviennot
    ContributorAuthor

    If RethinkDB does not apply distinct by itself, the documentation in http://www.rethinkdb.com/docs/secondary-indexes/ruby/ is misleading/wrong:

    Use a multi index and a mapping function to speed get_all/contains
    If your program frequently executes a get_all followed by a contains, that operation can be made more efficient by creating a compound multi index using a mapping function on the field that contains the list.

    # 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)
    1. get(1) should be get_all(1) (otherwise the filter coming after makes no sense)
    2. Assuming get(1) is fixed with get_all(1), if the equipment list has duplicated element, the two queries are not equivalent, unless we add a distinct term on the sindexed query.
  2. danielmewes commented on Nov 17, 2014

    @danielmewes
    Member

    Thanks for letting us know.

    As you've noticed, a distinct is not automatically applied behind a getAll or between using a multi index. Doing so would change the memory complexity of such queries, because we would have to keep track of already seen documents (it doesn't matter if you retrieve something like 3 documents, but imagine you do a between and a million documents match the range).

    There is one exception, which is querying multi geo indexes through getIntersecting. getIntersecting never returns duplicates (this is possible because the number of results it can produce is limited by the array size limit).

    We will clarify this in the documentation.

  3. danielmewes commented on Nov 17, 2014

    @danielmewes
    Member

    I created rethinkdb/docs#580 for updating the docs. Marking this as moved.

  4. added this to the moved milestone on Nov 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions