Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mustRaw, shouldRaw, etc is overwriting previous query conditions #162

Open
rohitjalmi90 opened this issue May 14, 2024 · 2 comments
Open

Comments

@rohitjalmi90
Copy link

rohitjalmi90 commented May 14, 2024

Hello @babenkoivan,

Thank you for awesome package :)

This is regarding previous issue mentioned here : #125

I was able to reproduce this using following testcases.
Note : All function ie mustRaw, sortRaw ignore the condition prior to using those function.
Testcases you provided are working as expected since mustRaw is before must

Testcase

$expected = [
            'bool' => [
                'must' => [
                    ['term' => ['year' => 2019]],
                    ['term' => ['year' => ['value' => 2020]]],
                ],
            ],
        ];
        
$actual = Query::bool()
    ->must(
        Query::term()
            ->field('year')
            ->value('2020')
    )
    ->mustRaw(['term' => ['year' => 2019]])
    ->buildQuery();
$this->assertEquals($expected, $actual);

Output : Failed

Query formed :
{"bool":{"must":{"term":{"year":2019}},"filter":[{"term":{"__soft_deleted":0}}]}}

Package version :

"babenkoivan/elastic-migrations": "^3.0",
"babenkoivan/elastic-scout-driver-plus": "^4.0",

"name": "babenkoivan/elastic-scout-driver-plus",
 "version": "v4.7.0",

"name": "babenkoivan/elastic-migrations",
 "version": "v3.4.0",
@babenkoivan
Copy link
Owner

Hi, @rohitjalmi90, the idea of mustRaw and similar functions is to allow you to provide the entire must clause in a single call, it completely replaces the "must" clause of the query. When you invoke mustRaw you tell the builder to replace "must" with the provided queries, when you call must you tell it to add a query to the "must" clause. This is why you can call mustRaw and then must, but not vice versa.

If you want to add just one raw query to the list, you can change your code to:

$actual = Query::bool()
    ->must(
        Query::term()
            ->field('year')
            ->value('2020')
    )
    ->must(['term' => ['year' => 2019]])
    ->buildQuery();

Note that must also accept an array as input.

Hope this helps :)

@babenkoivan
Copy link
Owner

I've updated the documentation to make it more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants