Getting an array inside a document sorted on the basis of some precondition


(azitabh) #1

Let's say this is the document indexed in elasticsearch:
{:id=>123,:a=>10, :b=>12,
:c=>[{:x=>A,:y=>102,:z=>92},{:x=>B,:y=>110,:z=>96},{:x=>C,:y=>94,:z=>106}]}

And I am firing some query to elasticsearch and it's guaranteed that this
will be the only document in the result set.
Is there a way to instruct elasticsearch that the array 'c' in the returned
document should be sorted on the basis of (y+3z).

Since the expression will be different every time, so I can't take care of
this while indexing.

Thanks
-Azitabh

--


(Radu Gheorghe) #2

Hello Azitabh,

If what you want is to sort the elements of the array within each
document, then I'm not sure how you can do it other than within your
application. I don't know of any way for changing the content of
results.

If you want to change the order of documents you get when you search,
based on the sum of the mentioned values, you might use a Custom Score
Query[0] or Script-based Sorting[2]. Something like this:

Although it would be slow for more results, because the script is
based on _source. I couldn't get it working with "doc" :frowning:

[0] http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html
[1] http://www.elasticsearch.org/guide/reference/api/search/sort.html

Best regards,
Radu

http://sematext.com/ -- ElasticSearch -- Solr -- Lucene

On Wed, Nov 7, 2012 at 8:36 AM, azi azitabh@gmail.com wrote:

Let's say this is the document indexed in elasticsearch:
{:id=>123,:a=>10, :b=>12,
:c=>[{:x=>A,:y=>102,:z=>92},{:x=>B,:y=>110,:z=>96},{:x=>C,:y=>94,:z=>106}]}

And I am firing some query to elasticsearch and it's guaranteed that this
will be the only document in the result set.
Is there a way to instruct elasticsearch that the array 'c' in the returned
document should be sorted on the basis of (y+3z).

Since the expression will be different every time, so I can't take care of
this while indexing.

Thanks
-Azitabh

--

--


(Igor Motov) #3

If this is the only document and its source is stored, you can use
script_field to find and extract the element with the highest score.
Something like this:

{
"query": {
"term" : {
"id": "123"
}
},
"script_fields": {
"top_c": {
"script": "c=_source["c"]; top_c=null;
top_score=java.lang.Integer.MIN_VALUE; for(cur : c) {score=cur["y"]+
3*cur["z"]; if(score > top_score){ top_c=cur; top_score=score}}; top_c"
}
}
}

On Wednesday, November 7, 2012 1:36:58 AM UTC-5, Azitabh Ajit wrote:

Let's say this is the document indexed in elasticsearch:
{:id=>123,:a=>10, :b=>12,
:c=>[{:x=>A,:y=>102,:z=>92},{:x=>B,:y=>110,:z=>96},{:x=>C,:y=>94,:z=>106}]}

And I am firing some query to elasticsearch and it's guaranteed that this
will be the only document in the result set.
Is there a way to instruct elasticsearch that the array 'c' in the
returned document should be sorted on the basis of (y+3z).

Since the expression will be different every time, so I can't take care of
this while indexing.

Thanks
-Azitabh

--


(system) #4