0

I would like make query like where price > 555 and price < 5555 and quantity > 33

I have query like:

$query = array(
            'query' => queryFilters($filters),
            'sort' => array($sortOrder[0] => $sortOrder[1], 'updated_at' => 'desc'),
            'from' => ($page - 1) * $pageSize,
            'size' => $pageSize,
        );

Now, lets assume my filters is like:

filter[price_from]=555&filter[price_to]=5555&filter[quantity_from]=33

What should be result of queryFilters($filters) to make it work? I tried filtered query from this doc but result was like or (used array of ranges: 0=>range[...], 1=>range[...], 2=>range[...]) but result was like or query. Any suggestions? I will have query=>constant_score=>filter=>exists=>field[items] and [missing](same structure) in result of queryFilters($filters) also. Using range just for one (price gte, lte) works good, but cant use it for multiple fields (price, quantity).

1 Answer 1

3

You basically need something like this that uses bool/must with two range filters:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "price": {
                  "gt": 555,
                  "lt": 5555
                }
              }
            },
            {
              "range": {
                "quantity": {
                  "gt": 33
                }
              }
            }
          ]
        }
      }
    }
  }
}

So, your queryFilters function needs to build the above query out of the query string you're getting. I guess it's pretty straightforward from there on.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, so, should is something like 'OR' and 'must' is 'AND'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.