QueryParser: make empty-query behaviour configurable (set_empty_query_match_all) #2655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
an empty user query ("", whitespace-only, or a string that tokenises to nothing) is always translated into EmptyQuery, which matches no documents.
Issue #386 requests making this behaviour configurable because analytics dashboards and “show everything” listings often prefer an empty search box to return all documents.
What’s in this PR
API additions
set_empty_query_match_all(should_match_all: bool) – opt-in/out.
get_empty_query_match_all() -> bool – read current setting.
Centralised conversion – new helper logical_ast_to_query ensures the flag is respected by all parse_* and build_* entry points.
If the parsed AST is empty -> returns
AllQuery when the flag is true.
EmptyQuery (unchanged legacy behaviour) when false.
No breaking changes – the default remains “match nothing”; all existing tests continue to pass.
Docs – inline rustdoc comments explain the new behaviour.
Usage example
-------------```rust
let mut query_parser = QueryParser::for_index(&index, default_fields);
// Opt-in so that an empty search matches everything.
query_parser.set_empty_query_match_all(true);
// "" now yields AllQuery instead of EmptyQuery
let query = query_parser.parse_query("").unwrap();
assert_eq!(format!("{query:?}"), "AllQuery");
Impact
• Allows downstream applications to implement a “show all” behaviour without post-processing.
• Does not affect existing users unless they explicitly enable it.
Closes [#386]