Skip to content

Conversation

cjrh
Copy link
Collaborator

@cjrh cjrh commented Sep 6, 2025

This adds .and_must_match(), and_must_not_match(), and or_should_match() methods which produce boolean queries that can be chained in a fluent interface. Some care is taken to keep the chain flatter by reusing a BooleanQuery layer (and simply append the subsequent clauses).

Here's a silly example from the tests:

        index = ram_index
        searcher = index.searcher()

        # Queries for testing
        query_mice = Query.term_query(index.schema, "title", "mice")  # Matches "Of Mice and Men"
        query_old = Query.term_query(index.schema, "title", "old")  # Matches "The Old Man and the Sea"
        query_man = Query.term_query(index.schema, "title", "man")  # Matches "The Old Man and the Sea"

        # "The Old Man and the Sea" contains both "old" and "man"
        # (but with many chains)
        combined_must = (
            query_old
            .and_must_match(query_man)
            .and_must_not_match(query_mice)
        )
        result = searcher.search(combined_must, 10)
        assert len(result.hits) == 1

@cjrh cjrh requested review from wallies and Sidhant29 September 6, 2025 03:38
Comment on lines +73 to +74
let inner: Box<dyn tv::query::Query> = if let Some(boolean_query) =
self.inner.downcast_ref::<BooleanQuery>()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently this only checks whether self is a boolean query and will then reuse that to append the new clause; however, it is possible that other is also a boolean query. So a further optimization can be made to either append to other (if a BQ), or to fully merge self and other if they're both BQ.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(some care must be taken when doing the appends, in case other_occur is a MUST NOT)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OTOH we may want to intentionally NOT incorporate optimizations for other, in light of #287 (comment). It may be desired to only pull things into an existing BooleanQuery when it's on the "left hand side".

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.

1 participant