3

I am trying to construct must query on multiple terms, the array looks like this:

$params = [
'body' => [
    'query' => [
        "bool" => [
            "must" => [
                "terms" => [
                    "categories" => [
                        "Seating",
                    ],
                ],
                "terms" => [
                    "attributes.Color" => [
                        "Black",
                    ],
                ]
            ],
            "filter" => [
                "range" => [
                    "price" => [
                        "gte" => 39,
                        "lte" => 2999,
                    ],
                ],
            ],
        ],
    ],
    'from' => 0,
    'size' => 3,
],
];

Which is represented in JSON like this:

{
"query": {
    "bool": {
        "must": {
            "terms": {
                "attributes.Color": ["Black"]
            }
        },
        "filter": {
            "range": {
                "price": {
                    "gte": "39",
                    "lte": "2999"
                }
            }
        }
    }
},
"from": 0,
"size": 3
}

The problem is, JSON objects are represented as arrays in PHP so if I setup key for one array, it is rewritten. Do you have any idea on how to create multiple terms query in PHP?

Thanks in advance.

1
  • why not group same key values? like this "terms" => [ "categories" => [ "Seating" ], "attributes.Color" => [ "Black" ] ], Commented Jun 13, 2016 at 13:18

1 Answer 1

3

You need to add an additional array to enclose all your terms queries

$params = [
'body' => [
    'query' => [
        "bool" => [
            "must" => [
              [
                "terms" => [
                    "categories" => [
                        "Seating",
                    ],
                ]
              ],
              [
                "terms" => [
                    "attributes.Color" => [
                        "Black",
                    ],
                ]
              ]
            ],
            "filter" => [
                "range" => [
                    "price" => [
                        "gte" => 39,
                        "lte" => 2999,
                    ],
                ],
            ],
        ],
    ],
    'from' => 0,
    'size' => 3,
],
];
Sign up to request clarification or add additional context in comments.

Comments

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.