Dias Dias - 4 months ago 29
Ruby Question

How to do complex querying with logical operations by using searchkick

Iam using searchkick library as an elasticsearch client for Product searching.
https://github.com/ankane/searchkick

It is possible to create 'OR' condition and 'AND' condition;

AND operation
Product.search where: {price: {lte: 200}, in_stock: true}

OR operation
Product.search where: {or: [[{in_stock: true}, {backordered: true}]]}

But Iam stuck with creating multiple 'AND' 'OR' conditions with searchkick.

I need something like

A OR B OR ( C AND D )

or I need like this,

A AND B AND ( C OR D )

Please guide me, how to achieve this

Thanks

Answer

A OR B OR ( C AND D )

Product.search where: {or: [[{brand: 'nike'}, {in-stock: true}, {price: {lte: 12}, color: 'red'}]]}

A AND B AND ( C OR D )

Product.search where: {brand: 'nike', in-stock: true, or: [ [{price: {lte: 12}}, {color: 'red'}] ]}

Update

(A OR B) AND (C OR D)

 Product.search where: {or: [[ {or: [[{brand: "nike"}, {in-stock: "true"}]]}], [{or: [[{price: 100}, {color: "red"}]]}]]}