TransWikia.com

GraphQL query to search for pages based on template

Sitecore Asked by Serhii Shushliapin on August 23, 2021

The following GraphQL query returns items based on the Page template:

  search(keyword: "Dream",
    first: 10, 
    language: "en",
    fieldsEqual: [{name:"_templatename", value:"Page" }]
  ) {
    results {
      items {
        name
        id
        language
        item {
          url
        }
      }
    }
  }
}

ATM, Page is the only page template in the solution so the query works fine. There is a need to introduce new (numerous) page types like Product Page, FAQ, Organization inheriting Page (mainly for the content editor usability to assign proper insert options).

How to extend the query to cover all the pages inheriting Page template?

2 Answers

Instead of targeting the _templatename field you should target the multi-valued _templates field which contains the inheritance tree for an item. Since this will get translated into a Solr query, despite the fact that we're using the "fieldsEqual" argument the field only needs to contain the value, which is perfect for our inheritance filtering use case.

Here's what the changed argument should look like (be sure to change the ID):

   fieldsEqual: [{name:"_templates", value:"e6fd0c645a27449caafece6f377b3e3e" }]

You'll notice that the value needs to be template ID we're looking for, not the name of it. You also need to make sure the ID is normalized to what gets indexed (no brackets, lower case alpha, and no dashes).

If you need to dynamically convert your templates to useable ID's at query time, you may find this method useful: Sitecore.ContentSearch.Utilities.IdHelper.NormalizeGuid

Answered by Justin Laster on August 23, 2021

Currently, there is no OR in the search, you can do a search for each page type, or first do a facetOn: _templatename than you know how many items there are for each templateName

But to keep it simple add a CheckBox Field with for example name IncludeInSearch default value 1 to all Page template that can include in the search. and use

fieldsEqual:[{name:"IncludeInSearch", value:"1" } ]

and re-index your index.

As a side effect editor have no the option to hide also pages from the search.

Answered by Jan Bluemink on August 23, 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