d
Topic
Posts:
21
11 days ago

Search product from title, handle and description

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.

i
Replies
Kieran Masterton Shopify Employee
Posts:
2
Last edited 10 days ago
g
1
upvotes

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!

Posts:
21
10 days ago

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

Posts:
21
8 days ago

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 

Kieran Masterton Shopify Employee
Posts:
2
7 days ago

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

Posts:
21
6 days ago

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