Unlock seamless workflows and faster delivery with our latest releases – get the details

How to search for articles containing the word "demo" in any field of the document.

16 replies
Last updated: Aug 9, 2021
Can anyone help me how can i get All articles which has the word "demo" in article document. Here I have article document in that i have title, description, body, source etc i want that "demo" word is searched inside whole Article document and would return me all articles which has demo present in any field.
Aug 5, 2021, 11:48 AM
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
Aug 5, 2021, 2:33 PM
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
Aug 5, 2021, 2:33 PM
On top of what Joseph said, the docs on Query Scoring and GROQ Parameters may be relevant as you’re working on this.
Aug 5, 2021, 2:37 PM
HI - you can find articles with the string demo in the body field (assuming it's blocks) like this
*[body[].children[].text match "demo"]
, but you would need to use a series of filters to catch "demo" in other fields as well, with something like

*[body[].children[].text match "demo" || title match "demo"]
though that could get cumbersome. What about a boolean field called "demo":
https://www.sanity.io/docs/boolean-type ?
Aug 5, 2021, 2:33 PM
if i use like this then is it fine? *[_type == "article"] | ["Times of India" in [title,publicdescription,body[].children[].text,source]]
Aug 5, 2021, 2:39 PM
can you give brief whats difference of using "in" and "match" when i tried using match it was causing me error. I dont know why
Aug 5, 2021, 2:40 PM
in
will look for an operand in an array or path . It won’t work in your case as title, publicdescription, source, etc. are probably not arrays but string fields.

match
is a text search operator and would be the operator I would use here. The array traversal might cause an issue so I would use the pt::text() function to return a string instead of traversing through body[] and children[]:

*[_type == 'article'][[title, publicdescription, pt::text(body), source] match "Times of India"]
Aug 5, 2021, 2:49 PM
On using above query i m getting error.
Aug 6, 2021, 4:59 AM
Any chance you're using v1 of the GROQ API? To use the
pt::text()
function you'll need to use v2021-03-25 or later.
Aug 6, 2021, 6:41 AM
My word is not getting search in body which is rich text other all fields its works perfectly.Even though i changed the version from v1 to v20121-03-25 its gives same error.
Aug 6, 2021, 6:58 AM
Interesting. When you run this query do you get an array of objects containing the conjoined content from each document’s rich text field?

*[_type == 'article'] {
	'richText': pt::text(body)
}
Aug 6, 2021, 3:33 PM
Interesting. When you run this query do you get an array of objects containing the conjoined content from each document’s rich text field?

*[_type == 'article'] {
	'richText': pt::text(body)
}
Aug 6, 2021, 3:33 PM
Thanks for help
Aug 7, 2021, 11:46 AM
Thanks for help
Aug 7, 2021, 11:46 AM
No problem. Were you able to get things working?
Aug 7, 2021, 5:02 PM
Yes its working with some 10-15sec delay but it works for me thanks
Aug 9, 2021, 4:03 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?