Content tagging JSS GraphQL

Created: 6 May 2019, last update: 30 Jan 2022

Content tagging JSS GraphQL

Context Tagging it is an out of the box features of Sitecore for Years, it came with Sitecore 7 and use Buckets so there can be many tags if you like. Context Tagging, from my experience it is rarely being used. In Sitecore version 9.1 there is Tag content using Sitecore Cortex Content Tagging. Machine learning (ML) based natural language processing. It is now extra easy to use. Also easy to configure. Because the ML Content tagging it is now cool :) see Tag content using Sitecore Cortex Content Tagging.

Using content tagging in the frontend with JSS.
The tags are stored in field __semantics this Standard field is default not in the JSS output so you need to add it. See JSS Customizing Layout Service Rendering Output.

Using GraphQL with Content tagging
You can get the tags with this Integrated GraphQL query

query MyDemoQuery( $contextItem: String!) 
{
  contextItem: item(path: $contextItem) {
    id
    semantics:field(name  : "__semantics") {
      ...Semantics
      }
    } 
  }

fragment Semantics on MultilistField {
  tags: targetItems {
  name
  id
  }
} 

Output:

{
  "data": {
    "contextItem": {
      "id": "8ED40F28F4BC492E806D7B6D4EA0CD86",
      "semantics": {
        "tags": [
          {
            "name": "Technology_Internet",
            "id": "A3D34571E2F14F42B6B589C462F399BF"
          },
          {
            "name": "Sitecore",
            "id": "A52538B6F3B14AAB8B498BCCB1716EB0"
          },
          {
            "name": "sitecore",
            "id": "79F943CB365E40A390AF3937AF641F90"
          }
        ]
      }
    }
  }
}

Search, You can also search on the __semantics field, you need to use the lowercase value of the GUID in the Query, else you found nothings.

{
  search( 
   fieldsEqual:[{name:"__semantics",value:"a52538b6f3b14aab8b498bccb1716eb0"}] 
  rootItem:"/sitecore/content"
       ) {
   results {
     items {
       item {
         id
         name
         path
         semantics: field(name  : "__semantics")  {
          value
          ...Semantics
         }     
       }
     }
     totalCount
   }
  }
}

fragment Semantics on MultilistField {
  tags:targetItems {
  name
  id
  }
}

Output:

{
  "data": {
    "search": {
      "results": {
        "items": [
          {
            "item": {
              "id": "8ED40F28F4BC492E806D7B6D4EA0CD86",
              "name": "home",
              "path": "/sitecore/content/MyApp/NL/home",
              "semantics": {
                "value": "{A3D34571-E2F1-4F42-B6B5-89C462F399BF}|{A52538B6-F3B1-4AAB-8B49-8BCCB1716EB0}|{79F943CB-365E-40A3-90AF-3937AF641F90}",
                "tags": [
                  {
                    "name": "Technology_Internet",
                    "id": "A3D34571E2F14F42B6B589C462F399BF"
                  },
                  {
                    "name": "Sitecore",
                    "id": "A52538B6F3B14AAB8B498BCCB1716EB0"
                  },
                  {
                    "name": "sitecore",
                    "id": "79F943CB365E40A390AF3937AF641F90"
                  }
                ]
              }
            }
          },
          {
            "item": {
              "id": "44C3D3B8125D419FBACE7B736FD72542",
              "name": "11",
              "path": "/sitecore/content/MyApp/NL/home/11",
              "semantics": {
                "value": "{A52538B6-F3B1-4AAB-8B49-8BCCB1716EB0}",
                "tags": [
                  {
                    "name": "Sitecore",
                    "id": "A52538B6F3B14AAB8B498BCCB1716EB0"
                  }
                ]
              }
            }
          },
          {
            "item": {
              "id": "54F86D59FE7E4827BFA1723760B034F6",
              "name": "new",
              "path": "/sitecore/content/MyApp/NL/home/new",
              "semantics": {
                "value": "{A52538B6-F3B1-4AAB-8B49-8BCCB1716EB0}",
                "tags": [
                  {
                    "name": "Sitecore",
                    "id": "A52538B6F3B14AAB8B498BCCB1716EB0"
                  }
                ]
              }
            }
          }
        ],
        "totalCount": 3
      }
    }
  }
}

Example with facets

{
 search( 
   fieldsEqual:[{name:"_content",  value:"home" }] 
   facetOn:["__semantics"]
   rootItem:"/sitecore/content"
       ) {
   results {
     items {
       item {
         id
         name
         semantics: field(name  : "__semantics")  {
          tags: value
         }
       
       }
     }
     totalCount
   }
    facets {
      name
      values(hideEmpty: true) {
        count
        item {
          displayName
          id
        }
      }
    }
 }
}

Output:

 {
  "data": {
    "search": {
      "results": {
        "items": [
          {
            "item": {
              "id": "F2C56C7603EF44D9A51193771558BEA8",
              "name": "home 2",
              "semantics": {
                "tags": ""
              }
            }
          },
          {
            "item": {
              "id": "44C3D3B8125D419FBACE7B736FD72542",
              "name": "11",
              "semantics": {
                "tags": "{A52538B6-F3B1-4AAB-8B49-8BCCB1716EB0}"
              }
            }
          },
          {
            "item": {
              "id": "8ED40F28F4BC492E806D7B6D4EA0CD86",
              "name": "home",
              "semantics": {
                "tags": "{A3D34571-E2F1-4F42-B6B5-89C462F399BF}|{A52538B6-F3B1-4AAB-8B49-8BCCB1716EB0}"
              }
            }
          },
          {
            "item": {
              "id": "110D559FDEA542EA9C1C8A5DF7E70EF9",
              "name": "Home",
              "semantics": {
                "tags": ""
              }
            }
          }
        ],
        "totalCount": 4
      },
      "facets": [
        {
          "name": "__semantics",
          "values": [
            {
              "count": 2,
              "item": {
                "displayName": "Sitecore",
                "id": "A52538B6F3B14AAB8B498BCCB1716EB0"
              }
            },
            {
              "count": 1,
              "item": {
                "displayName": "Technology_Internet",
                "id": "A3D34571E2F14F42B6B589C462F399BF"
              }
            }
          ]
        }
      ]
    }
  }
}

Azure Search
It works with SOLR, Currently there are some issues with Azure Search, An Example the facet query gives this error in JSS 11.1:

  "facets": null
    }
  },
  "errors": [
    {
      "message": "GraphQL.ExecutionError: Error trying to resolve facets. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index\r\n   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)\r\n   at Sitecore.ContentSearch.Azure.Query.SearchResultIterator.d__9.MoveNext()\r\n   at Sitecore.ContentSearch.Azure.Query.CloudSearchResults`1.d__15.MoveNext()\r\n   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)\r\n   at Sitecore.Services.GraphQL.Content.GraphTypes.ContentSearch.ContentSearchResultsGraphType.ResolveFacets(ResolveFieldContext`1 context)\r\n   at GraphQL.Resolvers.FuncFieldResolver`2.GraphQL.Resolvers.IFieldResolver.Resolve(ResolveFieldContext context)\r\n   at GraphQL.Execution.ExecutionStrategy.d__7.MoveNext()\r\n   --- End of inner exception stack trace ---",
      "locations": [
        {
          "line": 23,
          "column": 5
        }
      ],
      "path": [
        "search",
        "facets"
      ],
      "extensions": {
        "code": "ARGUMENT_OUT_OF_RANGE"
      }
    }
  ]
}