Elasticsearch Subquery Scoring Optimization

If you want to build a search query in Easticsearch where you can give documents a bonus score depending on how often a property can be found in other documents

- you need is a Subquery which is not supported by Elasticsearch but can be programmed with any Programming Language such as PHP.

An Example for a Subquery is the problem:

Imagine a CD online shop. You want to score CDs ( = documents) higher which

1. match a term query AND

2. and which artist has many other CDs in your shop database

In this case you could use a subquery function like this:

 * @see
 * @param string $index
 * @param string $type
 * @param string $query
 * @return array CDs
public function popularArtistsSubquery($index, $type, $query){
    $response = $this->moreLikeThisQuerySearch($index, $type, $query);
    $response = json_decode($response);
    $hits = array();
    if($response->hits->total > 0){
        foreach($response->hits->hits as $hit){
            $responseMatch = $this->matchQuerySearch($index, $type,$hit->_source->artist);
            $responseMatch = json_decode($responseMatch);
            $artistCount = $responseMatch->hits->total;
            $hit->_score += $artistCount;
            $hit->artistCount = $artistCount;
            $hits[] = $hit;
        usort($hits, function($a, $b)
            return $a->_score < $b->_score;
    return $hits;