I have a search results page that doesn't show all matching results. The result entries that do show appear to older entries that have smaller ID numbers, although I'm not sure if that has anything to do with the issue.

In my case, I have a people channel. Each person (entry) in the channel has a related entry called relatedOffices (associated with a locations structure section). Although the entries for "John", "Jim", and "Jerry" all have "Dallas" selected in the relatedOffices field, only "John" and "Jim" show in the results.

I've been able to deduce the issue down to the queryParam search string. If the search query string is just relatedOffices:Dallas, all three entries show. But if the search query string is AND relatedOffices:Dallas, only two of the entries show.

The addition of AND seems to change the results, but I'm not sure why. I'm hoping to keep the AND in the string as well because I'm using multiple parameters in my search.

I tried rebuilding the index (php craft resave/entries --update-search-index ), but that doesn't seem to resolve the issue.

Craft CMS 3.5.8 is what I'm running.

Here's my query string and query param set up:

{# 1. Getting parameters from URL  #}
{% set keywordParam = craft.app.request.getParam('q')%}
{% set nameParam = craft.app.request.getParam('name')%}
{% set industryParam = craft.app.request.getParam('industry')%}
{% set locParam = craft.app.request.getParam('loc')%}

{# 2.Building query string #}
{%- set queryString -%}
    {% spaceless %}
        {{- keywordParam ? "#{keywordParam} AND " -}}
        {{- nameParam ? "#{macros.paramString(nameParam, 'title')}" -}}
        {{- industryParam ? " AND #{macros.paramString(industryParam, 'industriesRelated')}" -}}
        {{- locParam ? " AND #{macros.paramString(locParam, 'relatedOffices')}" -}}
    {% endspaceless %}
{%- endset -%}

{# 2B. Escape queryString output and set default to be ' ' so that if there's no params, ' ' outputs which leads to no results #}
    {% set queryString = queryString|escape|default(' ') %}

{# 3. Set queryParams #}    
{% set queryParams = {
    section:    'people',
    limit:      15,
    search:     (queryString)
} %}
  • What is the final queryString you're sending? How are you executing your query? .al()? If you're searching on fields, I would set the parameters of your custom field itself. – RitterKnight Sep 9 at 17:00
  • @RitterKnight The final queryString is AND relatedOffices:Dallas. The query is executed by the paginate tag. The parameters is set to search within the custom field itself, too. To test this all, I directly passed AND relatedOffices:Dallas and relatedOffices:Dallas as the queryString value. The AND relatedOffices:Dallas version doesn't show all results, but relatedOffices:Dallas displays all the results, so I'm not sure why the inclusion of AND is throwing off the results. – user3092 Sep 9 at 17:17

Your Answer

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

Browse other questions tagged or ask your own question.