Avanced sort expressions in Search feature (Java)

74 views
Skip to first unread message

Jordi P.S.

unread,
Sep 11, 2012, 12:54:24 PM9/11/12
to google-a...@googlegroups.com
Hello,

I have a question concerning the SortExpression:
I'd like to sort a list of documents in Ascending order. I'd like to avoid the 0 or negative values, which I'd put them at the end of the list. Is there a way to do it with a sort expression?

Example list:  1 0 4 5 2
Sorted list: 1 2 4 5 0 

Any idea?

I'm thinking in something like: sortexpression="MAX_DOUBLE+field_name"

Jordi.

Amy Unruh

unread,
Sep 12, 2012, 3:47:45 AM9/12/12
to google-a...@googlegroups.com
hi,

How about adding a query filter on that field?  E.g., say this numeric field is called 'ranking'.  Your query could include:
 ranking > 0
along with any other search terms. E.g. if you also want to look for the word 'books', your query would look like:
 books ranking > 0

Then, define the sort expression to order on that field in ascending order.


Jordi.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/AlCFVki-VKsJ.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Jordi Planadecursach Soler

unread,
Sep 12, 2012, 4:05:19 AM9/12/12
to google-a...@googlegroups.com
I need all the elements: the ones that ranking > 0 and the ones that ranking <= 0. 

As you say I could do 2 searchs the first with a query filter > 0 and the other with the query filter <= 0 and unify them in the application logic.

This would be a solution but, do you think there is a solution with only 1 search ?

Jason

unread,
Sep 12, 2012, 9:57:42 AM9/12/12
to google-a...@googlegroups.com
In order to do it in one query, you could add a field that is used specifically for sorting.  If the value of the normal field is positive, the sort field would be the same as the normal field.  If the value of the normal field is negative or zero, the sort field could be something like:  99999999 - normal field.  This would work as long as there's a known upper limit of the value of the normal positive field.

Amy Unruh

unread,
Sep 16, 2012, 11:57:14 PM9/16/12
to google-a...@googlegroups.com
On 12 September 2012 23:57, Jason <jas...@gmail.com> wrote:
In order to do it in one query, you could add a field that is used specifically for sorting.  If the value of the normal field is positive, the sort field would be the same as the normal field.  If the value of the normal field is negative or zero, the sort field could be something like:  99999999 - normal field.  This would work as long as there's a known upper limit of the value of the normal positive field.

Yes, that approach seems reasonable to me too if you want to do it in one query. [The sort expressions can currently draw on operators like max(), min(), *, +, /, and - ; if you can't build an expression that generates the value you want, then precomputing a sort field can be a good tactic].

However, unifying the two lists of results at the application level does not seem problematic, since it sounds like you essentially just need to concatenate them.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/_ZMYPzrhEvAJ.

Jordi Planadecursach Soler

unread,
Sep 17, 2012, 7:03:30 AM9/17/12
to google-a...@googlegroups.com
Hello,

Thanks for your answers. I'll take the precomputing solution as it is more flexible. I tried both in dev server and work correctly.

Let's see in prod.

Thanks,
Jordi.
Reply all
Reply to author
Forward
0 new messages