Irfan Bilir
Irfan Bilir

Reputation: 73

How to use a "where not" clause with searchkick

I am using Searchkick with Elasticsearch to get products with a search term.

I am trying to add a WHERE NOT clause to it so it won't return any products with a regular_price that is null.

@products = Product.search(
        search,
        where: {
          regular_price: {
            not: "null" # I have also tried nil, both won't work
          }
        },
        page: params[:page],
        per_page: per_page,
      )

The search ignores my WHERE NOT clause.

I would also be grateful for some debugging tips for this.

Edit:

So I have tried to use the following but now it just returns every product that fits the search. Ignoring the WHERE NOT part of the search.

@products = Product.search(
        search,
        where: {
          regular_price: {
            _not: "null" 
          }
        },
        page: params[:page],
        per_page: per_page,
      )

UPDATE:

I have not found a solution to this issue and I moved on with a diffrent approach. By filtering out the products with a regular_price of 0.

Upvotes: 2

Views: 1594

Answers (2)

varunvlalan
varunvlalan

Reputation: 950

@products = Product.search(
    search,
    where: {
      regular_price: {
        _not: nil 
      }
    },
    page: params[:page],
    per_page: per_page,
  )

Just replace "null" with nil and it would filter out the products that don't have regular_price.

Upvotes: 2

Irfan Bilir
Irfan Bilir

Reputation: 73

Haven't found an answer yet. Because there aren't many products that I want to filter out with this query, I just filter them out while rendering.

Upvotes: 0

Related Questions