2

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 -->
  • Could it be because you are hard-setting section: 'inventions', in your params? – Oli Nov 9 '20 at 10:35
  • @Oli I would expect that results would show since entries have relationships to the investigators and in this case I want to show results where the investigators field in the inventions channel has the correct person. – CreateSean Nov 9 '20 at 15:05
  • Oh, I see, I thought you wanted entries from another section. Can you try to re-index your entries just to make sure ./craft resave/entries --update-search-index. – Oli Nov 10 '20 at 13:09
  • I tried that before posting this and it didn't work. @oli should I send an email directly to support? – CreateSean Nov 10 '20 at 17:06
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.