TransWikia.com

Front End Search Related Entries

Craft CMS Asked on January 4, 2021

I have a front end search which is working fine when searching titles, however when searching for text in rich text fields, or an input field caseNumber or the title of a related entry there are zero results.

The search form has an input field where copy from the entries body or the name of the investigator (related entry) can be searched. There is also a select field that is used for categories to filter.

How to get results from the related entries to display?

Below is my simplified template code.

  <section class="py-16 bg-grayBrand-lighter">
    <div class="container flex-col">


      {% set params = {
        section: 'inventions',
        limit: 10
      } %}

      {% if searchQueryKeywords !="" and searchQueryKeywords != null and searchQueryKeywords !="null" %}
{% set params = params | merge({
          search:'title:' ~ searchQueryKeywords ~ ' OR summary:' ~ searchQueryKeywords ~ ' OR applications:' ~ searchQueryKeywords ~ ' OR investigators:' ~ searchQueryKeywords ~ ' OR caseNumber:' ~ searchQueryKeywords,  })
        %}
      {% endif %}

      {#
      // get related category
      #}
      {% if searchCategory != null and searchCategory !="" %}
        {% set searchQueryCategory = craft.categories.group('researchAreas').slug(searchCategory).one() %}
      {% endif %}

      {% if searchQueryCategory is defined %}
        {% set params = params | merge ({
          relatedTo: [
            'and',
            { targetElement: searchQueryCategory }
          ]
        }) %}
      {% endif %}


      {# set the entries with all the search/fitering params #}
          {% set entries = craft.entries(params) %}

      {% paginate entries as pageInfo, pageEntries %}

      <p class="w-full mb-2 font-bold uppercase">
        Search Results
      </p>



    {#
    // let's do some math so we can display the current result count
    // so that it's 11-20 of 23 results if on page 2 with 23 results
     #}
    {% set firstCount = (pageInfo.currentPage * 10) - 9 %}
    {% set lastCount = "" %}
    {% for entry in pageEntries %}
      {% if loop.last %}
        {% set lastCount = firstCount + loop.length -1 %}
      {% endif %}
    {% endfor %}
    {# end math #}


      <p class="mb-6">
        <span class="font-bold">Displaying:</span> <span class="text-14">
        {% if entries|length =="0" %}
          0
        {% else %}
          {{ firstCount }} - {{ lastCount }} of {{ entries|length }}
        {% endif %}
         Results</span>
      </p>

      {% for entry in pageEntries %}
        <div class="mb-8 {{ loop.last ? '':'border-b border-borderColor pb-8' }}">


          <div class="flex flex-col md:flex-row md:justify-between">
            <div class="md:mr-16 md:w-2/3">
              <h4 class="mb-2 leading-7 text-23"><a href="{{ entry.url }}" class="no-underline hover:underline">{{ entry.title }}</a></h4>

              {% set summary = entry.summary|striptags %}
              <p>
                {# There's a bug with truncating text - currently can only limit by characters not words. Will look into solution later #}
                {{ seomatic.helper.truncateOnWord(summary, 250) }}
              </p>
            </div>

            <div class="mt-2 md:w-1/3">
              <p class="font-bold text-14">
                Investigators
              </p>

              <ul>
                {% for entry in entry.investigators.all() %}
                  <li class="leading-snug text-14">{{ entry.firstName }} {{ entry.lastName }}</li>
                {% endfor %}
              </ul>
            </div>

          </div>

        </div>
      {% endfor %}


    {% include '_includes/pagination' with {
          fullWidth:false,
        } %}



    </div><!-- /.container -->
  </section><!-- /.py-16 -->

One Answer

Make sure that you checked Use this field’s values as search keywords under Settings → Fields → Your field

With that in place, run ./craft resave/entries --update-search-index (docs) and your entries should start showing up in your search results.

Correct answer by Oli on January 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP