GraphQL how to do a "Phrase Query"

Solved
Excursionist
18 3 0

I am trying to do a "Phrase Query" on the product title field. In my example the phrase is: Quick Fuel

 

The following query returns everything with the word Quick in the title. The first 4 returned products in the screenshot do not have the phrase "Quick Fuel" in the title.

non-phrase-query.png

 

Based on the docs, the phrase needs to have double quotes around it:

docs-phrase.png

 

Double quotes around the query and the phrase is a syntax error:

multiple-double-quotes-error.png

 

Single quotes around the query and double quotes around the phrase is a syntax error:

single-quotes-error.png

 

Double quotes around the query and single quotes around the phrase returns nothing:

single-quotes-nothing-returned.png

 

How can this be done?

 

Thanks

0 Likes
Shopify Staff
Shopify Staff
915 54 123

Hey there, 

 

Are you still able to replicate this? I set up some products in my own test shop titled "Quick Fuel Brawler" and then another called "Quick Test" but when performing the same query as you posted (query: "title:Quick Fuel") only the product with "Quick Fuel" in the title is being returned. So it looks like this may have been fixed. 

 

If it's still not working as you'd expect though, could you post your shop domain here? Or if you'd rather not provide that, even a product ID would work and I could find your shop with that and attempt to replicate again.

0 Likes
Excursionist
18 3 0

I just ran it again and I'm still getting the same results. I tried it with both API's.

 

Here's the shop domain: https://universal-auto-parts.myshopify.com/

 

Admin API:

Screen Shot 2019-07-16 at 3.35.17 PM.png

 

Storefront API:

Screen Shot 2019-07-16 at 3.36.06 PM.png

0 Likes
Highlighted

Success.

Shopify Staff
Shopify Staff
915 54 123

Hey again,

 

So what actually happens when you run 'query: "title:Quick Fuel"' is a search for products that have 'quick' in the title, and then 'fuel' in any searched field (title, description, handle, product type, etc.) - so I'm assuming all of the products you weren't expecting to appear have 'fuel' as some product attribute somewhere. I had only been copying your product titles when trying to replicate this, which is why I wasn't seeing them show up on my own test store. 

 

In order to make this search work and only return products with "Quick Fuel" in the title, this should work : query: "title:\"quick fuel\"*"

0 Likes
Excursionist
18 3 0
That did it, Thank you!
0 Likes
New Member
3 0 0

Thanks @Josh, I was getting tripped up on the same thing.

 

The docs didn't list " and ' as special characters that can be backslash escaped: https://help.shopify.com/en/api/getting-started/search-syntax#special-characters …just FYI incase they should be flagged for an update(?) Cheers.

0 Likes
New Member
3 0 0

Sorry - follow up question: Does this work when searching for a term with trailing whitespace?

 

I need to clean up duplicate vendors "my_vendor" and "my_vendor " (with trailing whitespace). Currently both:

products(query: "vendor:\"my_vendor \"*", first:3)   # With asterisk
products(query: "vendor:\"my_vendor \"", first:3) # Without asterisk

return products containing:

"vendor": "my_vendor"

 

0 Likes
Shopify Staff
Shopify Staff
915 54 123

Hey @malalta , 

 

I'm not sure off the top of my head, to be honest. I'd need to do some testing to see how to do that or if it can even be done, but I'm not having any luck saving a vendor with whitespace at the end of the name. Are you familiar at all with how you got a vendor name into that state? I've tried using the API, the admin, and a CSV import but the whitespace is getting stripped from the end of the name in each case.

0 Likes
New Member
3 0 0

I'm not sure how it got into this state unfortunately. The client has just hired our agency to help, so it was that way before I came along...

 

If it's an edge case don't worry too much about it. I'll just write some code to loop through all products and strip the whitespace from any vendor value. If your testing shows that whitespace is stripped when saving these days, we should be able to avoid this happening again going forward. Cheers, Andrew.

0 Likes