Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 678
Dias Avatar asked Jan 08 '15 06:01

Dias


People also ask

How to write complex SQL queries?

How to write complex sql queries? 1.Use of Multiple AND OR operators : The queries which are using multiple AND OR operators are called as complex queries. 2.Use of subqueries and corelated subqueries : The subqueries are also the complex in nature. We require to use multiple... 3.Use of Joins The ...

Why I have tried to explain each and every query in detail?

I have tried to explain each and every query in detail so that everyone will get idea of how it is executed step-by-step. In SQL and PL SQL interviews we require to know the key and important complex sql queries in detail level. These are important Complex SQL Queries :

How many rows are there in a complex query?

As you can see, we have a complex query and 2 rows in the result. Without any comments, we can’t easily say what does this query does and how it works. Let’s change that now.

Is it possible to check the quality of a query?

Still, you can check it. I always advise that you test parts of your queries. While they won’t be displayed in the final results, they will be used in the background.


1 Answers

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"}]]}]]}
like image 198
Dias Avatar answered Oct 10 '22 06:10

Dias