I was researching the same question and found the answer after some
tinkering around. You need a JS source function. You must write the JS
source code for the sort algorithm too.

JSSourceFunction phaseFunction = new JSSourceFunction(
        "function(v) {" +
                "return v.sort(function(a, b) {" +
                            "return a.field - b.field ;" +
                                "}" +
                         ");" +
             "}");
IndexQuery iq = new BinValueQuery(BinIndex.named("indx_name"),"bucket",
"value"); // get an index
MapReduceResult execute = client.mapReduce(iq).addMapPhase(new
NamedJSFunction("Riak.mapValuesJson"),
false).addReducePhase(phaseFunction).execute();
String resultRaw = execute.getResultRaw(); // Raw data
Collection<YourType> result = execute.getResult(YourType .class); //
serialized to a domain object

The result of this operation will depend on what you return in your
reduce() function. The result may be a YourType or YourType[] etc. I wanted
to mention that since Jackson can complain about serialization issues if
you return the wrong values.

Anyone know how to get this into a wiki ?

Thanks
Deepak Bala

On Wed, Aug 29, 2012 at 5:09 PM, Oved Machlev <[email protected]>wrote:

>  Hi,****
>
> ** **
>
> How can I use the sorting functions over 'real' fields in the stored
> objects? How can I set the field on which I want to sort by?****
>
> ** **
>
> It seems that the following refers to the key, in my case it's a UUID, and
> therefore useless to sort by:****
>
> ** **
>
>                 MapReduceResult result = riakClient.****
>
>                                 mapReduce("some_bucket").****
>
>                                 addMapPhase(*new* NamedJSFunction(
> "Riak.mapValuesJson"), *false*).****
>
>                                 *addReducePhase(new NamedErlangFunction(
> "riak_kv_mapreduce", "reduce_sort"), true).*****
>
>                                 execute();****
>
> ** **
>
> ** **
>
> thanks,****
>
> Oved.****
>
> _______________________________________________
> riak-users mailing list
> [email protected]
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>
>
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com

Reply via email to