# Multiple OR and AND conditions with Elasticsearch php

**URL:** <https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286>\
**Category:** Elasticsearch\
**Created:** [July 9, 2021, 2:39pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286 "2021-07-09T14:39:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmastery](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hmastery](https://discuss.elastic.co/u/hmastery)\
**Post date:** [July 9, 2021, 2:39pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/1 "2021-07-09T14:39:06Z")

</div>

I would like to achieve the following query with ElasticSearch :

product\_shipper = 1 AND ((product\_type = product and price\>0) OR (product\_type = product\_variation and price=0))

I've built the following query but it doesn't work, sending me an empty result 🙂

```auto
                  "bool"=> [
                     "must"=> [
                        [
                           "bool"=> [
                              "should" => [
                                    "bool"=> [
                                        "must"=> [
                                            ["match" => [ "product_type" => "product"] ],
                                            [ 
                                                'range'=>['price'=>['gt'=>0]]
                                            ],
                                        ]
                                    ],
                                     "bool"=> [
                                        "must"=> [
                                            ["match" => [ "product_type" => "product_variation"] ],
                                            ["match" => [ "price" => 0] ]
                                        ]
                                    ]
                              ]
                           ]
                        ],
                        ['match' => [ 'product_shipper' => $shipper] ],
                     ]
                  ]
               ]

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 9, 2021, 3:13pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/2 "2021-07-09T15:13:30Z")

</div>

Start simple, add complexity, does this work by itself?

```auto
 ['match' => [ 'product_shipper' => $shipper] ]

```

---

<div class="post-metadata">

**Author:** ![hmastery](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hmastery](https://discuss.elastic.co/u/hmastery)\
**Post date:** [July 9, 2021, 3:20pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/3 "2021-07-09T15:20:49Z")

</div>

Thanks Rugenl for your answer.

Yes 🙂 it does.

I've also modified slightly the request to this :

```auto

               "query"=> [
                    "bool"=> [
                        "must"=> [
                            [
                            "bool"=> [
                                "should" => [
                                    [
                                        "bool"=> [
                                            "must"=> [
                                                ["match" => [ "product_type" => "product"] ],
                                                ["range"=>['price'=>['gt'=>0]]],
                                            ]
                                        ],
                                        "bool"=> [
                                            "must"=> [
                                                ["match" => [ "product_type" => "product_variation"] ],
                                                ['range'=>['price'=>['lte'=>0]]]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        ['match' => [ 'product_shipper' => $shipper] ],
                        ]
                    ]
               ]

```

But what happens is that I only have result for product\_shipper = $shipper AND product\_type = "product\_variation" AND price \<= 0

The other part of the should request is ignored, there is no product\_type = "product" and prive \> 0 in the results, and yes I checked there many of them in my dataset.

Any idea why it just analyzes the second part of my should clause ?

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 9, 2021, 6:20pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/4 "2021-07-09T18:20:02Z")

</div>

Comparing what you have to this [answer](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/2), you have `"should" => [ [` they have `"should" => [ {`

I wonder if the array of things keeps resetting the keys to the last value seen vs processing them as a list {}.

---

<div class="post-metadata">

**Author:** ![hmastery](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hmastery](https://discuss.elastic.co/u/hmastery)\
**Post date:** [July 11, 2021, 5:24pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/5 "2021-07-11T17:24:08Z")

</div>

There is no {} in the php sdk only . Damn I feel so lost on this one. It's like I'm super close to get it right but no haha. So frustrating.

Any other idea Rugen ? Maybe the syntax I use within the should is not correct? I don't think there is an issue with the official php SDK for such a simple task. But there is so few documentation for it.

How would you write this query if you were to use the regular CURL syntax?

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 11, 2021, 7:29pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/6 "2021-07-11T19:29:08Z")

</div>

Yea, you've brought a good one 🙂 I'd suggest opening an issue on the [git project](https://github.com/elastic/elasticsearch-php) for this.

Another thing to try is to switch the order of the "OR" items, just so see if it changes the results.

---

<div class="post-metadata">

**Author:** ![hmastery](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hmastery](https://discuss.elastic.co/u/hmastery)\
**Post date:** [July 11, 2021, 8:53pm UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/7 "2021-07-11T20:53:34Z")

</div>

Yep, I just opened a ticket : [Multiple Must within Should clause does not work · Issue #1152 · elastic/elasticsearch-php · GitHub](https://github.com/elastic/elasticsearch-php/issues/1152)

Let's see if I'm wrong or if there is an issue in the SDK, which I doubt but still ^^

Thanks for your help man 🙂 , if anyone else has something in mind, just let me know. I've been stuck for 2 days on this one.

---

<div class="post-metadata">

**Author:** ![hmastery](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hmastery](https://discuss.elastic.co/u/hmastery)\
**Post date:** [July 14, 2021, 8:42am UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/8 "2021-07-14T08:42:28Z")

</div>

Hello 🙂

There was an issue in my syntax within the should clause...

Here is the proper code :

```auto
"query"=> [
    "bool"=> [
        "must"=> [
            [
                "bool"=> [
                    "should" => [
                        [
                            "bool"=> [
                                "must"=> [
                                    ["match" => [ "product_type" => "product"] ],
                                    ["range"=>['price'=>['gt'=>0]]],
                                ]
                            ],
                        ],
                        [
                            "bool"=> [
                                "must"=> [
                                    ["match" => [ "product_type" => "product_variation"] ],
                                    ['range'=>['price'=>['lte'=>0]]]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            [
                'match' => ['product_shipper' => $shipper]
            ],
        ]
    ]
]

```

Thanks a lot for your help 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 11, 2021, 8:42am UTC](https://discuss.elastic.co/t/multiple-or-and-and-conditions-with-elasticsearch-php/278286/9 "2021-08-11T08:42:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
