How to update table taxonomy_index when adding a node referencing a term? I have two node types: Lesson and Exercise. A lesson contains several exercises, which are children. Lessons have a category field which is a reference field of type taxonomy term. Exercises point to lessons through a Lesson reference field, which I suppose indirectly maps exercises to terms.

When I create a lesson, I use the following code:

    $node = Node::create([
      // The node entity bundle.
      'type' =&gt; 'lesson',
      'langcode' =&gt; 'en',
      'created' =&gt; $lesson-&gt;created,
      'changed' =&gt; $lesson-&gt;changed,
      'uid' =&gt; $lesson-&gt;uid,
      'title' =&gt; $lesson-&gt;title,
      'field_category' =&gt; $tid,
      'body' =&gt; [
        'summary' =&gt; $lesson-&gt;summary,
        'value' =&gt; $lesson-&gt;body,
        'format' =&gt; 'full_html',
      ],
    ]);
    $node-&gt;save(); 


Note the field_category field.

The above does not result in the taxonomy_index table being updated. I could update it with an sql query, but I would like to know how to do it via the above core approach.

And for the Exercise node type, I suppose I need to do likewise, unless my linking of an Exercise to a Lesson already accomplishes the hierarchy.

How do I alter the above to result in the taxonomy_index table being updated?
I ended up figuring this out. here is what I did:

$result=db_query("SELECT nid FROM node WHERE type = 'lesson'");
foreach($result as $row){
    $node=entity_load('node', $row-&gt;nid);
    $result =  taxonomy_build_node_index($node);
}


This ensures that my lesson node is tracked in the taxonomy_index table. I should probably start learning the class-based approach, but for now this works.