Script Based Sorting

959 views
Skip to first unread message

S.K. Asgarov

unread,
Mar 6, 2012, 6:07:23 AM3/6/12
to Elastica - Elasticsearch PHP Client
Hi, help me pls, how can i use Script Based Sorting (http://
www.elasticsearch.org/guide/reference/api/search/sort.html)?

This code

$sort = array(
"script" => "doc[tstamp].value * 2"
);

return me an earchPhaseExecutionException[Failed to execute phase
[query], total failure; Isn't it right?

ruflin

unread,
Mar 6, 2012, 6:54:02 AM3/6/12
to elastica-...@googlegroups.com
Please try

$sort = array(
 '_script' => array('script' ' ....'));

I have never tried it, but this should work.

S.K. Asgarov

unread,
Mar 6, 2012, 7:02:58 AM3/6/12
to Elastica - Elasticsearch PHP Client
$sort = array(
   '_script' => array('script' => 'doc[tstamp].value * 2')
);
$query->addSort($sort);

didn't work =( same exception

Nicolas Ruflin

unread,
Mar 6, 2012, 7:08:31 AM3/6/12
to elastica-...@googlegroups.com
Ok, I see two options for this.

1. You write a test
2. You do it the old way and post what the output for the following is:

print_r($query->toArray());

Then we should see if the structure matches the one from elasticsearch.
Message has been deleted

S.K. Asgarov

unread,
Mar 6, 2012, 8:34:01 AM3/6/12
to Elastica - Elasticsearch PHP Client
Here's print_r
array(2) {
["query"] => array(1) {
["term"] => array(1) {
[100000] => string(6) "100000"
}
}
["sort"] => array(1) {
[0] => array(1) {
["_script"] => array(1) {
["script"] => string(23) "doc['tstamp'].value * 2"
}
}
}
}


May be problem in '[0] => array(1) {'?
>  smime.p7s
> 4KViewDownload

ruflin

unread,
Mar 6, 2012, 8:42:41 AM3/6/12
to elastica-...@googlegroups.com
Ah, I see the issue. You are using addSort instead of addSort. Please try it again.

S.K. Asgarov

unread,
Mar 6, 2012, 8:59:43 AM3/6/12
to Elastica - Elasticsearch PHP Client
Oh, yes! It's works! Thank u very much, i love u!

Here's working example
$sort = array(
"_script" => array(
'script' => "doc['field_name'].value * 2",
'type' => 'number'),
);
$query->setSort($sort);

Nicolas Ruflin

unread,
Mar 6, 2012, 9:00:09 AM3/6/12
to elastica-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages