Knowledge Base

Search Module Filters

Last Modified:
26 Jul 2021
User Level:
Administrator +

This section outlines the facets or filters available for use within the PHP Search Module.

These items are configured on the 'text/config' cotnent layout for the Search Module Configuration content type and displayed via the Search Module View cotnent type.

There are mutliple configurable facets available for use to filter results, below are examples of what you can use.

Default filters:

  • Search
  • Substring Search
  • Word Search
  • Exact Match
  • Cost Search
  • Date Search
  • A-Z Filter

The first step is to ensure each of your required fitlers are available for use and initialized within the text/config of the Search Module Configuration.

 $search = \T4\PHPSearchLibrary\SearchFactory::getInstance('Search', $config['source']);
$substringSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterBySubstring', $search);
$wordSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterByWord', $search);
$letterComparisonSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterByLetterComparison', $search);
$costSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterByRange', $search);
$exactSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterByExactMatch', $search);
$dateSearch = \T4\PHPSearchLibrary\FilterFactory::getInstance('FilterByDate', $search);

Below are examples of each filter in use. Each example has two code blocks. One corressponding to the configuration, where the logic is applied and another used within the view to display the facet/filter. 

The example below is directly from the Terminalfour Sample Site. It includes a refrence to the HTML and the JSON, where multiple elements are searched at once, using a combination option available within each facet/filter. Here's a link to the Sample Site where each filter can be seen/used. 

Substring Search

This filter will return results that contain the query anywhere within the element e.g. 'bus' will match 'business', 'bus', 'filibuster'.

Search Module Configuration: text/config 

if ($queryHandler->isQuerySet('keywords')) {
    $substringSearch->setMember('element', 'courseName');
    $substringSearch->setMember('query', $queryHandler->getQueryValue('keywords'));
    $substringSearch->setMember('combinationOption', true);
    $substringSearch->runFilter();
    $substringSearch->setMember('element', 'courseOverview');
    $substringSearch->runFilter();
    $search->combineResults();
}

Search Module View: View element

<div id="searchoptionsGeneric" role="search" data-t4-ajax-group="courseSearch" aria-label="Generic Search">
  <form method="get">
    <div class="panel course-search-widget">
      <fieldset>
        <legend>Filter for programs</legend>
        <noscript>
          <button type="submit" class="button small secondary expand">Search by keyword</button>
        </noscript>
        <div id="hidden-form-generic" data-t4-ajax-group="courseSearch">
          <?php
            $formatQueryAsHiddenInput = \T4\PHPSearchLibrary\QueryFormatterFactory::getInstance('FormatQueryAsHiddenInput', $queryHandler);
            $formatQueryAsHiddenInput->setMember('excludedQueries', array('keywords', 'page'));
            echo $formatQueryAsHiddenInput->format();
          ?>
        </div>
      </fieldset>
    </div>
  </form>
</div>

Word Search

This filter will return results that contain the query as a contiguous phrase or word e.g. 'bus' will match 'bus', but NOT 'business', or 'filibuster'.

Search Module Configuration: text/config 

if ($queryHandler->isQuerySet('courseType')) {
    $wordSearch->setMember('element', 'courseType');
    $wordSearch->setMember('query', $queryHandler->getQueryValue('courseType'));
    $wordSearch->runFilter();
}

Exact Match

This filter will return results that have the provided query in the element and nothing else. This is a stricter implementation than Word Search, above.

Search Module Configuration: text/config 

if ($queryHandler->isQuerySet('courseDepartments')) {
    $exactSearch->setMember('element', 'courseDepartments');
    $exactSearch->setMember('query', $queryHandler->getQueryValue('courseDepartments'));
    $exactSearch->setMember('multipleValueState', true);
    $exactSearch->setMember('multipleValueSeparator', ', ');
    $exactSearch->runFilter();
}

Search Module View: View element

<?php
    $element = 'courseType';
    $genericFacet->setMember('element', $element);
    $genericFacet->setMember('type', 'List');
    $genericFacet->setMember('facetSource', 'documents');
    $genericFacet->setMember('sortingState', true);
    $genericFacet->setMember('multipleValueState', true);
    $genericFacet->setMember('multipleValueSeparator', ', ');
    $genericFacet->setMember('customSortByName', ['Undergraduate','Postgraduate','Advanced Diploma','Associate']);
    $search = $genericFacet->displayFacet(); ?>
<?php if (!empty($search)) : ?>
  <div id="checkboxes-<?php echo strtolower($element)?>" class="columns-full">
    <div class="panel course-search-widget">
      <fieldset>
        <legend class="h4">Departments</legend>
        <span class="clear" data-t4-clear="<?php echo $element; ?>">Clear filters &times;</span>
        <?php $i = 0; ?>
        <?php foreach ($search as $item) : ?>
          <label for="<?php echo $element.'['.$i.']'; ?>" class="label-text">
            <input type="checkbox" id="<?php echo $element.'['.$i++.']'; ?>" value="<?php echo $item['value'] ?>" data-cookie="T4_persona" name="<?php echo $element ?>" data-t4-value="<?php echo strtolower($item['value']) ?>" <?php echo $item['selected'] ? 'checked' : '' ?>>
            <?php echo $item['label'] ?>
          </label>
        <?php endforeach; ?>
      </fieldset>
    </div>
  </div>
<?php endif; ?>

Cost/Range Search

This filter will return results that are within the specified numeric range in the chosen element. An example would be returning courses that have a cost associated with them so the user might like to see courses that cost between 1,000 to 5,000.

Search Module Configuration: text/config 

if ($queryHandler->isQuerySet('courseCost')) {
    $costSearch->setMember('element', 'courseCost');
    $costSearch->setMember('query', $queryHandler->getQueryValue('courseCost'));
    $costSearch->runFilter();
}

Search Module View: View element

<?php
  $element = 'courseCost';
  $genericFacet->setMember('element', $element);
  $genericFacet->setMember('type', 'Range');
  $genericFacet->setMember('facetSource', 'documents');
  $genericFacet->setMember('sortingState', true);
  $genericFacet->setMember('multipleValueSeparator', ', ');
  $search = $genericFacet->displayFacet(); ?>
  <?php if (!empty($search)) : ?>
    <div id="range-<?php echo strtolower($element)?>" class="columns-full">
      <div class="panel course-search-widget">
        <div class="h4">Maximum Cost</div>
        <span class="clear" data-t4-clear="<?php echo $element; ?>">Clear filters &times;</span>
        <label for="pay-range-slider" class="hidden">Range</label>
        <?php $step = ($search['max']-$search['min'])/8 ?>
          <div class="range">
            <input
              name="<?php echo $element ?>"
              class="range-slider2"
              id="pay-range-slider"
              type="range"
              min="<?php echo $search['min']; ?>"
              max="<?php echo $search['max']; ?>"
              step="<?php echo $step; ?>"
              value="<?php echo $search['value']; ?>" />
            <div class="range-list">
            <?php if ($step > 0) : ?>
              <?php for ($i=$search['min'], $c=0; $i<=$search['max']; $i+=$step, $c++) : ?>
                <?php if ($c%2!=1) : ?>
                  <span><?php echo '$'.($i/1000).'K' ?></span>
                <?php endif;?>
              <?php endfor; ?>
            <?php endif;?>
            </div>
          </div>
      </div>
   </div>
<?php endif; ?>

Date Search

Search based on a Date Element. Content can be filtered by users entering two dates and only displaying content falling within that date range.

Search Module Configuration: text/config 

 if ($queryHandler->isQuerySet('startDateBefore') || $queryHandler->isQuerySet('startDateAfter')) {
    $dateSearch->setMember('element', 'startDateNew');
    if ($queryHandler->isQuerySet('startDateAfter')) {
        $dateSearch->setMember('query', $queryHandler->getQueryValue('startDateAfter'));
        $dateSearch->setMember('type', "after");
        $dateSearch->runFilter();
    }
    if ($queryHandler->isQuerySet('startDateBefore')) {
        $dateSearch->setMember('query', $queryHandler->getQueryValue('startDateBefore'));
        $dateSearch->setMember('type', "before");
        $dateSearch->runFilter();
    }
}

Search Module View: View element

<?php
    $elementAfter = 'startDateAfter';
    $genericFacet->setMember('element', $elementAfter);
    $genericFacet->setMember('type', 'Date');
    $genericFacet->setMember('facetSource', 'documents');
    $genericFacet->setMember('sortingState', true);
    $genericFacet->setMember('multipleValueSeparator', ', ');
    $dateAfter = $genericFacet->displayFacet();
    $elementBefore = 'startDateBefore';
    $genericFacet->setMember('element', $elementBefore);
    $genericFacet->setMember('type', 'Date');
    $genericFacet->setMember('facetSource', 'documents');
    $genericFacet->setMember('sortingState', true);
    $genericFacet->setMember('multipleValueSeparator', ', ');
    $dateBefore = $genericFacet->displayFacet(); ?>
<?php if (!empty($search)) : ?>
    <div id="range-<?php echo strtolower($element)?>" class="columns-full">
        <div class="panel course-search-widget">
            <div class="h4">Starting Dates</div>
            <span class="clear" data-t4-clear="<?php echo $elementAfter.','.$elementBefore; ?>">Clear filters &times;</span>
            <label for="date-<?php echo strtolower($elementAfter)?>">
                 From
            </label>
            <input
                name="<?php echo $elementAfter ?>"
                id="date-<?php echo strtolower($elementAfter)?>"
                type="date"
                placeholder="YYYY-MM-DD"
                value="<?php echo $dateAfter['value']; ?>" />
            <label for="date-<?php echo strtolower($elementBefore)?>">
                To
            </label>
            <input
                name="<?php echo $elementBefore ?>"
                id="date-<?php echo strtolower($elementBefore)?>"
                type="date"
                placeholder="YYYY-MM-DD"
                value="<?php echo $dateBefore['value']; ?>" />
        </div>
    </div>
<?php endif; ?>

Back to top