0

I am counting nodes of a specific type associated with terms with this query:

$connection = \Drupal::database();

$query = $connection->select('taxonomy_index', 'ti');
$query->join('node_field_data', 'nfd', 'ti.nid = nfd.nid');
$query->addField('ti', 'tid', 'tid');
$query->condition('tid', $entity->id(), '=');
$query->condition('nfd.type', 'article');
$query->condition('nfd.status', '1');

$count = $raw_data = $query->countQuery()->execute()->fetchField();

I then use this information in a live search. Calling the query each time the page is refreshed is a performance overhead.

Is it possible to calculate these values (node count of each term from some vocabularies) and store it in a search index?

  • You could cache the result per term in such a way that the cache is invalidated only when something happens that might affect the result, eg an article is published. Drupal 8 has built-in mechanisms for doing this. Read up on cache tags and see if that helps. – Alfred Armstrong Apr 10 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.