# Perl, query, sorting

**URL:** <https://discuss.elastic.co/t/perl-query-sorting/14634>\
**Category:** Elasticsearch\
**Created:** [November 28, 2013, 3:46pm UTC](https://discuss.elastic.co/t/perl-query-sorting/14634 "2013-11-28T15:46:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_B\_Fischer](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@Oliver\_B\_Fischer](https://discuss.elastic.co/u/Oliver_B_Fischer)\
**Post date:** [November 28, 2013, 3:46pm UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/1 "2013-11-28T15:46:26Z")

</div>

Hi,

can someone help me to add a working sort clause to this search  
statement (Perl):

my $result = $es-\>search(fields =\> ['message'],  
size =\> $opt-\>{'size'},  
sort =\> [  
"event\_time"  
]);

How can I say: Sort by event\_time in descending order?

Bye,

Oliver

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/52976552.40008%40swe-blog.net](https://groups.google.com/d/msgid/elasticsearch/52976552.40008%40swe-blog.net).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 28, 2013, 4:12pm UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/2 "2013-11-28T16:12:00Z")

</div>

Hi Oliver

```
sort => [{ event_time => 'desc'}]

```

clint

On 28 November 2013 16:46, Oliver B. Fischer [mailsink@swe-blog.net](mailto:mailsink@swe-blog.net) wrote:

> Hi,
> 
> can someone help me to add a working sort clause to this search statement  
> (Perl):
> 
> my $result = $es-\>search(fields =\> ['message'],  
> size =\> $opt-\>{'size'},  
> sort =\> [  
> "event\_time"  
> ]);
> 
> How can I say: Sort by event\_time in descending order?
> 
> Bye,
> 
> Oliver
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> msgid/elasticsearch/52976552.40008%[40swe-blog.net](http://40swe-blog.net).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPt3XKSk5o-N1%3DJROSQV8%3DjSgx34MwOLP7MsRK9kpivqWPd2ig%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPt3XKSk5o-N1%3DJROSQV8%3DjSgx34MwOLP7MsRK9kpivqWPd2ig%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Oliver\_B\_Fischer](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@Oliver\_B\_Fischer](https://discuss.elastic.co/u/Oliver_B_Fischer)\
**Post date:** [November 28, 2013, 4:29pm UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/3 "2013-11-28T16:29:03Z")

</div>

```
my $result = $es->search(fields => ['message'],
	                     size => $opt->{'size'},
	                     sort => [
	                    	{event_time => 'desc'}
	                     ]);

```

results in

[Failed to parse source  
[{"size":5,"fields":"message","sort":[{"HASH(0x7fc31382d3a0)":{}}]}]]];  
nested: SearchParseException[[faststrack-2013.11.25][4]:  
from[-1],size[5]: Parse Failure [No mapping found for  
[HASH(0x7fc31382d3a0)] in order to sort on]]; }]'},'request' =\> {'body'  
=\> undef,'qs' =\> {'sort' =\> 'HASH(0x7fc31382d3a0)','fields' =\>  
'message','size' =\> 5},'method' =\> 'GET','serialize' =\> 'std','path' =\>  
'/\_search','ignore' =\> },'status\_code' =\> 400}

????

Am 28.11.13 17:12, schrieb Clinton Gormley:

> sort =\> [{ event\_time =\> 'desc'}]

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/52976F4F.5080500%40swe-blog.net](https://groups.google.com/d/msgid/elasticsearch/52976F4F.5080500%40swe-blog.net).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 29, 2013, 10:11am UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/4 "2013-11-29T10:11:41Z")

</div>

Ahhh, sorry - this is a change from the previous Perl API.

The search() method can take a sort parameter at the top level, which goes  
into the query string, or inside the body. The query-string version is  
somewhat limited, so the body version would look like this:

```
$es->search(
    fields => ['message'],
    size => $opt->{size},
    body => {
        sort => [{ event_time=>'desc'}]
    }
)

```

clint

On 28 November 2013 17:29, Oliver B. Fischer [mailsink@swe-blog.net](mailto:mailsink@swe-blog.net) wrote:

> ```
> my $result = $es->search(fields => ['message'],
> size => $opt->{'size'},
> sort => [
> {event_time => 'desc'}
> ]);
> 
> ```
> 
> results in
> 
> [Failed to parse source [{"size":5,"fields":"message",  
> "sort":[{"HASH(0x7fc31382d3a0)":{}}]}]]]; nested: SearchParseException[[faststrack-2013.11.25][4]:  
> from[-1],size[5]: Parse Failure [No mapping found for  
> [HASH(0x7fc31382d3a0)] in order to sort on]]; }]'},'request' =\> {'body' =\>  
> undef,'qs' =\> {'sort' =\> 'HASH(0x7fc31382d3a0)','fields' =\>  
> 'message','size' =\> 5},'method' =\> 'GET','serialize' =\> 'std','path' =\>  
> '/\_search','ignore' =\> },'status\_code' =\> 400}
> 
> ????
> 
> Am 28.11.13 17:12, schrieb Clinton Gormley:
> 
> sort =\> [{ event\_time =\> 'desc'}]
> 
> > 
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> msgid/elasticsearch/52976F4F.5080500%[40swe-blog.net](http://40swe-blog.net).
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPt3XKRHYPhZ%3DyYHRH1o6SYmZuvaCcpDSeCaK2RMHqNQ8Z1G1g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPt3XKRHYPhZ%3DyYHRH1o6SYmZuvaCcpDSeCaK2RMHqNQ8Z1G1g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Oliver\_B\_Fischer](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@Oliver\_B\_Fischer](https://discuss.elastic.co/u/Oliver_B_Fischer)\
**Post date:** [November 29, 2013, 3:10pm UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/5 "2013-11-29T15:10:21Z")

</div>

Thanks, now it works as I need it!

Am 29.11.13 11:11, schrieb Clinton Gormley:

> sort =\> [{ event\_time=\>'desc'}]

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5298AE5D.1020908%40swe-blog.net](https://groups.google.com/d/msgid/elasticsearch/5298AE5D.1020908%40swe-blog.net).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:04am UTC](https://discuss.elastic.co/t/perl-query-sorting/14634/6 "2017-07-06T02:04:10Z")

</div>


