Skip to content

Secondary indexes doc has outdated information #731

Closed

Description

@coffeemug

http://rethinkdb.com/docs/secondary-indexes says "Currently, compound indexes cannot be queried by a prefix. See Github issue rethinkdb/rethinkdb#955 to track progress."

This is no longer true. We've implemented r.minval/r.maxval, as well as openness and closeness in between. The doc should be updated (see rethinkdb/rethinkdb#955 for how users can use between to query compound indexes by prefix).

/cc @chipotle

Activity

  1. stuartpb commented on Apr 24, 2015

    @stuartpb

    Also, r.minval and r.maxval should probably be listed somewhere in the main API documentation - right now they're only tucked away in the long-form docs for between, IIRC.

  2. coffeemug commented on Apr 24, 2015

    @coffeemug
    ContributorAuthor

    r.minval/maxval also don't show up in the data explorer suggestions (since they aren't documented as separate terms). I agree that we should document them as part of this issue.

  3. stuartpb commented on Apr 24, 2015

    @stuartpb

    Are r.minval and r.maxval still for use only within between, or can they be serialized? I was trying to figure out how I'd sink documents for a secondary index I skim the top of, and it seemed like a good case for using r.maxval or Infinity - or is it possible to just allow those documents to opt out of the index altogether with a value like null?

  4. coffeemug commented on Apr 24, 2015

    @coffeemug
    ContributorAuthor

    minval/maxval aren't serializable.

    I'm not entirely sure what you're trying to do. Could you post on https://groups.google.com/forum/#!forum/rethinkdb and we'll help figure it out?

  5. stuartpb commented on Apr 24, 2015

    @stuartpb

    I can post in more detail in the Google group, but what I'm trying to indicate, regarding the subject of this issue, is that it isn't clear from the current secondary index / getAll docs how to represent, query (or avoid querying), and/or join with missing or partial indices, or if such cases are even valid.

    (Edit: rethinkdb/rethinkdb#1032 makes the situation around missing / null value indices significantly clearer.)

    For instance, if I have a table where I have done

    r.table('posts').indexCreate("location", [r.row("articleId"), r.row("commentId")])

    And then I push a few documents, including a document with an articleId but no commentId (ie. because it refers to the "article" itself):

    {id:'foo', articleId: 'def678', commentId: 'mugugaipan'}
    {id:'baz', articleId: 'abc123', commentId: 'specificco'}
    {id:'jar', articleId: 'abc123'}
    

    and then I have a few more documents in another table ('watchers'), including one that refers to the no-comment object jar:

    {id:'foo2', foreignArticleId: 'def678', foreignCommentId: 'mugugaipan'}
    {id:'baz6', foreignArticleId: 'abc123', foreignCommentId: 'specificco'}
    {id:'baz19', foreignArticleId: 'abc123', foreignCommentId: 'specificco'}
    {id:'jar7', foreignArticleId: 'abc123'}
    

    And then I run a join on this compound index:

    r.table('watchers').join([r.row('foreignArticleId'),r.row('foreignCommentId')],'posts',{index:'location'})

    for jar7, will this join just join jar, or will it join jar and baz?

  6. stuartpb commented on Apr 24, 2015

    @stuartpb

    It's especially confusing regarding the relationship between rethinkdb/rethinkdb#955 and rethinkdb/rethinkdb#1032, which is also listed as a "limitation" that seems like it gives desirable behavior. (And what does not indexing null values mean in the context of compound indices?)

  7. stuartpb commented on Apr 24, 2015

    @stuartpb

    Regarding my question about omitting documents from a secondary index, using null is/was a way to do that, but there's movement toward making null a discrete value (with a place in sorting) for secondary indices - rethinkdb/rethinkdb#1032 (comment) seems to imply the way forward for excluding docs from a secondary index is to use a complex expression that will generate an error for these cases (which strikes me as a little weird, if for no other reason than that when I think "error", I think "anomalous operation that gets flagged and logged and might halt the caller", not "normal result value").

  8. stuartpb commented on Apr 24, 2015

    @stuartpb

    Also, while we're on the subject of issues with the sindex documentation, rethinkdb/rethinkdb#2773 should be mentioned in the same breath as rethinkdb/rethinkdb#1032 for the paragraph on how secondary indices can't be null or objects.

  9. danielmewes commented on Apr 24, 2015

    @danielmewes
    Member

    @stuartpb

    And then I run a join on this compound index:

    r.table('watchers').join([r.row('foreignArticleId'),r.row('foreignCommentId')],'posts',{index:'location'})

    for jar7, will this join just join jar, or will it join jar and baz?

    It will join nothing for jar7 because the join function errors on that document and so it cannot determine its join key.
    For a similar reason, jar can actually never be joined with anything because it will not appear in the secondary index since the secondary index function cannot be evaluated on it. You have to use default in the secondary index function if you want to handle missing fields and index those documents in a different way instead.

  10. danielmewes commented on Apr 24, 2015

    @danielmewes
    Member

    @stuartpb For clarification: There's nothing special about compound indexes in RethinkDB. They are simply indexes with arrays as keys. You can also have a "mixed" index where some values are indexed under arrays and others are not.

  11. stuartpb commented on Apr 24, 2015

    @stuartpb

    What if the code were changed to

    r.table('posts').indexCreate("location", [r.row("articleId").default(null), r.row("commentId").default(null)])

    and

    r.table('watchers').join([r.row('foreignArticleId').default(null),r.row('foreignCommentId').default(null)],'posts',{index:'location'})

    Would that work now, or does rethinkdb/rethinkdb#1032 need to be implemented? What about if the defaults were '' instead of null? What about r.branch(r.row.hasFields('commentId'),[r.row("articleId"), r.row("commentId")], [r.row("articleId")]) and its counterpart?

  12. danielmewes commented on Apr 24, 2015

    @danielmewes
    Member

    @stuartpb you can deduce the behavior of the query under those functions as follows:

    1. The document jar in the posts table will now be indexed under this value:
      ['abc123', null]
    2. During the join, the document jar7 from the watchers table will lead to a lookup of the key ['abc123', null]. eq_join does an equality lookup, so it will match exactly the document jar and none of the others, since ['abc123', null] = ['abc123', null] but not for example ['abc123', null] = ['abc123', specificco]. There really is no magic to this. Arrays are equal if they are the same length and all of their elements are equal. null is just a value like any other, and since it's inside of an array here it isn't affected by Disallow getting changes on a filtered limit #1032. Incidentally, that is why we want to fix Disallow getting changes on a filtered limit #1032, since secondary indexes currently break that rule by making null special which is bad and confusing.
  13. stuartpb commented on Apr 24, 2015

    @stuartpb

    This error behavior is something I feel the docs kind of gloss over right now: the JavaScript (and Lua) programmer in me translates [r.row('foo'), r.row('bar')] to [row.foo, row.bar] which would evaluate to something like ['value', undefined], which has equivalent access semantics to ['value'] (although the length of the former, in JS, is still 2). So what I intuitively "expect" is somewhat muddled, but the idea that getting a missing field will result in an error, which causes the expression to fail and (silently) omit the row from the index (as we're currently discussing in rethinkdb/rethinkdb#1032), is not what I would predict (for all I would guess, these errors would get triggered on insertion and get forwarded back to my client as a failed transaction).

    I'd say this error behavior should be described up-front in the docs on secondary indices, ie. with a mention in the examples for complex (expression-based) indexes, and then a whole section describing how errors get handled in index creation (with links to issues like rethinkdb/rethinkdb#4118) and how that may or may not be what you want (link to rethinkdb/rethinkdb#4121).

  14. stuartpb commented on Apr 24, 2015

    @stuartpb

    On the subject of compound indexes: are you saying there's nothing special about compound indexes, in terms of how they're implemented and what perf characteristics they have, versus any other ReQL expression (except that more complex ReQL expressions may take longer to evaluate by virtue of being more complex)?

    If so, I'd say they shouldn't have their own heading in the secondary index docs - at least, not where they are right now, where they come between simple indexes and multi indexes (and separate from the section on expressions). They should be listed as an example of "a common case of defining an index by an expression", possibly as the lead-in into the section on more complicated expressions.

    The notion of array equivalence (when an index is not defined as multi) should probably be raised as an example within the section on simple indexes, as this is another feature that can be counter-intuitive, if starting from a language-semantics (JavaScript) mindset (where ['a','b','c'] != ['a','b','c']). I think this would work well near the end of the section, acting as a lead-in to the contrasting multi behavior (so, kind of like it is now, except using a field with array values rather than a ReQL expression, so as to provide a gentler learning curve for the later section on expression-based indexes).

    Also, from what I've gleaned from the docs and this discussion, I can't answer the question of which index expression I'd be better off using, the default-null-for-missing-fields version or the branch-to-use-different-length-arrays version. Is one going to have significantly better perf characteristics?

  15. danielmewes commented on Apr 24, 2015

    @danielmewes
    Member

    I'd say this error behavior should be described up-front in the docs on secondary indices, ie. with a mention in the examples for complex (expression-based) indexes,

    That's a great idea. (@chipotle)

    I think we should still mention compound indexes explicitly. Most people think of them as a separate use case, and in most other databases they are completely separate index types. It so happens that RethinkDB's regular indexes are powerful enough to cover this use case too, but we should still explain exactly how to accomplish it.

    It sounds like a good idea to point out that compound indexes in RethinkDB are merely indexes over an array composed of multiple fields, and are hence just a special instance of regular indexes. We should do that (@chipotle).

    I can't answer the question of which index expression I'd be better off using, the default-null-for-missing-fields version or the branch-to-use-different-length-arrays version. Is one going to have significantly better perf characteristics?

    Their performance will be similar, but they'll sort differently. An array [X] will sort below [X, y] for any y, but [X, null] will sort equal to [X, null] and below and/or above some other values of y in [X, y].
    Just opened #734 for documenting that part.

  16. chipotle commented on Apr 30, 2015

    @chipotle
    Contributor

    I'd say this error behavior should be described up-front in the docs on secondary indices, ie. with a mention in the examples for complex (expression-based) indexes

    I've been digging through this and I confess I've gotten a bit lost. What error behavior are we talking about documenting? The suggestion to use r.error as the official way of deliberately omitting documents from secondary indexes?

  17. stuartpb commented on Apr 30, 2015

    @stuartpb

    The fact that errors cause documents to not be indexed, and do not get reported or fail the query.
    On Apr 30, 2015 11:30 AM, Watts [email protected] wrote:
    I'd say this error behavior should be described up-front in the docs on secondary indices, ie. with a mention in the examples for complex (expression-based) indexes

    I've been digging through this and I confess I've gotten a bit lost. What error behavior are we talking about documenting? The suggestion to use r.error as the official way of deliberately omitting documents from secondary indexes?

    —Reply to this email directly or view it on GitHub.

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