Skip to content

Conversation

@ajroetker
Copy link
Contributor

@ajroetker ajroetker commented Nov 11, 2025

This change adds support for filtering facet terms by prefix and/or regex pattern. Which is useful for faceting during search_as_you_type style faceting for terms that match what is in the searchbox, aggregated by field (for books I can facet facet authors, titles etc in an autosuggest a bit cleaner now).

  • Adds TermPrefix and TermPattern fields to FacetRequest
  • Updated NewTermsFacetBuilder to return error
  • Only matching terms trigger string allocation from []byte

This change adds support for filtering facet terms by prefix and/or
regex pattern. Which is useful for faceting during search_as_you_type
style faceting for terms that match what is in the searchbar,
aggregated by field (for books I can facet facet authors, titles etc
in an autosuggest a bit cleaner now).

- Adds TermPrefix and TermPattern fields to FacetRequest
- Updated all callers of NewTermsFacetBuilder (now returns error)
- Only matching terms trigger string allocation from []byte

Example usage:
{
  "facets": {
    "prod_categories": {
      "field": "category",
      "size": 10,
      "prefix": "prod-"
    }
  }
}
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds prefix and regex filtering capabilities to term facets in Bleve's search functionality, enabling search-as-you-type style faceting where terms can be filtered to match patterns during aggregation.

Key changes:

  • Added TermPrefix and TermPattern fields to FacetRequest struct for filtering configuration
  • Modified NewTermsFacetBuilder to accept prefix and pattern parameters and return an error for invalid regex patterns
  • Optimized UpdateVisitor to perform zero-allocation prefix and regex matching on []byte before converting to string

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
search.go Added TermPrefix and TermPattern fields to FacetRequest for API support
search/facet/facet_builder_terms.go Implemented prefix/regex filtering logic with optimized byte-level matching
index_impl.go Updated to pass prefix and pattern parameters to NewTermsFacetBuilder and handle errors
search/collector/topn_test.go Updated test to handle new error-returning signature of NewTermsFacetBuilder
search/facet/facet_builder_terms_test.go Added comprehensive test coverage for prefix, regex, combined filtering, and edge cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add SetPrefixFilter/SetRegexFilter to FacetRequest and TermsFacetBuilder
- Validate regex patterns in FacetRequest.Validate()
- Revert NewTermsFacetBuilder signature to avoid breaking changes
- Improve code clarity and size estimation
@ajroetker ajroetker force-pushed the ajroetker/term-facet-filtering branch from 823fd99 to e344c09 Compare November 13, 2025 16:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (1)

search/facet/facet_builder_terms.go:122

  • The missing counter may not accurately reflect documents without matching values when filters are active. A document with terms that don't match the filters will be counted as "missing" even though it has values for the field. Consider whether this is the intended behavior or if missing should only count documents that truly have no value for the field, regardless of filters.

If the current behavior is intentional (i.e., "missing" means "no matching values"), the documentation should be updated to clarify this semantic change when filters are applied.

}

func (fb *TermsFacetBuilder) EndDoc() {
	if !fb.sawValue {
		fb.missing++
	}
}


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Member

@abhinavdangeti abhinavdangeti left a comment

Choose a reason for hiding this comment

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

This is great, thanks for this @ajroetker .

@abhinavdangeti abhinavdangeti modified the milestones: v2.6.0, v2.5.6 Nov 14, 2025
@abhinavdangeti abhinavdangeti merged commit d242813 into blevesearch:master Nov 14, 2025
9 checks passed
@ajroetker ajroetker deleted the ajroetker/term-facet-filtering branch November 15, 2025 06:29
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