def _get_slice(self, elements: IResultSet, query: IResultSet) -> Iterable: """Get slice defined by `query.limit` and `query.offset`. :returns: IResultSet if not `query.limit`, else [IResource] """ elements_slice = elements if query.limit: docids_slice = islice(elements.all(resolve=None), query.offset, query.offset + query.limit) elements_slice = [elements.resolver(x) for x in docids_slice] return elements_slice
def _sort_elements(self, elements: IResultSet, query: SearchQuery) -> IResultSet: if query.sort_by == '': pass elif query.sort_by == 'reference': if query.reverse: raise NotImplementedError() if query.limit: raise NotImplementedError() references = [x for x in query.references if x[0] is not None] if not references: # we need at least one reference return elements reference = references[0] references_index = self.get_index('reference') elements_sorted = references_index.search_with_order(reference) elements = elements_sorted.intersect(elements) else: sort_index = self.get_index(query.sort_by) # TODO: We should assert the IIndexSort interface here, but # hypatia.field.FieldIndex is missing this interface. assert 'sort' in sort_index.__dir__() elements = elements.sort(sort_index, reverse=query.reverse, limit=query.limit or None) return elements
def _sort_elements(self, elements: IResultSet, query: SearchQuery) -> IResultSet: sort_index = self._get_index(query.sort_by) if sort_index is not None: # TODO: We should assert the IIndexSort interface here, but # hypatia.field.FieldIndex is missing this interface. assert "sort" in sort_index.__dir__() elements = elements.sort(sort_index, reverse=query.reverse, limit=query.limit or None) return elements
def test_search_with_group_by_and_resolve_false(self, registry, pool, inst, query): from adhocracy_core.interfaces import ISimple from hypatia.interfaces import IResultSet child = self._make_resource(registry, parent=pool, iresource=ISimple) result = inst.search(query._replace(group_by='interfaces', resolve=False)) assert IResultSet.providedBy(result.group_by[ISimple])
def _sort_elements(self, elements: IResultSet, query: SearchQuery) -> IResultSet: sort_index = self.get_index(query.sort_by) if sort_index is not None: # TODO: We should assert the IIndexSort interface here, but # hypatia.field.FieldIndex is missing this interface. assert 'sort' in sort_index.__dir__() elements = elements.sort(sort_index, reverse=query.reverse, limit=query.limit or None) return elements
def _get_frequency_of(self, elements: IResultSet, query: SearchQuery) -> dict: frequency_of = {} if query.frequency_of: index = self.get_index(query.frequency_of) for value in index.unique_values(): value_query = index.eq(value) value_elements = value_query.execute(resolver=None) intersect = elements.intersect(value_elements) count = len(intersect) if count == 0: continue frequency_of[value] = count return frequency_of
def _get_group_by(self, elements: IResultSet, query: SearchQuery) -> dict: group_by = {} if query.group_by: index = self.get_index(query.group_by) for value in index.unique_values(): value_query = index.eq(value) value_elements = value_query.execute(resolver=None) intersect = elements.intersect(value_elements) if len(intersect) == 0: continue group_by[value] = intersect sort_index = self.get_index(query.sort_by) if sort_index is not None and query.sort_by != "reference": for key, intersect in group_by.items(): intersect_sorted = intersect.sort(sort_index, reverse=query.reverse, limit=query.limit or None) group_by[key] = intersect_sorted for key, intersect in group_by.items(): group_by[key] = self._resolve(intersect.all(), query) return group_by
def _get_group_by(self, elements: IResultSet, query: SearchQuery) -> dict: group_by = {} if query.group_by: index = self.get_index(query.group_by) for value in index.unique_values(): value_query = index.eq(value) value_elements = value_query.execute(resolver=None) intersect = elements.intersect(value_elements) if len(intersect) == 0: continue group_by[value] = intersect sort_index = self.get_index(query.sort_by) if sort_index is not None and query.sort_by != 'reference': for key, intersect in group_by.items(): intersect_sorted = intersect.sort(sort_index, reverse=query.reverse, limit=query.limit or None) group_by[key] = intersect_sorted for key, intersect in group_by.items(): group_by[key] = self._resolve(intersect.all(), query) return group_by
def _sort_elements(self, elements: IResultSet, query: SearchQuery) -> IResultSet: if query.sort_by == '': pass elif query.sort_by == 'reference': if query.reverse: raise NotImplementedError() if query.limit: raise NotImplementedError() references = [x for x in query.references if x[0] is not None] if not references: # we need at least one reference return elements reference = self._get_query_value(references[0]) references_index = self.get_index('reference') elements_sorted = references_index.search_with_order(reference) elements = elements_sorted.intersect(elements) else: sort_index = self.get_index(query.sort_by) # TODO: We should assert the IIndexSort interface here, but # hypatia.field.FieldIndex is missing this interface. assert 'sort' in sort_index.__dir__() elements = elements.sort(sort_index, reverse=query.reverse) return elements
def test_search_default_query(self, registry, pool, inst, query): from hypatia.interfaces import IResultSet child = self._make_resource(registry, parent=pool) result = inst.search(query) assert IResultSet.providedBy(result.elements) assert list(result.elements) == [child]