4

I'm trying to use the Sitecore GraphQL search function that takes in the "facetOn" parameter. Unfortunately I'm not having much luck with the results, and I suspect it's because I'm doing something wrong and don't know where to look for the documentation. So I have a Field called "Category" and I want to facet on that when I search because I imagine it will essentially give me a count of different "Category" value occurrences. But the below is not working.

query {
  search(rootItem: "92B4F8E7-6324-4C01-B873-7FDC992EDDEE" facetOn:["_category"] ) {
    results {
      totalCount
      items {
        item {
          name
          _category: field(name: "Category") {
            value
          }
        }
      }
    }

    facets {

      name
      values {
        item {
          id

        }
        value
      }

    }
  }
}
  • Do you have an error? or just zero facets? – Jan Bluemink Sep 26 at 7:58
  • I get Zero facets. I don't believe we're using any special searching that's not out of the box. Correct my if I'm wrong, but I think that may be Content Search? I've tried your solution below, facetOn ["Category"] and facetOn["_category"] both still return zero facets. Could it also be that the Category field is not text, but is rather a Grouped Droplist? @JanBluemink – UpcomingSitecoreExpert Sep 26 at 14:50
  • Do you have item result, with Category values? – Jan Bluemink Sep 26 at 15:09
  • Yep, there are results for the Category values. Do you think the field of faceOn need to be indexed? @JanBluemink – UpcomingSitecoreExpert Sep 26 at 18:49
  • For me it works with a Grouped Droplist. with version do you use? – Jan Bluemink Sep 27 at 7:14
1

Your query looks good, I test it on Sitecore 9.1, SOLR with a Text field, that work with this adjustment. It is now facetOn "_category" looks like because the field(name: "Category") your field is named "Category" so use

search(rootItem: "92B4F8E7-6324-4C01-B873-7FDC992EDDEE" facetOn:["Category"] )

It is not giving a count per facet. you can do that by adding the count to the facets

facets {
      name
      values {
        count
        item {
          id
        }
        value
      }
}

Note, this is working on SOLR, for Azure Search there are some limitations, facet on an list is an issue, ARGUMENT_OUT_OF_RANGE

Below is a working example on the out of the box Title field from the sample item. (note item in facet is null, because result is a string not a reference)

query {
  search(rootItem: "/sitecore/content/home" facetOn:["Title"]  ) {
    results {
      totalCount
      items {
        item {
          name
          _TitleField: field(name: "Title") {
            value
          }
        }
      }
    }

    facets {
      name
      values {
        count
        item {
          id
        }
        value
      }

    }
  }
}

One thing to mention, in my returning facet name it is lowercased so Category returns as category, also a GUID returned as uppercase without dash, if you want to search on that do first a to lowercase.

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.