Skip to content

Fixing the exclude query - #450

Merged
YaphetKG merged 2 commits into
developfrom
fix/exclude_query
Sep 22, 2026
Merged

YaphetKG merged 2 commits into
developfrom
fix/exclude_query

Conversation

@hina-shah

Copy link
Copy Markdown
Contributor

No description provided.

@hina-shah
hina-shah requested review from YaphetKG and gaurav September 8, 2026 15:04

@gaurav gaurav left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I've made some minor suggestions for documentation and improving the assertion output, but feel free to ignore them if they're not useful.

body = _build_all_query_builders()[builder_name]
must = body["query"]["bool"]["must"]
function_score = (must[0] if isinstance(must, list) else must)["function_score"]
should = function_score["query"]["bool"]["should"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really the case that:

must = body["query"]["bool"]["must"]
should = body["query"]["bool"]["must"]["function_score"]["query"]["bool"]["should"]

If so, it might be useful to add a comment explaining why there's a query instead a function score inside another query (I'm guessing the internal query calculates a score for the outer query?).

"query": query,
"default_operator": "and",
"flags": "OR|AND|NOT|PHRASE|PREFIX"
"flags": "OR|AND|NOT|PHRASE|PREFIX|WHITESPACE"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to link to the relevant documentation (https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-simple-query-string-query#supported-flags) unless that makes this code harder to read.

Comment thread tests/unit/test_async_search.py Outdated
for clause in should:
sqs = clause.get("simple_query_string") or clause["nested"]["query"]["simple_query_string"]
flags.add(sqs["flags"])
self.assertEqual(len(flags), 1, "expected identical flags across should clauses")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner to assert identical flags across should clauses within the loop, so that the assertion error clearly indicates which clause e.g.

first_flags = set()
for clause in should:
  ...
  flags = sqs["flags"]
  self.assertTrue(flags, f"Empty flags found in clause {clause}")
  if not first_flags:
    first_flags = sqs["flags"]
  self.assertEqual(sqs["flags"], first_flags, f"Flags in clause {clause} ({flags}) differ from expected flags {first_flags} found before it in the query {should}")

If it is better to put that outside the clause loop, then I would change this write out the different flags we've seen:

Suggested change
self.assertEqual(len(flags), 1, "expected identical flags across should clauses")
self.assertEqual(len(flags), 1, f"expected identical flags across should clauses but got {len(flags)} combinations: {flags}")

@YaphetKG
YaphetKG merged commit 7f04f28 into develop Sep 22, 2026
9 checks passed
@YaphetKG
YaphetKG deleted the fix/exclude_query branch September 22, 2026 15:38
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

Successfully merging this pull request may close these issues.

3 participants