taxonomy terms – Select nodes that share some of the same tags, and sort by tags in common count

I have nodes that can have many tags.
I’m able to display nodes that share some of the tags.

        $query = Drupal::entityQuery('node');

        if ($nodeId != null) {
            $query->condition('nid', $nodeId, '<>');
        }
        $query->condition('field_tags', $tagsIds, 'IN');
        $query->condition('status', 1);

That works. But now I’d like to sort them by relevance. In other words, the ones that have the most tags in common should come first.

Any idea how I can achieve that?
Thanks!