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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With