13

I'm currently building an app for deployment to GAE, using Objectify 3.1. I am getting strange results when attempting to do a query with an order() clause.

My domain:


public class InvoiceLineItem
{
    private int units;

    private BigDecimal unitCost;
    private BigDecimal extendedCost;

    private String description;

    @Parent Key<Invoice> invoice;
}

I am attempting to gather all of the InvoiceLineItems associated with a given Invoice using the following:

ofy ().query (InvoiceLineItem.class).ancestor (invoiceKey).list ( );

In my test case, this works just fine, returning 2 rows as expected.

However, when I try to add a sort order to the above query, like so:

ofy ().query (InvoiceLineItem.class).ancestor (invoiceKey).order ("+description").list ();

I always get 0 results. I've tried changing the order direction, the field its ordering by, the location of the order () clause in the query, all to no effect. Can anyone see anything that I'm doing wrong here?

Thanks...

Jaroslav Záruba
  • 4,694
  • 5
  • 39
  • 58
Steve
  • 2,678
  • 8
  • 40
  • 54

1 Answers1

28

There are a couple potential issues here:

  • The description field must be indexed
  • The description field must be less than 500 chars, because over 500 chars gets converted to a Text which is not indexable
  • Get rid of the +. It's either .order("description") or .order("-description").
stickfigure
  • 13,458
  • 5
  • 34
  • 50
  • It turns out that its the '+'. I thought I had read that you could use '+' for ascending or '-' for descending. It seems that only the minus is necessary. Thanks for the tip. – Steve May 07 '12 at 20:30
  • 2
    An interesting aside, though. I was under the impression that in Objectify 3.1 all fields were indexed, unless annotated with `@`Unindexed. Version 4.0 changes this behavior to all fields are unindexed unless annotated with `@`Indexed. Am I incorrect with this impression? – Steve May 07 '12 at 20:33
  • 2
    Yes - you are correct. In 3.1, all fields are indexed by default. I wasn't sure if the pasted code was exact so it seemed wise to mention the index issue, which is a common mistake. Glad it's working now! – stickfigure May 08 '12 at 01:52