Documentation Search

Documentation Search

Custom Document and Query Transformers

We can create custom document and query transformers. Document transformers are applied during the indexing process, and query transformers are applied to user search queries at search time.

#Writing Document Transformers

Document transformers are applied to each index entry during the indexing process. Document transformers should add a string to the index entry's additionalContextData array.

Before inserting an entry into the search index, the documentation search provider will combine all items within the additionalContextData into a single string and store it along with the rest of the indexed data. This allows us to add additional information that will be considered when users perform searches.

All document transformers must implement the Stillat\DocumentationSearch\Contracts\DocumentTransformer interface. The following example would ensure that each indexed item would be found using the search query "Super Awesome Phrase":

1<?php
2 
3use Stillat\DocumentationSearch\Contracts\DocumentTransformer;
4 
5class AwesomeDocumentTransformer implements DocumentTransformer
6{
7 public function handle(DocumentFragment $fragment, $entry): void
8 {
9 $fragment->additionalContextData[] = 'Super Awesome Phrase';
10 }
11}

#Writing Query Transformers

Query transformers are very similar to documentation document transformers, with the different being they are applied to a users search query when using the {{ documentation:results }} Antlers tag.

Query transformers implement the Stillat\DocumentationSearch\Contracts\QueryTransformer interface.

The following, incredibly simple, query transformer would remove all instances of the word "the" from a users search query:

1<?php
2 
3use Illuminate\Support\Str;
4use Stillat\DocumentationSearch\Contracts\QueryTransformer;
5 
6class SimpleQueryTransformer implements QueryTransformer
7{
8 public function handle(string $value): string
9 {
10 return Str::replace('the', '', $value);
11 }
12}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.