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

(new \Elastica\Query\Bool())->toArray()) and json_encode() generate problematic ES payload #817

Closed
mfn opened this issue Apr 21, 2015 · 3 comments

Comments

@mfn
Copy link
Contributor

mfn commented Apr 21, 2015

I know this method says toArray() but nevertheless it can generate a payload which still isn't suitable for ES because it would require a new stdClass instead of an empty array.

The code

var_dump((new \Elastica\Query\Bool())->toArray());

generates

array(1) {
  ["bool"]=>
  array(0) {
  }
}

and json encoded for ES this gets

echo json_encode((new \Elastica\Query\Bool())->toArray(), JSON_PRETTY_PRINT); # => { "bool": [] }

It seems this is problematic in certain cases and bool should be an empty object and not array.

Here's an example I discovered this (example from Sense):

POST /test/test/1
{
  "awesome": "stuff"
}

GET /test/test/_search
{
    "query": {
        "filtered": {
            "query": {
                "bool": []
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "not": {
                                "filter": {
                                    "missing": {
                                        "field": "whatever"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

This results in: ... nested: QueryParsingException[[test] No query registered for [not]]; }]

When however using {} for bool the query executes fine.

Here's a more "PHP only" example:

    $body = new \Elastica\Query();
    $body->setQuery(
      new \Elastica\Query\Filtered(
        new \Elastica\Query\Bool(),
        (new \Elastica\Filter\Bool())
          ->addShould(
            new \Elastica\Filter\BoolNot(
              new \Elastica\Filter\Missing('whatever'))
        )
      )
    );
    echo json_encode($body->toArray(), JSON_PRETTY_PRINT);

Results in:

{
    "query": {
        "filtered": {
            "query": {
                "bool": []
            },
            "filter": {
                "bool": {
                    "should": [
                        {
                            "not": {
                                "filter": {
                                    "missing": {
                                        "field": "whatever"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

Sending this to ES will generate the aforementioned error message.

@ruflin
Copy link
Owner

ruflin commented Apr 24, 2015

Based on some tests I did and issue elastic/elasticsearch#7240 an empty bool query should act like a match all query. I will add a fix for this issue.

@ruflin
Copy link
Owner

ruflin commented Apr 24, 2015

See #822

ruflin added a commit that referenced this issue Apr 24, 2015
Fix empty bool query to act as match all query #817
@ruflin
Copy link
Owner

ruflin commented Apr 24, 2015

Close ashttps://github.com//pull/822 is merged.

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