TNT Search and Taxonomy Included in Results

I am attempting to add add a taxonomy value inside of TNTSearch result. Specifically, my search results show the Title and a Content snippet. I want to include a taxonomy in the search results. My frontmatter:

---
title: TEST Document
taxonomy:
	id: ['dp-04']
---

I want to include in my TNT Search Result: {{ val.taxonomy.id }}

<a href="{{ base_url ~ val.link }}">{{ val.taxonomy.id }} {{ val.title }}</a>

1 Like

Hi @crusnac, you might find this thread helpful: Indexing Taxonomies with TNTsearch

I was able to include my taxonomy in the index, but I want to display it in the search output as well. Any ideas?

1 Like

Sorry, I do not know how you would do that myself.

I got this to work. See code for reference below. Note - I am only including the Taxonomy->ID field in the results. Code adjustment may be necessary to include other taxonomy.

  public function onTNTSearchQuery(Event $e){
        $query = $e['query'];
        $page = $e['page'];
        $options = $e['options'];
        $fields = $e['fields'];
        $gtnt = $e['gtnt'];

        $tax = $page->taxonomy();

        if (!isset($tax['id'][0])){
            $id = $tax['id'][0] = ''; // set default value
        }else{
            $id = $tax['id'][0];
        }

        $content = $gtnt->getCleanContent($page);
        $title = $page->title();


        $relevant = $gtnt->tnt->snippet($query, $content, $options['snippet']);

        if (strlen($relevant) <= 6) {
            $relevant = substr($content, 0, $options['snippet']);
        }

    
        $fields->hits[] = [
            'link' => $page->route(),
            'title' =>  $gtnt->tnt->highlight($title, $query, 'em', ['wholeWord' => false]),
            'content' =>  $gtnt->tnt->highlight($relevant, $query, 'em', ['wholeWord' => false]),
            'id' =>  $gtnt->tnt->highlight($id, $query, 'em', ['wholeWord' => false]),
        ];
        
    $e->stopPropagation();
}

Also, don’t for get to call the onTNTSearchQuery function in the beginning.

 public static function getSubscribedEvents()
    {
        return [
            'onTNTSearchIndex' => ['onTNTSearchIndex', 0],
            'onTNTSearchQuery' => ['onTNTSearchQuery', 1000]
        ];
    }
1 Like

Awesome, thanks for sharing @crusnac!