Search product from title, handle and description

Shopify Partner
27 0 2

Hi Guys,

I am trying to search product through shopify API. I need to search the query param from product title, handle and description(if possible).

I can do that individually like below:

@title = params[:query]
#search by title
@products = ShopifyAPI::Product.find(:all,:params => {:title=> @title})
#search by handle
@products = ShopifyAPI::Product.find(:all,:params => {:handle=> @title})

But what i want is for the query parameter it should search for both title as well as handle and give the combine result. It should use OR logic between the fields to search.

Can anyone help.

0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
22 0 11

Hi Navin,

It is possible to pass multiple params to the find method like so:

@title = 't-shirt'
@products = ShopifyAPI::Product.find(:all,:params => {:title => @title, :handle => @title})

However, this will build request to the products endpoint with the following query params:

/admin/products.json?title=t-shirt&handle=t-shirt

As you may have found these query params limit the results using an AND operator, meaning you won't receive the combined results you're looking for. Instead, you'll need to make two requests and merge these results.

If you're concerned about the number of requests your app is making, a good alternative would be to use our GraphQL Admin API which will allow you to get the results you need in one request. Here's an example query:

{
  productByHandle(handle:"t-shirt") {
    id
    title
  }
  products(first:10, query:"title:*t-shirt*") {
    edges {
      node {
        id
        title
        handle
      }
    }
  }
}

And here's some more documentation about the GraphQL Admin API and how to query it via tha shopify_api gem

I hope this helps!

1 Like
Shopify Partner
27 0 2

Hi Kieran,

Thank you very much for pointing me to the right direction. I haven't had chance of looking into the GrahpQL API but i will surely look into it and try to implement it here.

Thanks again for your valuable suggestion.

Regards,

Navin

0 Likes
Shopify Partner
27 0 2

Hi Kieran,

A quick question. On your answer above, the first option  you put forward was to send multiple API request to get the products individually and then merging it. 

But will not there be replication of products in the merged result? As both API could respond with common products.

What do you think of this problem? Is there any workaround to this?

Regards,

Navin 

0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
22 0 11

Hi Navin,

I think you'd be best to de-dupe based on handle as these are unique for products in Shopify. I would opt to do something like this:

@products = @products.uniq { |p| p.handle }

(where @products is your merged array of product objects)

Here's a similar question on SO: https://stackoverflow.com/questions/109781/uniq-by-object-attribute-in-ruby

I hope this helps!

Cheers,

Kieran

0 Likes
Highlighted
Shopify Partner
27 0 2

Hi Kieran,

Thanks again for your reply. I got the gist and will try it.

I'm curious how the shopify does the product searching on its end. As i can see that it search from title,description and handle.

Are you aware of core logic on the search that shopify itself implements. Is there a possibility that we use the same? 

Thanks for your help!!

Regards,

Navin

0 Likes