Skip to content

Commit

Permalink
Avoid broken aggregation (#95)
Browse files Browse the repository at this point in the history
⚠️ this PR requires a proper review. I'm not 100% sure it will not brake things.

It will help to avoid having a `must` key outside of `bool` context.
Example:

Before a fix:
```
{
    "query": {
        ...
    },
    "aggs": {
        "topics": {
            "filters": {
                "filters": {
                    ...
                    "bool": {
                        "must": {
                            "bool": {
                                "must": [
                                    {
                                        "query_string": {
                                            ...
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}
```
which cause a
```
"caused_by": {
            "type": "named_object_not_found_exception",
            "reason": "[87:33] unknown field [must]"
        }
```

after the fix:
```
{
    "query": {
        ...
    },
    "aggs": {
        "topics": {
            "filters": {
                "filters": {
                    ...
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "query_string": {
                                        ...
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}
```

Initial query provided to `fix_query`:

```{'query': {'bool': {'must_not': [{'term': {'type': 'composite'}}, {'constant_score': {'filter': {'exists': {'field': 'nextversion'}}}}], 'must': [{'term': {'_id': 'foo'}}], 'should': [], 'filter': [{'bool': {'must': [{'term': {'_type': 'items'}}]}}]}}, 'aggs': {'topics': {'filters': {'filters': {'created_to_old': {'bool': {'must': [{'range': {'versioncreated': {'lte': '2017-01-01T23:59:59+0000'}}}]}}, 'created_from_future': {'bool': {'must': [{'range': {'versioncreated': {'gte': '2020-09-17T04:00:00+0000'}}}]}}, 'filter': {'bool': {'must': [{'terms': {'genre.name': ['other']}}]}}, 'query': {'bool': {'must': [{'query_string': {'query': 'Foo', 'default_operator': 'AND', 'lenient': True}}]}}}}}}, 'size': 0}```
  • Loading branch information
ride90 authored Sep 18, 2020
1 parent 933d9d6 commit 1f60ba0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.py[co]

# env
.python-version
/env
/venv
/bin
Expand Down
2 changes: 1 addition & 1 deletion eve_elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def fix_query(query, top=True, context=None):
elif key == "query_string":
new_query[key] = val
val.setdefault("lenient", True)
elif key == "query" and not top:
elif key == "query" and not top and context != "aggs":
new_query["bool"] = {"must": fix_query(val, top=False, context=context)}
elif top:
new_query[key] = fix_query(val, top=False, context=key)
Expand Down

0 comments on commit 1f60ba0

Please sign in to comment.