Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - sort multi-index query

I'm building an autocomplete search with elasticsearch so I need to query 3 indexes posts, comments, authors. I have the following query:

{
   "query":{
      "query_string":{
         "query":"something"
      }
   }
}

the call:

curl -X GET 'http://localhost:9200/posts,comments,authors/_search?pretty' -d '{
  "query": {
    "query_string": {
      "query": "something"
    }
  }
}'

I need to sort the results by specific index fields, for example:

posts index has a field called comments_count, comments votes_count and authors posts_count. When comparing posts, then it should sort by comments_count, when comments then votes_count, when authors then posts_count.

It's possible to do something like that? I wouldn't like to merge the indexes into one because they indexes completely different documents.

like image 775
Marcus Mansur Avatar asked Jun 01 '13 05:06

Marcus Mansur


1 Answers

Well, my problem was that if i sort by a field that is not present in all indexes, documents wont be returned.

Managed to solve with:

{
   "_script":{
      "script":"type=doc['_type'].value;if(type=='posts'){return doc['comments_count'].value} else {return 0};",
      "type":"number",
      "order":"desc"
   }
}

at least now the documents are shown, even if at the bottom.

like image 99
Marcus Mansur Avatar answered Oct 12 '22 12:10

Marcus Mansur