faceted_search
- class opensearchpy.helpers.faceted_search.FacetedSearch(query=None, filters={}, sort=())[source]
Bases:
object
Abstraction for creating faceted navigation searches that takes care of composing the queries, aggregations and filters as needed as well as presenting the results in an easy-to-consume fashion:
class BlogSearch(FacetedSearch): index = 'blogs' doc_types = [Blog, Post] fields = ['title^5', 'category', 'description', 'body'] facets = { 'type': TermsFacet(field='_type'), 'category': TermsFacet(field='category'), 'weekly_posts': DateHistogramFacet(field='published_from', interval='week') } def search(self): ' Override search to add your own filters ' s = super(BlogSearch, self).search() return s.filter('term', published=True) # when using: blog_search = BlogSearch("web framework", filters={"category": "python"}) # supports pagination blog_search[10:20] response = blog_search.execute() # easy access to aggregation results: for category, hit_count, is_selected in response.facets.category: print( "Category %s has %d hits%s." % ( category, hit_count, ' and is chosen' if is_selected else '' ) )
- Parameters:
- aggregate(search)[source]
Add aggregations representing the facets selected, including potential filters.
- filter(search)[source]
Add a
post_filter
to the search request narrowing the results based on the facet filters.
- query(search, query)[source]
Add query part to
search
.Override this if you wish to customize the query used.