OpenSearch Python Client Logo
  • API Reference
    • Clients
    • Connection Types
    • connection_pool
    • exceptions
    • helpers
      • aggs
      • analysis
      • document
      • faceted_search
        • FacetedSearch
      • field
      • function
      • index
      • mapping
      • query
      • search
      • update_by_query
      • wrappers
    • metrics
    • Plugins
    • serializer
    • transport
  • Index
  • License
  • Contributing
  • Code of Conduct
  • Developer Guide
  • GitHub Repository
OpenSearch Python Client
  • API Reference
  • helpers
  • faceted_search
  • Edit on GitHub

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:
  • query (Any) – the text to search for

  • filters (Any) – facet values to filter

  • sort (Any) – sort information to be passed to Search

  • query –

  • filters –

  • sort –

add_filter(name, filter_values)[source]

Add a filter for a facet.

Parameters:
  • name (Any) –

  • filter_values (Any) –

Return type:

Any

aggregate(search)[source]

Add aggregations representing the facets selected, including potential filters.

Parameters:

search (Any) –

Return type:

Any

build_search()[source]

Construct the Search object.

Return type:

Any

execute()[source]

Execute the search and return the response.

Return type:

Any

filter(search)[source]

Add a post_filter to the search request narrowing the results based on the facet filters.

Parameters:

search (Any) –

Return type:

Any

highlight(search)[source]

Add highlighting for all the fields

Parameters:

search (Any) –

Return type:

Any

query(search, query)[source]

Add query part to search.

Override this if you wish to customize the query used.

Parameters:
  • search (Any) –

  • query (Any) –

Return type:

Any

search()[source]

Returns the base Search object to which the facets are added.

You can customize the query by overriding this method and returning a modified search object.

Return type:

Any

sort(search)[source]

Add sorting information to the request.

Parameters:

search (Any) –

Return type:

Any

Previous Next

© Copyright OpenSearch Project Contributors.