10

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

Dias
  • 862
  • 8
  • 17
  • 2
    Got the 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'}] ]} – Dias Jan 12 '15 at 05:26
  • You should answer your own question. That way you help the community by highlighting the answer and indicating that this problem was solved. Also you'll get points ;) – Ole Henrik Skogstrøm Dec 14 '15 at 13:56

2 Answers2

27

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"}]]}]]}
Dias
  • 862
  • 8
  • 17
0

For (A OR B) AND (C OR D) it should be (no double brackets needed):

where: { _and: [ { _or: [ {A}, {B} ] }, { _or: [{C}, {D}] } ] }