GraphQL requests executing simple searches
Several samples illustrate how a simple search string entered by your visitor can be put in a GraphQL request.
The following simple search looks for the search string "interest rate" in any English-language content of any objects:
search(
criteria: {
languageField: {
key: "content"
value: "interest rate"
language: "english"
}
}
)
{
results {
edges {
node {
searchResult {
id
itemType
fields
mainContentField
rawLanguageTitle
}
}
}
}
}
The following search uses an OR operator to combine the previous search with a search for the search string in the English raw language title:
search(
criteria: {
languageField: {
key: "content"
value: "interest rate"
language: "english"
or: {
field: {
key: "rawLanguageTitle"
value: "interest rate"
language: "english"
}
}
}
}
)
{
results {
edges {
node {
searchResult {
id
itemType
fields
mainContentField
rawLanguageTitle
}
}
}
}
}
Both of the above samples return content published using data publishing. If you want to retrieve content published using the old template-based publishing framework (which produces Component Presentations, among others), you must also include a
brokerResult node. The following sample shows a query fetching fields from both results:
search(
criteria: {
languageField: {
key: "content"
value: "吹きエア調整"
language: "japanese"
}
}
)
{
results(first: 2, after: "Mg==") {
edges {
cursor
node {
searchResult {
id
itemType
locale
rawLanguageTitle
publicationTitle
publicationId
mainContentField
}
brokerResult{
title
}
}
}
}
}
The following codeblock similarly performs a search in content that was published using templates, but it searches by Content Manager ID:
search(
criteria: {
id: {
values: ["tcm_5-342"]
}
}
)
{
results {
searchResult {
id
rawLanguageTitle
}
brokerResult {
title
itemType
}
}
}
Another search for template-rendered content, searching in a language-specific field:
search(
criteria: {
languageField: {
key: "content"
value: "Sitemap"
language: "english"
strict: false
}
}
)
{
results {
edges {
cursor
node {
searchResult {
id
rawLanguageTitle
}
brokerResult {
title
itemType
}
}
}
}
}
Finally, an example of a sorted OR search. Results are sorted by the
clientId metadata text field. Note that search result sorting is case-sensitive (that is, "Zorro" is sorted above "giraffe" because uppercase "Z" comes before lowercase "g").
search(
criteria: {
field: {
key: "locale"
value: "en_gb"
strict: false
}
or: {
languageField: {
key: "content"
value: "Lorem"
language: "english"
strict" true
}
}
}
)
{
results {
edges {
cursor
node {
searchResult {
id
rawLanguageTitle
}
brokerResult {
title
itemType
}
}
}
}
sortBy: {
fields: "clientID"
sortAsText: true
sortingDirection: ASCENDING
}
}
For more information, consult the API documentation itself, available through a client such as GraphiQL or GraphQL PlayGround.