def __init__(self, **kwargs): super(SolrGroupSearchQuery, self).__init__(**kwargs) self.group = True self.gquery_filter = SearchNode() self.group_format = "simple" self.group_ngroups = True self.group_queries = set()
def add_group_query(self, query_filter, use_or=False, is_master=True, tag=None): """ Adds a group query to the current query. """ if use_or: connector = SQ.OR else: connector = SQ.AND if self.gquery_filter and query_filter.connector != SQ.AND and len( query_filter) > 1: self.gquery_filter.start_subtree(connector) subtree = True else: subtree = False for child in query_filter.children: if isinstance(child, tree.Node): self.gquery_filter.start_subtree(connector) self.add_group_query(child, is_master=False) self.gquery_filter.end_subtree() else: expression, value = child self.gquery_filter.add((expression, value), connector) connector = query_filter.connector if query_filter.negated: self.gquery_filter.negate() if subtree: self.gquery_filter.end_subtree() if is_master: # If and only if we have iterated through all the children of the # query_filter, the SQ object, append the query fragment to the # set of group queries, and reset self.gquery_filter back to an # empty SearchNode. qfrag = self.gquery_filter.as_query_string( self.build_query_fragment) # If specified, add a local param to identify individual group # query statements. if tag: qfrag = '{!tag="%s"} ' % str(tag) + qfrag if qfrag: self.group_queries.add(qfrag) self.gquery_filter = SearchNode()
def add_group_query(self, query_filter, use_or=False, is_master=True, tag=None): """ Adds a group query to the current query. """ if use_or: connector = SQ.OR else: connector = SQ.AND if self.gquery_filter and query_filter.connector != SQ.AND and len(query_filter) > 1: self.gquery_filter.start_subtree(connector) subtree = True else: subtree = False for child in query_filter.children: if isinstance(child, tree.Node): self.gquery_filter.start_subtree(connector) self.add_group_query(child, is_master=False) self.gquery_filter.end_subtree() else: expression, value = child self.gquery_filter.add((expression, value), connector) connector = query_filter.connector if query_filter.negated: self.gquery_filter.negate() if subtree: self.gquery_filter.end_subtree() if is_master: # If and only if we have iterated through all the children of the # query_filter, the SQ object, append the query fragment to the # set of group queries, and reset self.gquery_filter back to an # empty SearchNode. qfrag = self.gquery_filter.as_query_string(self.build_query_fragment) # If specified, add a local param to identify individual group # query statements. if tag: qfrag = '{!tag="%s"} ' % str(tag) + qfrag if qfrag: self.group_queries.add(qfrag) self.gquery_filter = SearchNode()
class SolrGroupSearchQuery(SolrSearchQuery): def __init__(self, **kwargs): super(SolrGroupSearchQuery, self).__init__(**kwargs) self.group = True self.gquery_filter = SearchNode() self.group_format = "simple" self.group_ngroups = True self.group_queries = set() def add_group_query(self, query_filter, use_or=False, is_master=True, tag=None): """ Adds a group query to the current query. """ if use_or: connector = SQ.OR else: connector = SQ.AND if self.gquery_filter and query_filter.connector != SQ.AND and len( query_filter) > 1: self.gquery_filter.start_subtree(connector) subtree = True else: subtree = False for child in query_filter.children: if isinstance(child, tree.Node): self.gquery_filter.start_subtree(connector) self.add_group_query(child, is_master=False) self.gquery_filter.end_subtree() else: expression, value = child self.gquery_filter.add((expression, value), connector) connector = query_filter.connector if query_filter.negated: self.gquery_filter.negate() if subtree: self.gquery_filter.end_subtree() if is_master: # If and only if we have iterated through all the children of the # query_filter, the SQ object, append the query fragment to the # set of group queries, and reset self.gquery_filter back to an # empty SearchNode. qfrag = self.gquery_filter.as_query_string( self.build_query_fragment) # If specified, add a local param to identify individual group # query statements. if tag: qfrag = '{!tag="%s"} ' % str(tag) + qfrag if qfrag: self.group_queries.add(qfrag) self.gquery_filter = SearchNode() def run(self, spelling_query=None): """Builds & executes the query. Returns a list of result groupings.""" final_query = self.build_query() kwargs = { 'start_offset': self.start_offset, 'result_class': self.result_class, 'group_query': [i for i in self.group_queries], 'group_format': self.group_format, 'group_ngroups': self.group_ngroups } if self.order_by: order_by_list = [] for order_by in self.order_by: if order_by.startswith('-'): order_by_list.append('%s desc' % order_by[1:]) else: order_by_list.append('%s asc' % order_by) kwargs['sort_by'] = ", ".join(order_by_list) if self.narrow_queries: kwargs['narrow_queries'] = self.narrow_queries if self.query_facets: kwargs['query_facets'] = self.query_facets if self.end_offset is not None: kwargs['end_offset'] = self.end_offset if self.highlight: kwargs['highlight'] = self.highlight if spelling_query: kwargs['spelling_query'] = spelling_query self._results = self.backend.search(final_query, **kwargs) self._hit_count = sum([r['hits'] for r in self._results]) def has_run(self): """Indicates if any query has been run.""" return None not in (self._results, self._hit_count) def _clone(self, **kwargs): clone = super(SolrGroupSearchQuery, self)._clone(**kwargs) clone.group_queries = self.group_queries return clone
class SolrGroupSearchQuery(SolrSearchQuery): def __init__(self, **kwargs): super(SolrGroupSearchQuery, self).__init__(**kwargs) self.group = True self.gquery_filter = SearchNode() self.group_format = "simple" self.group_ngroups = True self.group_queries = set() def add_group_query(self, query_filter, use_or=False, is_master=True, tag=None): """ Adds a group query to the current query. """ if use_or: connector = SQ.OR else: connector = SQ.AND if self.gquery_filter and query_filter.connector != SQ.AND and len(query_filter) > 1: self.gquery_filter.start_subtree(connector) subtree = True else: subtree = False for child in query_filter.children: if isinstance(child, tree.Node): self.gquery_filter.start_subtree(connector) self.add_group_query(child, is_master=False) self.gquery_filter.end_subtree() else: expression, value = child self.gquery_filter.add((expression, value), connector) connector = query_filter.connector if query_filter.negated: self.gquery_filter.negate() if subtree: self.gquery_filter.end_subtree() if is_master: # If and only if we have iterated through all the children of the # query_filter, the SQ object, append the query fragment to the # set of group queries, and reset self.gquery_filter back to an # empty SearchNode. qfrag = self.gquery_filter.as_query_string(self.build_query_fragment) # If specified, add a local param to identify individual group # query statements. if tag: qfrag = '{!tag="%s"} ' % str(tag) + qfrag if qfrag: self.group_queries.add(qfrag) self.gquery_filter = SearchNode() def run(self, spelling_query=None, **kwargs): """Builds & executes the query. Returns a list of result groupings.""" final_query = self.build_query() kwargs['start_offset'] = self.start_offset kwargs['result_class'] = self.result_class kwargs['group_query'] = [i for i in self.group_queries] kwargs['group_format'] = self.group_format kwargs['group_ngroups'] = self.group_ngroups if self.order_by: order_by_list = [] for order_by in self.order_by: if order_by.startswith('-'): order_by_list.append('%s desc' % order_by[1:]) else: order_by_list.append('%s asc' % order_by) kwargs['sort_by'] = ", ".join(order_by_list) if self.narrow_queries: kwargs['narrow_queries'] = self.narrow_queries if self.query_facets: kwargs['query_facets'] = self.query_facets if self.end_offset is not None: kwargs['end_offset'] = self.end_offset if self.highlight: kwargs['highlight'] = self.highlight if spelling_query: kwargs['spelling_query'] = spelling_query self._results = self.backend.search(final_query, **kwargs) self._hit_count = sum([r['hits'] for r in self._results]) def has_run(self): """Indicates if any query has been run.""" return None not in (self._results, self._hit_count) def _clone(self, **kwargs): clone = super(SolrGroupSearchQuery, self)._clone(**kwargs) clone.group_queries = self.group_queries return clone